Skip to content

Commit

Permalink
[instagram] restore 'cursor' functionality (#2991)
Browse files Browse the repository at this point in the history
  • Loading branch information
mikf committed Oct 3, 2022
1 parent b8d268f commit 3e65645
Showing 1 changed file with 23 additions and 8 deletions.
31 changes: 23 additions & 8 deletions gallery_dl/extractor/instagram.py
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,14 @@ def _extract_tagged_users(src, dest):
"username" : user["username"],
"full_name": user["full_name"]})

def _init_cursor(self):
return self.config("cursor") or None

def _update_cursor(self, cursor):
self.log.debug("Cursor: %s", cursor)
self._cursor = cursor
return cursor


class InstagramUserExtractor(InstagramExtractor):
"""Extractor for an Instagram user profile"""
Expand Down Expand Up @@ -741,6 +749,9 @@ def _call(self, endpoint, **kwargs):
def _pagination(self, endpoint, params=None, media=False):
if params is None:
params = {}
extr = self.extractor
params["max_id"] = extr._init_cursor()

while True:
data = self._call(endpoint, params=params)

Expand All @@ -752,9 +763,12 @@ def _pagination(self, endpoint, params=None, media=False):

if not data.get("more_available"):
return
params["max_id"] = data["next_max_id"]
params["max_id"] = extr._update_cursor(data["next_max_id"])

def _pagination_post(self, endpoint, params):
extr = self.extractor
params["max_id"] = extr._init_cursor()

while True:
data = self._call(endpoint, method="POST", data=params)

Expand All @@ -764,18 +778,21 @@ def _pagination_post(self, endpoint, params):
info = data["paging_info"]
if not info.get("more_available"):
return
params["max_id"] = info["max_id"]
params["max_id"] = extr._update_cursor(info["max_id"])

def _pagination_sections(self, endpoint, params):
extr = self.extractor
params["max_id"] = extr._init_cursor()

while True:
info = self._call(endpoint, method="POST", data=params)

yield from info["sections"]

if not info.get("more_available"):
return
params["max_id"] = info["next_max_id"]
params["page"] = info["next_page"]
params["max_id"] = extr._update_cursor(info["next_max_id"])


class InstagramGraphqlAPI():
Expand Down Expand Up @@ -871,9 +888,8 @@ def _call(self, query_hash, variables):

def _pagination(self, query_hash, variables,
key_data="user", key_edge=None):
cursor = self.extractor.config("cursor")
if cursor:
variables["after"] = cursor
extr = self.extractor
variables["after"] = extr._init_cursor()

while True:
data = self._call(query_hash, variables)[key_data]
Expand All @@ -890,8 +906,7 @@ def _pagination(self, query_hash, variables,
raise exception.StopExtraction(
"%s'%s posts are private", self.item, s)

variables["after"] = self._cursor = info["end_cursor"]
self.extractor.log.debug("Cursor: %s", self._cursor)
variables["after"] = extr._update_cursor(info["end_cursor"])


@cache(maxage=360*24*3600, keyarg=1)
Expand Down

0 comments on commit 3e65645

Please sign in to comment.