-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathInsta.py
More file actions
142 lines (120 loc) · 4.3 KB
/
Insta.py
File metadata and controls
142 lines (120 loc) · 4.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import requests
import hmac
import json
import hashlib
import uuid
class Insta(object):
STATUS_FAILED = -1
STATUS_OK = 0
def __init__(self, user, pw):
self.user = user
self.pw = pw
self.genGuid()
self.genDeviceId()
self.logged_in = False
self.user_info = []
self.session = requests.Session()
def genDeviceId(self):
self.device_id = "android-%s" % self.guid.replace("-", "")[0:16]
pass
def genGuid(self):
self.guid = str(uuid.uuid1())
pass
def get(self, url):
"""
Issues a GET request to a specified endpoint within the Instagram
private API.
Arguments:
url: path of endpoint, domain name removed (already prepended)
Returns a requests.request object.
"""
if self.logged_in:
headers = {
"user-agent": self.getUserAgent()
}
return self.session.get(
"https://instagram.com/api/v1/" + url,
headers=headers, verify=False
)
else:
print "You must be logged in before issuing requests"
return -1
def getUserAgent(self):
return "Instagram 6.21.2 Android (19/4.4.2; 480dpi; \
1152x1920; Meizu; MX4; mx4; mt6595; en_US)"
def getInfo(self,key):
"""
Returns the key associated with the user's information
"username" - Username of the user
"profile_pic_url" - URL of user's profile profile_picture
"full_name" - Full name of the user
"fbuid" - Facebook ID of user
"is_private" - 'true' if user's profile is private,
'false' if public
"""
try:
return self.user_info[key]
except KeyError:
return "null"
def login(self):
"""
Logs in and verifies the users supplied credentials when creating
an instance of Insta.
If login is successful, we will setup a dictionary that
contain the following keys:
"username" - Username of the user
"profile_pic_url" - URL of user's profile profile_picture
"full_name" - Full name of the user
"fbuid" - Facebook ID of user
"is_private" - 'true' if user's profile is private,
'false' if public
*Additional keys may be contained, but these are the few that are guaranteed
to exist within the dictionary
"""
data = json.dumps(
{
"device_id": self.device_id,
"guid": self.guid,
"username": self.user,
"password": self.pw,
"Content-Type": "application/x-www-form-urlencoded;"
"charset=UTF-8"
}
)
responseJson = json.loads(
self.post("accounts/login/", {
"signed_body": self.signMessage(data) + "." + data,
"ig_sig_key_version": 6
}).text
)
if responseJson['status'] != 'ok':
print "Failed to login...\nStatus: %s\nMessage: %s" % (
responseJson['status'], responseJson['message']
)
return self.STATUS_FAILED
else:
self.user_info = responseJson['logged_in_user']
return self.STATUS_OK
def post(self, url, contents):
"""
Issues a POST request to the specified endpoint
within the Instagram private API.
Arguments:
url: path of the specified endpoint, without
domain name (it is already prepended)
contents: a dictionary containing the POST data
Returns a requests.request object.
"""
headers = {
"user-agent": self.getUserAgent()
}
return self.session.post("https://instagram.com/api/v1/" + url,
data=contents,
headers=headers,
verify=False
)
def signMessage(self, data):
return hmac.new(
"25eace5393646842f0d0c3fb2ac7d3cfa15c052436ee86b5406a8433f54d24a5",
data,
hashlib.sha256).hexdigest()