Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Instagram] Add support for user's saved collection #2769

Merged
merged 4 commits into from
Jul 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/supportedsites.md
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,7 @@ Consider all sites to be NSFW unless otherwise known.
<tr>
<td>Instagram</td>
<td>https://www.instagram.com/</td>
<td>Channels, Highlights, Posts, Reels, Saved Posts, Stories, Tag Searches, Tagged Posts, User Profiles</td>
<td>Channels, Collections, Highlights, Posts, Reels, Saved Posts, Stories, Tag Searches, Tagged Posts, User Profiles</td>
<td>Supported</td>
</tr>
<tr>
Expand Down
28 changes: 26 additions & 2 deletions gallery_dl/extractor/instagram.py
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,7 @@ def _pagination_graphql(self, query_hash, variables):
variables["after"] = self._cursor = info["end_cursor"]
self.log.debug("Cursor: %s", self._cursor)

def _pagination_api(self, endpoint, params=None):
def _pagination_api(self, endpoint, params={}):
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I should mention I changed the None to {} here to prevent this error when the collection has more available

[instagram][error] An unexpected error occurred: TypeError - 'NoneType' object does not support item assignment. Please run gallery-dl again with the --verbose flag, copy its output and report this issue on https://github.com/mikf/gallery-dl/issues .
[instagram][debug]
Traceback (most recent call last):
  File "/mnt/d/REPO/PUBLIC/gallery-dl/gallery_dl/job.py", line 77, in run
    for msg in extractor:
  File "/mnt/d/REPO/PUBLIC/gallery-dl/gallery_dl/extractor/instagram.py", line 49, in items
    for post in self.posts():
  File "/mnt/d/REPO/PUBLIC/gallery-dl/gallery_dl/extractor/instagram.py", line 541, in posts
    for item in self._pagination_api(endpoint):
  File "/mnt/d/REPO/PUBLIC/gallery-dl/gallery_dl/extractor/instagram.py", line 408, in _pagination_api
    params["max_id"] = data["next_max_id"]
TypeError: 'NoneType' object does not support item assignment

while True:
data = self._request_api(endpoint, params=params)
yield from data["items"]
Expand Down Expand Up @@ -509,7 +509,7 @@ def posts(self):
class InstagramSavedExtractor(InstagramExtractor):
"""Extractor for ProfilePage saved media"""
subcategory = "saved"
pattern = USER_PATTERN + r"/saved"
pattern = USER_PATTERN + r"/saved/?$"
test = ("https://www.instagram.com/instagram/saved/",)

def posts(self):
Expand All @@ -518,6 +518,30 @@ def posts(self):
return self._pagination_graphql(query_hash, variables)


class InstagramCollectionExtractor(InstagramExtractor):
"""Extractor for ProfilePage saved collection media"""
subcategory = "collection"
pattern = USER_PATTERN + r"/saved/([^/?#]+)/([^/?#]+)"
test = (
"https://www.instagram.com/instagram/saved/collection_name/123456789/",
)

def __init__(self, match):
InstagramExtractor.__init__(self, match)
self.user, self.collection_name, self.collection_id = match.groups()

def metadata(self):
return {
"collection_id" : self.collection_id,
"collection_name": text.unescape(self.collection_name),
}

def posts(self):
endpoint = "/v1/feed/collection/{}/posts/".format(self.collection_id)
for item in self._pagination_api(endpoint):
yield item["media"]


class InstagramTagExtractor(InstagramExtractor):
"""Extractor for TagPage"""
subcategory = "tag"
Expand Down