Skip to content

Commit e1ea12e

Browse files
committed
Return type of endpoint calls changed. Will affect clients.
Used to return full response AND unpacked JSON dictionary. Now return ONLY full response when logging in, because it contains the cookies necessary for the login process, but ALL OTHER calls to endpoints return ONLY JSON dict, which contains the information they actually need. This will affect every client who examines a return value.
1 parent 9195eeb commit e1ea12e

File tree

3 files changed

+17
-10
lines changed

3 files changed

+17
-10
lines changed

InstagramAPI/base.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -135,9 +135,9 @@ def _sendrequest(self, endpoint, post=None, login=False, headers=None):
135135
"""
136136
:param endpoint: URL to call
137137
:param post: data to HTTP POST. If None, do a GET call.
138-
:param login: if True, this is a call to login, so no need to check we are logged in.
138+
:param login: if True, this is a call to login, so no need to check we are logged in. Also changes return type.
139139
:param headers: if not None, override default headers
140-
:return: tuple: (full_response, extracted dictionary of JSON part) of the response from Instagram
140+
:return: tuple: full response from Instagram if login else just extracted dictionary of JSON part
141141
142142
TODO: most clients will only need one or the other of the responses. Can we simplify?
143143
@@ -178,10 +178,17 @@ def _sendrequest(self, endpoint, post=None, login=False, headers=None):
178178
response.status_code, response.text)
179179
raise
180180

181+
if login:
182+
# Need full reponse, containing cookies
183+
LOGGER.debug("Instagram responded successfully to special login operation.")
184+
return response
185+
186+
# Otherwise, unpack just the JSON part
187+
181188
json_dict = json.loads(response.text)
182189

183190
LOGGER.debug("Instagram responded successfully: %s", json_dict)
184-
return response, json_dict
191+
return json_dict
185192

186193
@staticmethod
187194
def _iterator_template(func, field, delaybetweencalls=0):
@@ -190,7 +197,7 @@ def _iterator_template(func, field, delaybetweencalls=0):
190197
"""
191198
max_id = None
192199
while True:
193-
_, json_dict = func(max_id=max_id)
200+
json_dict = func(max_id=max_id)
194201
max_id = json_dict.get('next_max_id', None)
195202
for item in json_dict.get(field, []):
196203
yield item

InstagramAPI/endpoints.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,7 @@ def login(self, force=False):
370370
self._session = requests.Session()
371371
# if you need proxy make something like this:
372372
# self.s.proxies = {"https": "http://proxyip:proxyport"}
373-
full_response, _ = self._sendrequest(
373+
full_response = self._sendrequest(
374374
'si/fetch_headers/?challenge_type=signup&guid=' + self.generate_uuid(False), login=True)
375375

376376
data = {
@@ -382,17 +382,17 @@ def login(self, force=False):
382382
'password': self._password,
383383
'login_attempt_count': '0'}
384384

385-
full_response, json_dict = self._sendrequest(
385+
full_response = self._sendrequest(
386386
'accounts/login/',
387387
post=self._generatesignature(json.dumps(data)),
388388
login=True)
389389

390390
self._isloggedin = True
391-
self._loggedinuserid = json_dict["logged_in_user"]["pk"]
391+
self._loggedinuserid = json.loads(full_response.text)["logged_in_user"]["pk"]
392392
self._ranktoken = "%s_%s" % (self._loggedinuserid, self._uuid)
393393
self._csrftoken = full_response.cookies["csrftoken"]
394394

395-
return full_response, json_dict
395+
return full_response
396396

397397
def logout(self):
398398
try:

examples/display_my_user_details.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010

1111
def display():
1212
api = InstagramAPI(credentials.USERNAME, credentials.PASSWORD)
13-
_, login_data = api.login()
14-
_, profile_data = api.get_profile_data()
13+
_ = api.login()
14+
profile_data = api.get_profile_data()
1515
print("You are logged in as %s." % profile_data[u"user"][u"username"])
1616
print("Your full name is %s." % profile_data[u"user"][u"full_name"])
1717
print("Your email address is %s." % profile_data[u"user"][u"email"])

0 commit comments

Comments
 (0)