Skip to content

Commit b1ef9b5

Browse files
committed
Fix get_liked_media bug (again?)
1 parent 17adc07 commit b1ef9b5

File tree

1 file changed

+27
-15
lines changed

1 file changed

+27
-15
lines changed

InstagramAPI/endpoints.py

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,8 @@ def explore(self):
246246
return self._sendrequest('discover/explore/')
247247

248248
def expose(self):
249+
# TODO: This might be deprecated.
250+
# http://instagram-private-api.readthedocs.io/en/latest/_modules/instagram_private_api/endpoints/misc.html
249251
data = json.dumps({
250252
'_uuid': self._uuid,
251253
'_uid': self._loggedinuserid,
@@ -282,8 +284,9 @@ def get_hashtag_feed(self, hashtagString, maxid=''):
282284
'feed/tag/' + hashtagString + '/?max_id=' + str(maxid) +
283285
'&rank_token=' + self._ranktoken + '&ranked_content=true&')
284286

285-
def get_liked_media(self, maxid=''):
286-
return self._sendrequest('feed/liked/?max_id=' + str(maxid))
287+
def get_liked_media(self, maxid=None):
288+
max_id_param = '?max_id=' + str(maxid) if maxid else ""
289+
return self._sendrequest('feed/liked/' + max_id_param)
287290

288291
def get_location_feed(self, locationId, maxid=''):
289292
return self._sendrequest(
@@ -516,17 +519,20 @@ def unlike(self, mediaId):
516519
def upload_photo(self, photo, caption=None, upload_id=None):
517520
if upload_id is None:
518521
upload_id = str(int(time.time() * 1000))
519-
data = {
520-
'upload_id': upload_id,
521-
'_uuid': self._uuid,
522-
'_csrftoken': self._csrftoken,
523-
'image_compression': '{"lib_name":"jt","lib_version":"1.3.0","quality":"87"}',
524-
'photo': (
525-
'pending_media_%s.jpg' % upload_id,
526-
open(photo, 'rb'),
527-
'application/octet-stream',
528-
{'Content-Transfer-Encoding': 'binary'})
529-
}
522+
523+
with open(photo, 'rb') as photo_file:
524+
data = {
525+
'upload_id': upload_id,
526+
'_uuid': self._uuid,
527+
'_csrftoken': self._csrftoken,
528+
'image_compression': '{"lib_name":"jt","lib_version":"1.3.0","quality":"87"}',
529+
'photo': (
530+
'pending_media_%s.jpg' % upload_id,
531+
photo_file.read(),
532+
'application/octet-stream',
533+
{'Content-Transfer-Encoding': 'binary'})
534+
}
535+
530536
m = MultipartEncoder(data, boundary=self._uuid)
531537
headers = {
532538
'X-IG-Capabilities': '3Q4=',
@@ -537,6 +543,8 @@ def upload_photo(self, photo, caption=None, upload_id=None):
537543
'Content-type': m.content_type,
538544
'Connection': 'close',
539545
'User-Agent': self.USER_AGENT}
546+
self._sendrequest("upload/photo/", post=m.to_string(), headers=headers)
547+
540548
self._session.post(self.API_URL + "upload/photo/",
541549
data=m.to_string(), headers=headers)
542550
if self.configure(upload_id, photo, caption):
@@ -571,8 +579,8 @@ def upload_video(self, video, thumbnail, caption=None, upload_id=None):
571579
upload_url = body['video_upload_urls'][3]['url']
572580
upload_job = body['video_upload_urls'][3]['job']
573581

574-
video_data = open(video, 'rb').read()
575-
# solve issue #85 TypeError: slice indices must be integers or None or have an __index__ method
582+
with open(video, 'rb') as videofile:
583+
videoData = videofile.read()
576584
request_size = int(math.floor(len(video_data) / 4))
577585
last_request_extra = (len(video_data) - (request_size * 3))
578586

@@ -602,13 +610,17 @@ def upload_video(self, video, thumbnail, caption=None, upload_id=None):
602610

603611
self._session.headers.update(
604612
{'Content-Length': str(end - start), 'Content-Range': content_range, })
613+
LOGGER.info("Starting to upload %d bytes of video data", len(video_data))
605614
response = self._session.post(
606615
upload_url, data=video_data[start:start + length])
607616
self._session.headers = headers
608617

609618
if response.status_code == 200:
610619
if self.configure_video(upload_id, video, thumbnail, caption):
620+
LOGGER.info("Video configuration complete. Exposing.")
611621
self.expose()
622+
LOGGER.info("Video upload complete.")
623+
612624
return False
613625

614626
def user_friendship(self, userId):

0 commit comments

Comments
 (0)