View difference between Paste ID: 8vm7tUSN and dUKfJaKa
SHOW: | | - or go back to the newest paste.
1
import random
2
import json
3
import hmac
4
import hashlib
5
import urllib
6
import urllib2
7
import md5
8
import string
9
10
class InstagramSession:
11
	login_session = ""
12
	def __init__(self, key, user_agent="Instagram 7.1.1 Android (21/1.3.1; 480; 1920x1080; samsung; SM-N9005; SM-N9005; smdkc210; en_US)"):
13
		self.username = ""
14
		self.password = ""
15
		self.email = ""
16
		self.name = ""
17
		self.key = key
18
		self.user_agent = user_agent
19
	def set_ssl_proxy(self, proxy):
20
		urllib2.install_opener(
21
			urllib2.build_opener(
22
				urllib2.ProxyHandler({'https': proxy})
23
			)
24
		)
25
	def _find_string_between(self, s, first, last ):
26
		try:
27
			start = s.index( first ) + len( first )
28
			end = s.index( last, start )
29
			return s[start:end]
30
		except ValueError:
31
			return ""
32
	def _sign_data(self, data):
33
		data = json.dumps(data, separators=(',',':'))
34
		signed_body = hmac.new(self.key, msg=data, digestmod=hashlib.sha256).hexdigest()
35
		return "signed_body=" + signed_body + "." + urllib.quote(data) + "&ig_sig_key_version=4"
36
	def _generate_guid(self):
37
		return "%04x%04x-%04x-%04x-%04x-%04x%04x%04x" % (random.randint(0, 65535), random.randint(0, 65535), random.randint(0, 65535), random.randint(16384, 20479), random.randint(32768, 49151), random.randint(0, 65535), random.randint(0, 65535), random.randint(0, 65535))
38
	def _get_request(self, request_path):
39
		headers = {"Connection":"Keep-Alive", "X-IG-Connection-Type":"WIFI", "X-IG-Capabilities":"BQ==", "Accept-Encoding":"gzip", "Accept-Language":"en-US", "User-Agent":self.user_agent}
40
		request = urllib2.Request("http://i.instagram.com" + request_path)
41
		response = urllib2.urlopen(request)
42
		return response
43
	def _post_request(self, request_path, request_data, cookies):
44
		headers = {"Host": "i.instagram.com", "Connection":"Keep-Alive", "X-IG-Connection-Type":"WIFI", "X-IG-Capabilities":"BQ==", "Accept-Encoding":"gzip", "Accept-Language":"en-US", "Cookie2":"$Version=1", "Content-Type":"application/x-www-form-urlencoded; charset=UTF-8", "User-Agent":self.user_agent, "Cookie":cookies}
45
		request = urllib2.Request("https://i.instagram.com" + request_path, data=request_data, headers=headers)
46
		request.add_header('Content-Length', len(request_data))
47
		request.add_header('Host', "i.instagram.com")
48
		response = urllib2.urlopen(request)
49
		return response
50
	def chunkstring(self, string, length):
51
		return (string[0+i:length+i] for i in range(0, len(string), length))
52
	def login(self, username, password):
53
		self.guid = self._generate_guid()
54
		self.username = username
55
		self.password = password
56
		response = self._get_request("/api/v1/si/fetch_headers/?challenge_type=signup&guid=" + self.guid)
57
		cookies = response.info()["Set-Cookie"]
58
		csrftoken = self._find_string_between(cookies, "csrftoken=", ";")
59
		mid = self._find_string_between(cookies, "mid=", ";")
60
		device_id = "android-" + md5.new(str(random.random())).hexdigest()[0:16]
61
		post_request_data = self._sign_data({"username":self.username, "password":self.password, "device_id": device_id, "guid":self.guid, "_csrftoken":csrftoken})
62
		print post_request_data
63
		response = self._post_request("/api/v1/accounts/login/", post_request_data, "csrftoken=" + csrftoken + "; mid=" + mid)
64
	def signup(self, username, password, email, name):
65
		self.guid = self._generate_guid()
66
		self.username = username
67
		self.password = password
68
		self.email = email
69
		self.name = name
70
		response = self._get_request("/api/v1/si/fetch_headers/?challenge_type=signup&guid=" + self.guid)
71
		cookies = response.info()["Set-Cookie"]
72
		csrftoken = self._find_string_between(cookies, "csrftoken=", ";")
73
		mid = self._find_string_between(cookies, "mid=", ";")
74
		post_request_data = self._sign_data({"email":self.email, "_csrftoken":csrftoken})
75
		response = self._post_request("/api/v1/users/check_email/", post_request_data, "csrftoken=" + csrftoken + "; mid=" + mid)
76
		device_id = "android-" + list(self.chunkstring(str(md5.new(str(random.randint(1000, 9999))).hexdigest()), 16))[1]
77
		post_request_data = self._sign_data({"email":self.email, "username":self.username, "password":self.password, "device_id":device_id, "guid":self.guid,"first_name":name})
78
		response = self._post_request("/api/v1/accounts/create/", post_request_data, "csrftoken=" + csrftoken + "; mid=" + mid)
79
		print device_id
80
		print response.info()
81
		print response.read()
82
83
84
session = InstagramSession("3f0a7d75e094c7385e3dbaa026877f2e067cbd1a4dbcf3867748f6b26f257117", "Instagram 7.1.1 Android (21/5.0.2; 480dpi; 1080x1776; LGE/Google; Nexus 5; hammerhead; hammerhead; en_US)")
85
session.signup("jsa230am", "blackman23", "[email protected]", "Dong Peterson")