Advertisement
Guest User

Untitled

a guest
Sep 20th, 2016
486
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.12 KB | None | 0 0
  1. import requests
  2. import hmac
  3. import random
  4. import uuid
  5. import urllib
  6. import json
  7. import hashlib
  8. import time
  9.  
  10. try:
  11. # python 2
  12. urllib_quote_plus = urllib.quote
  13. except:
  14. # python 3
  15. urllib_quote_plus = urllib.parse.quote_plus
  16.  
  17.  
  18. def _generate_signature(data):
  19. return hmac.new('012a54f51c49aa8c5c322416ab1410909add32c966bbaa0fe3dc58ac43fd7ede'.encode('utf-8'), data.encode('utf-8'), hashlib.sha256).hexdigest()
  20.  
  21.  
  22. def _generate_user_agent():
  23. resolutions = ['720x1280', '320x480', '480x800', '1024x768', '1280x720', '768x1024', '480x320']
  24. versions = ['GT-N7000', 'SM-N9000', 'GT-I9220', 'GT-I9100']
  25. dpis = ['120', '160', '320', '240']
  26.  
  27. ver = random.choice(versions)
  28. dpi = random.choice(dpis)
  29. res = random.choice(resolutions)
  30.  
  31. return (
  32. 'Instagram 4.{}.{} '
  33. 'Android ({}/{}.{}.{}; {}; {}; samsung; {}; {}; smdkc210; en_US)'
  34. ).format(
  35. random.randint(1, 2),
  36. random.randint(0, 2),
  37. random.randint(10, 11),
  38. random.randint(1, 3),
  39. random.randint(3, 5),
  40. random.randint(0, 5),
  41. dpi,
  42. res,
  43. ver,
  44. ver,
  45. )
  46.  
  47.  
  48. class InstagramSession(object):
  49.  
  50. def __init__(self):
  51. self.guid = str(uuid.uuid1())
  52. self.device_id = 'android-{}'.format(self.guid)
  53. self.session = requests.Session()
  54. self.session.headers.update({'User-Agent': _generate_user_agent()})
  55. def login(self, username, password):
  56.  
  57. headers = {
  58. 'host':'i.instagram.com',
  59. 'Content-Type':'application/x-www-form-urlencoded;charset=UTF-8',
  60. 'Accept':'*/*',
  61. 'Cookie':'mid=Vx_ogwAAAAEsrBWH8Osd2fiuQGTe',
  62. 'Connection':'keep-alive',
  63. 'Proxy-Connection':'keep-alive',
  64. 'X-IG-Capabilities':'3wI=',
  65. 'User-Agent':_generate_user_agent(),
  66. 'Accept-Language':'en-US;q=1',
  67. 'Content-Length':'275',
  68. 'Accept-Encoding':'gzip, deflate'
  69. }
  70. data = json.dumps({
  71. "device_id": self.device_id,
  72. "guid": self.guid,
  73. "username": username,
  74. "password": password,
  75. "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8"
  76. })
  77. print(data)
  78.  
  79. sig = _generate_signature(data)
  80.  
  81. payload = 'signed_body={}.{}&ig_sig_key_version=4'.format(
  82. sig,
  83. urllib_quote_plus(data)
  84. )
  85.  
  86. r = self.session.post("https://i.instagram.com/api/v1/accounts/login/", payload,headers=headers)
  87. r_json = r.json()
  88. print(r_json)
  89.  
  90. if r_json.get('status') != "ok":
  91. return False
  92.  
  93. return True
  94.  
  95. def upload_photo(self, filename):
  96. data = {
  97. "device_timestamp": time.time(),
  98. }
  99. files = {
  100. "photo": open(filename, 'rb'),
  101. }
  102.  
  103. r = self.session.post("https://instagram.com/api/v1/media/upload/", data, files=files)
  104. r_json = r.json()
  105. print(r_json)
  106.  
  107. return r_json.get('media_id')
  108.  
  109. def configure_photo(self, media_id, caption):
  110. data = json.dumps({
  111. "device_id": self.device_id,
  112. "guid": self.guid,
  113. "media_id": media_id,
  114. "caption": caption,
  115. "device_timestamp": time.time(),
  116. "source_type": "5",
  117. "filter_type": "0",
  118. "extra": "{}",
  119. "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8"
  120. })
  121. print(data)
  122.  
  123. sig = _generate_signature(data)
  124.  
  125. payload = 'signed_body={}.{}&ig_sig_key_version=4'.format(
  126. sig,
  127. urllib_quote_plus(data)
  128. )
  129.  
  130. r = self.session.post("https://instagram.com/api/v1/media/configure/", payload)
  131. r_json = r.json()
  132. print(r_json)
  133.  
  134. if r_json.get('status') != "ok":
  135. return False
  136.  
  137. return True
  138.  
  139. if __name__ == "__main__":
  140. username = 'biplov_dahal'
  141. password = '123123123vb'
  142. insta = InstagramSession()
  143. login = insta.login(username, password)
  144. print login
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement