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

Conversation

sheeyang
Copy link
Contributor

This adds support for user's saved collection medias, previously it would just download the user's entire saved medias.

@sheeyang sheeyang force-pushed the instagram_collection_support branch from 0161fa6 to 5e3b5da Compare July 18, 2022 17:59
@mikf
Copy link
Owner

mikf commented Jul 19, 2022

Thanks for the PR.

I seem to be unable to push any changes regarding this PR to your branch, so please apply this patch yourself:

diff --git a/gallery_dl/extractor/instagram.py b/gallery_dl/extractor/instagram.py
index f6f76eee..8f179113 100644
--- a/gallery_dl/extractor/instagram.py
+++ b/gallery_dl/extractor/instagram.py
@@ -526,11 +526,19 @@ class InstagramCollectionExtractor(InstagramExtractor):
         "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):
-        collection_id = self.url.split(
-            "/")[-2] if self.url[-1] == "/" else self.url.split("/")[-1]
-        endpoint = "/v1/feed/collection/{}/posts/".format(collection_id)
-        for item in self._pagination_api(endpoint, {}):
+        endpoint = "/v1/feed/collection/{}/posts/".format(self.collection_id)
+        for item in self._pagination_api(endpoint):
             yield item["media"]

It simplifies the 'collection_id' retrieval and adds some useful metadata fields. You should also run make (or scripts/supportedsites.py) to updated the list of supported sites.

@sheeyang
Copy link
Contributor Author

Thank you, I applied the patch and gave you write access to the branch.

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

@mikf mikf merged commit 977d53b into mikf:master Jul 27, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants