Skip to content

Commit

Permalink
[gfycat] cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
mikf committed May 29, 2022
1 parent a80ba17 commit 415c208
Showing 1 changed file with 5 additions and 25 deletions.
30 changes: 5 additions & 25 deletions gallery_dl/extractor/gfycat.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,16 +72,13 @@ def gfycats(self):
class GfycatUserExtractor(GfycatExtractor):
"""Extractor for gfycat user profiles"""
subcategory = "user"
directory_fmt = ("{category}", "{username|userName}")
directory_fmt = ("{category}", "{username}")
pattern = r"(?:https?://)?gfycat\.com/@([^/?#]+)/?(?:$|\?|#)"
test = ("https://gfycat.com/@gretta", {
"pattern": r"https://giant\.gfycat\.com/[A-Za-z]+\.mp4",
"count": ">= 100",
})

def metadata(self):
return {"userName": self.key}

def gfycats(self):
return GfycatAPI(self).user(self.key)

Expand Down Expand Up @@ -222,7 +219,6 @@ class GfycatAPI():

def __init__(self, extractor):
self.extractor = extractor
self.headers = {}

def gfycat(self, gfycat_id):
endpoint = "/v1/gfycats/" + gfycat_id
Expand All @@ -242,7 +238,7 @@ def collection(self, user, collection):
def collections(self, user):
endpoint = "/v1/users/{}/collections".format(user)
params = {"count": 100}
return self._pagination_collections(endpoint, params)
return self._pagination(endpoint, params, "gfyCollections")

def search(self, query):
endpoint = "/v1/gfycats/search"
Expand All @@ -251,28 +247,12 @@ def search(self, query):

def _call(self, endpoint, params=None):
url = self.API_ROOT + endpoint
return self.extractor.request(
url, params=params, headers=self.headers).json()

def _pagination(self, endpoint, params):
while True:
data = self._call(endpoint, params)
gfycats = data["gfycats"]

for gfycat in gfycats:
if "gfyName" not in gfycat:
gfycat.update(self.gfycat(gfycat["gfyId"]))
yield gfycat

if "found" not in data and len(gfycats) < params["count"] or \
not data["gfycats"]:
return
params["cursor"] = data["cursor"]
return self.extractor.request(url, params=params).json()

def _pagination_collections(self, endpoint, params):
def _pagination(self, endpoint, params, key="gfycats"):
while True:
data = self._call(endpoint, params)
yield from data["gfyCollections"]
yield from data[key]

if not data["cursor"]:
return
Expand Down

0 comments on commit 415c208

Please sign in to comment.