diff options
| author | 2023-03-13 02:07:49 -0400 | |
|---|---|---|
| committer | 2023-03-13 02:07:49 -0400 | |
| commit | 10987f08f8b6c510ba64f4b42d95ba67eec6e5b0 (patch) | |
| tree | 1af82cad9ac859a70cafc976a980280b939cfcc7 /gallery_dl/extractor/redgifs.py | |
| parent | 919f8ba16a7b82ba1099bd25b2c61c7881a05aa2 (diff) | |
New upstream version 1.25.0.upstream/1.25.0
Diffstat (limited to 'gallery_dl/extractor/redgifs.py')
| -rw-r--r-- | gallery_dl/extractor/redgifs.py | 102 |
1 files changed, 90 insertions, 12 deletions
diff --git a/gallery_dl/extractor/redgifs.py b/gallery_dl/extractor/redgifs.py index ad4282c..eaaef7d 100644 --- a/gallery_dl/extractor/redgifs.py +++ b/gallery_dl/extractor/redgifs.py @@ -23,6 +23,7 @@ class RedgifsExtractor(Extractor): def __init__(self, match): Extractor.__init__(self, match) self.key = match.group(1) + self.api = RedgifsAPI(self) formats = self.config("format") if formats is None: @@ -69,30 +70,89 @@ class RedgifsUserExtractor(RedgifsExtractor): """Extractor for redgifs user profiles""" subcategory = "user" directory_fmt = ("{category}", "{userName}") - pattern = r"(?:https?://)?(?:www\.)?redgifs\.com/users/([^/?#]+)" - test = ("https://www.redgifs.com/users/Natalifiction", { - "pattern": r"https://\w+\.redgifs\.com/[A-Za-z]+\.mp4", - "count": ">= 100", - }) + pattern = r"(?:https?://)?(?:\w+\.)?redgifs\.com/users/([^/?#]+)/?$" + test = ( + ("https://www.redgifs.com/users/Natalifiction", { + "pattern": r"https://\w+\.redgifs\.com/[\w-]+\.mp4", + "count": ">= 100", + }), + ("https://v3.redgifs.com/users/lamsinka89", { + "pattern": r"https://\w+\.redgifs\.com/[\w-]+\.(mp4|jpg)", + "count": ">= 100", + }), + ) def metadata(self): return {"userName": self.key} def gifs(self): - return RedgifsAPI(self).user(self.key) + return self.api.user(self.key) + + +class RedgifsCollectionExtractor(RedgifsExtractor): + """Extractor for an individual user collection""" + subcategory = "collection" + directory_fmt = ("{category}", "{userName}", "{folderName}") + archive_fmt = "{folderId}_{id}" + pattern = (r"(?:https?://)?(?:www\.)?redgifs\.com/users" + r"/([^/?#]+)/collections/([^/?#]+)") + test = ( + ("https://www.redgifs.com/users/boombah123/collections/2631326bbd", { + "pattern": r"https://\w+\.redgifs\.com/[\w-]+\.mp4", + "range": "1-20", + "count": 20, + }), + ("https://www.redgifs.com/users/boombah123/collections/9e6f7dd41f", { + "pattern": r"https://\w+\.redgifs\.com/[\w-]+\.mp4", + "range": "1-20", + "count": 20, + }), + ) + + def __init__(self, match): + RedgifsExtractor.__init__(self, match) + self.collection_id = match.group(2) + + def metadata(self): + data = {"userName": self.key} + data.update(self.api.collection_info(self.key, self.collection_id)) + return data + + def gifs(self): + return self.api.collection(self.key, self.collection_id) + + +class RedgifsCollectionsExtractor(RedgifsExtractor): + """Extractor for redgifs user collections""" + subcategory = "collections" + pattern = (r"(?:https?://)?(?:www\.)?redgifs\.com/users" + r"/([^/?#]+)/collections/?$") + test = ("https://www.redgifs.com/users/boombah123/collections", { + "pattern": (r"https://www\.redgifs\.com/users" + r"/boombah123/collections/\w+"), + "count": ">= 3", + }) + + def items(self): + for collection in self.api.collections(self.key): + url = "{}/users/{}/collections/{}".format( + self.root, self.key, collection["folderId"]) + collection["_extractor"] = RedgifsCollectionExtractor + yield Message.Queue, url, collection class RedgifsSearchExtractor(RedgifsExtractor): """Extractor for redgifs search results""" subcategory = "search" directory_fmt = ("{category}", "Search", "{search}") - pattern = r"(?:https?://)?(?:www\.)?redgifs\.com/browse/?\?([^#]+)" + pattern = r"(?:https?://)?(?:\w+\.)?redgifs\.com/browse/?\?([^#]+)" test = ( ("https://www.redgifs.com/browse?tags=JAV", { "pattern": r"https://\w+\.redgifs\.com/[A-Za-z-]+\.(mp4|jpg)", "range": "1-10", "count": 10, }), + ("https://v3.redgifs.com/browse?tags=JAV"), ("https://www.redgifs.com/browse?type=i&verified=y&order=top7"), ) @@ -102,14 +162,14 @@ class RedgifsSearchExtractor(RedgifsExtractor): return {"search": search} def gifs(self): - return RedgifsAPI(self).search(self.params) + return self.api.search(self.params) class RedgifsImageExtractor(RedgifsExtractor): """Extractor for individual gifs from redgifs.com""" subcategory = "image" pattern = (r"(?:https?://)?(?:" - r"(?:www\.)?redgifs\.com/(?:watch|ifr)|" + r"(?:\w+\.)?redgifs\.com/(?:watch|ifr)|" r"(?:www\.)?gifdeliverynetwork\.com|" r"i\.redgifs\.com/i)/([A-Za-z]+)") test = ( @@ -121,13 +181,16 @@ class RedgifsImageExtractor(RedgifsExtractor): ("https://redgifs.com/ifr/FoolishForkedAbyssiniancat"), ("https://i.redgifs.com/i/FoolishForkedAbyssiniancat"), ("https://www.gifdeliverynetwork.com/foolishforkedabyssiniancat"), + ("https://v3.redgifs.com/watch/FoolishForkedAbyssiniancat"), ) def gifs(self): - return (RedgifsAPI(self).gif(self.key),) + return (self.api.gif(self.key),) class RedgifsAPI(): + """https://api.redgifs.com/docs/index.html""" + API_ROOT = "https://api.redgifs.com" def __init__(self, extractor): @@ -149,6 +212,19 @@ class RedgifsAPI(): params = {"order": order} return self._pagination(endpoint, params) + def collection(self, user, collection_id): + endpoint = "/v2/users/{}/collections/{}/gifs".format( + user, collection_id) + return self._pagination(endpoint) + + def collection_info(self, user, collection_id): + endpoint = "/v2/users/{}/collections/{}".format(user, collection_id) + return self._call(endpoint) + + def collections(self, user): + endpoint = "/v2/users/{}/collections".format(user) + return self._pagination(endpoint, key="collections") + def search(self, params): endpoint = "/v2/gifs/search" params["search_text"] = params.pop("tags", None) @@ -161,12 +237,14 @@ class RedgifsAPI(): return self.extractor.request( url, params=params, headers=self.headers).json() - def _pagination(self, endpoint, params): + def _pagination(self, endpoint, params=None, key="gifs"): + if params is None: + params = {} params["page"] = 1 while True: data = self._call(endpoint, params) - yield from data["gifs"] + yield from data[key] if params["page"] >= data["pages"]: return |
