[gfycat] add 'collections' extractor (#2629)

This commit is contained in:
Mike Fährmann
2022-05-29 14:39:08 +02:00
parent ff5e10a86d
commit a80ba17ed4
2 changed files with 35 additions and 1 deletions

View File

@@ -87,7 +87,7 @@ class GfycatUserExtractor(GfycatExtractor):
class GfycatCollectionExtractor(GfycatExtractor):
"""Extractor for gfycat collections"""
"""Extractor for a gfycat collection"""
subcategory = "collection"
directory_fmt = ("{category}", "{collection_owner}",
"{collection_name|collection_id}")
@@ -114,6 +114,23 @@ class GfycatCollectionExtractor(GfycatExtractor):
return GfycatAPI(self).collection(self.key, self.collection_id)
class GfycatCollectionsExtractor(GfycatExtractor):
"""Extractor for a gfycat user's collections"""
subcategory = "collections"
pattern = r"(?:https?://)?gfycat\.com/@([^/?#]+)/collections/?(?:$|\?|#)"
test = ("https://gfycat.com/@sannahparker/collections", {
"pattern": GfycatCollectionExtractor.pattern,
"count": ">= 20",
})
def items(self):
for col in GfycatAPI(self).collections(self.key):
url = "https://gfycat.com/@{}/collections/{}/{}".format(
col["userId"], col["folderId"], col["linkText"])
col["_extractor"] = GfycatCollectionExtractor
yield Message.Queue, url, col
class GfycatSearchExtractor(GfycatExtractor):
"""Extractor for gfycat search results"""
subcategory = "search"
@@ -222,6 +239,11 @@ class GfycatAPI():
params = {"count": 100}
return self._pagination(endpoint, params)
def collections(self, user):
endpoint = "/v1/users/{}/collections".format(user)
params = {"count": 100}
return self._pagination_collections(endpoint, params)
def search(self, query):
endpoint = "/v1/gfycats/search"
params = {"search_text": query, "count": 150}
@@ -246,3 +268,12 @@ class GfycatAPI():
not data["gfycats"]:
return
params["cursor"] = data["cursor"]
def _pagination_collections(self, endpoint, params):
while True:
data = self._call(endpoint, params)
yield from data["gfyCollections"]
if not data["cursor"]:
return
params["cursor"] = data["cursor"]