Guest User

Untitled

a guest
May 16th, 2018
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.64 KB | None | 0 0
  1. def post(self, request, *args, **kwargs):
  2. username = request.POST.get('username')
  3. password = request.POST.get('pass')
  4. api_user = InstagramAPI(username=username, password=password)
  5. api_user.login()
  6. if not api_user.isLoggedIn:
  7. message = json.loads(api_user.LastResponse.text)['message']
  8. messages.error(request, message)
  9. return render(request, self.template_name, {})
  10. else:
  11. if not User.objects.filter(username=api_user.username).exists():
  12. User.objects.create_user(username=api_user.username, password=password)
  13.  
  14. auth_user = authenticate(username=api_user.username, password=password)
  15. login(request, auth_user)
  16. return redirect('core:home')
  17. # api_user.getSelfUsernameInfo()
  18. # picture = api_user.LastJson['user']['profile_pic_url']
  19. # rate = self.get_social_rete(api_user)
  20. # return render(request, 'personal_page.html', {'pic': picture, 'rate': rate})
  21.  
  22.  
  23.  
  24. def get_social_rete(self, api_obj):
  25. DEFAULT_DATE_FORMAT = "%Y-%m-%d"
  26. date_today = date.today()
  27. date_x = date_today - timedelta(days=90)
  28. date_x = date_x.strftime(DEFAULT_DATE_FORMAT)
  29. timestamp_date = int(parser.parse(date_x).timestamp())
  30. api_obj.getSelfUserFollowers()
  31. set_followers = {i['username'] for i in api_obj.LastJson['users']}
  32. api_obj.getSelfUsersFollowing()
  33. set_following = {i['username'] for i in api_obj.LastJson['users']}
  34. api_obj.getSelfUserFeed(minTimestamp=timestamp_date)
  35. list_media_id = [i['pk'] for i in api_obj.LastJson['items']]
  36. n = len(list_media_id)
  37. bidirectional_connection = set_following & set_followers
  38. author_sub_user_not = set_following - bidirectional_connection
  39. user_sub_author_not = set_followers - bidirectional_connection
  40. total_A = 0
  41. total_B = 0
  42. total_C = 0
  43. total_D = 0
  44. for pk in list_media_id:
  45. api_obj.getMediaLikers(pk)
  46. list_likes_users = [i['username'] for i in api_obj.LastJson['users'] if i['username'] != api_obj.username]
  47. for i in list_likes_users:
  48. if i in bidirectional_connection:
  49. total_A += 1
  50. elif i in author_sub_user_not:
  51. total_B += 1
  52. elif i in user_sub_author_not:
  53. total_C += 1
  54. else:
  55. total_D += 1
  56. total_E = 0
  57. total_F = 0
  58. total_G = 0
  59. total_H = 0
  60. for pk in list_media_id:
  61. api_obj.getMediaComments(str(pk))
  62. list_comments_users = [i['user']['username'] for i in api_obj.LastJson['comments'] if
  63. i['user']['username'] != api_obj.username]
  64. for i in list_comments_users:
  65. if i in bidirectional_connection:
  66. total_E += 1
  67. elif i in author_sub_user_not:
  68. total_F += 1
  69. elif i in user_sub_author_not:
  70. total_G += 1
  71. else:
  72. total_H += 1
  73. A = 1
  74. B = 0.7
  75. C = 0.3
  76. D = 0.1
  77. E = 0.5
  78. F = 0.35
  79. G = 0.15
  80. H = 0.05
  81. try:
  82. res = (
  83. A * total_A +
  84. B * total_B +
  85. C * total_C +
  86. D * total_D +
  87. E * total_E +
  88. F * total_F +
  89. G * total_G +
  90. H * total_H) / n
  91. except ZeroDivisionError:
  92. res = 0
  93. res = res if res >= 5 else 0
  94. return res
Add Comment
Please, Sign In to add comment