Advertisement
jejes

userinfo.py

Jul 22nd, 2018
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.31 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3.  
  4. import json
  5. import requests
  6. import re
  7.  
  8. class UserInfo:
  9. '''
  10. This class try to take some user info (following, followers, etc.)
  11. '''
  12. user_agent = ("Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 "
  13. "(KHTML, like Gecko) Chrome/48.0.2564.103 Safari/537.36")
  14. url_user_info = "https://www.instagram.com/%s/"
  15. url_list = {
  16. "ink361": {
  17. "main": "http://ink361.com/",
  18. "user": "http://ink361.com/app/users/%s",
  19. "search_name": "https://data.ink361.com/v1/users/search?q=%s",
  20. "search_id": "https://data.ink361.com/v1/users/ig-%s",
  21. "followers": "https://data.ink361.com/v1/users/ig-%s/followed-by",
  22. "following": "https://data.ink361.com/v1/users/ig-%s/follows",
  23. "stat": "http://ink361.com/app/users/ig-%s/%s/stats"
  24. }
  25. }
  26.  
  27. def __init__(self, info_aggregator="ink361"):
  28. self.i_a = info_aggregator
  29. self.hello()
  30.  
  31. def hello(self):
  32. self.s = requests.Session()
  33. self.s.headers.update({'User-Agent': self.user_agent})
  34. return True
  35.  
  36. def get_user_id_by_login(self, user_name):
  37. url_info = self.url_user_info % (user_name)
  38. info = self.s.get(url_info)
  39. json_info = json.loads(re.search('{"activity.+show_app', info.text, re.DOTALL).group(0)+'":""}')
  40. id_user = json_info['entry_data']['ProfilePage'][0]['graphql']['user']['id']
  41. return id_user
  42.  
  43. def search_user(self, user_id=None, user_name=None):
  44. '''
  45. Search user_id or user_name, if you don't have it.
  46. '''
  47. self.user_id = user_id or False
  48. self.user_name = user_name or False
  49.  
  50. if not self.user_id and not self.user_name:
  51. # you have nothing
  52. return False
  53. elif self.user_id:
  54. # you have just id
  55. search_url = self.url_list[self.i_a]["search_id"] % self.user_id
  56. elif self.user_name:
  57. # you have just name
  58. search_url = self.url_list[self.i_a][
  59. "search_name"] % self.user_name
  60. else:
  61. # you have id and name
  62. return True
  63.  
  64. search = self.s.get(search_url)
  65.  
  66. if search.status_code == 200:
  67. r = json.loads(search.text)
  68. if self.user_id:
  69. # you have just id
  70. self.user_name = r["data"]["username"]
  71. else:
  72. for u in r["data"]:
  73. if u["username"] == self.user_name:
  74. t = u["id"].split("-")
  75. self.user_id = t[1]
  76. # you have just name
  77. return True
  78. return False
  79.  
  80. def get_followers(self, limit=-1):
  81. self.followers = None
  82. self.followers = []
  83. if self.user_id:
  84. next_url = self.url_list[self.i_a]["followers"] % self.user_id
  85. while True:
  86. followers = self.s.get(next_url)
  87. r = json.loads(followers.text)
  88. for u in r["data"]:
  89. if limit > 0 or limit < 0:
  90. self.followers.append({
  91. "username": u["username"],
  92. #"profile_picture": u["profile_picture"],
  93. "id": u["id"].split("-")[1],
  94. #"full_name": u["full_name"]
  95. })
  96. limit -= 1
  97. else:
  98. return True
  99. if r["pagination"]["next_url"]:
  100. # have more data
  101. next_url = r["pagination"]["next_url"]
  102. else:
  103. # end of data
  104. return True
  105. return False
  106.  
  107. def get_following(self, limit=-1):
  108. self.following = None
  109. self.following = []
  110. if self.user_id:
  111. next_url = self.url_list[self.i_a]["following"] % self.user_id
  112. while True:
  113. following = self.s.get(next_url)
  114. r = json.loads(following.text)
  115. for u in r["data"]:
  116. if limit > 0 or limit < 0:
  117. self.following.append({
  118. "username": u["username"],
  119. #"profile_picture": u["profile_picture"],
  120. "id": u["id"].split("-")[1],
  121. #"full_name": u["full_name"]
  122. })
  123. limit -= 1
  124. else:
  125. return True
  126. if r["pagination"]["next_url"]:
  127. # have more data
  128. next_url = r["pagination"]["next_url"]
  129. else:
  130. # end of data
  131. return True
  132. return False
  133.  
  134. def get_stat(self, limit):
  135. # todo
  136. return False
  137.  
  138.  
  139. '''
  140. # example
  141. ui = UserInfo()
  142. # search by user_name:
  143. ui.search_user(user_name="danbilzerian")
  144. # or if you know user_id ui.search_user(user_id="50417061")
  145. print(ui.user_name)
  146. print(ui.user_id)
  147.  
  148. # get following list with no limit
  149. ui.get_following()
  150. print(ui.following)
  151.  
  152. # get followers list with limit 10
  153. ui.get_followers(limit=10)
  154. print(ui.followers)
  155. '''
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement