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

* [Instagram] Add support for user's saved collection

* [Instagram] Run formatter

* [Instagram] Simplify collection_id retrieval and add metadata

* [Instagram] Fix bug when params is not passed to _pagination_api
This commit is contained in:
Chew Shee Yang
2022-07-27 16:49:45 +08:00
committed by GitHub
parent 5b63df46c0
commit 977d53b640
2 changed files with 27 additions and 3 deletions

View File

@@ -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>

View File

@@ -398,7 +398,7 @@ class InstagramExtractor(Extractor):
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={}):
while True:
data = self._request_api(endpoint, params=params)
yield from data["items"]
@@ -509,7 +509,7 @@ class InstagramChannelExtractor(InstagramExtractor):
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):
@@ -518,6 +518,30 @@ class InstagramSavedExtractor(InstagramExtractor):
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"