[gfycat] cleanup
This commit is contained in:
@@ -72,16 +72,13 @@ class GfycatExtractor(Extractor):
|
|||||||
class GfycatUserExtractor(GfycatExtractor):
|
class GfycatUserExtractor(GfycatExtractor):
|
||||||
"""Extractor for gfycat user profiles"""
|
"""Extractor for gfycat user profiles"""
|
||||||
subcategory = "user"
|
subcategory = "user"
|
||||||
directory_fmt = ("{category}", "{username|userName}")
|
directory_fmt = ("{category}", "{username}")
|
||||||
pattern = r"(?:https?://)?gfycat\.com/@([^/?#]+)/?(?:$|\?|#)"
|
pattern = r"(?:https?://)?gfycat\.com/@([^/?#]+)/?(?:$|\?|#)"
|
||||||
test = ("https://gfycat.com/@gretta", {
|
test = ("https://gfycat.com/@gretta", {
|
||||||
"pattern": r"https://giant\.gfycat\.com/[A-Za-z]+\.mp4",
|
"pattern": r"https://giant\.gfycat\.com/[A-Za-z]+\.mp4",
|
||||||
"count": ">= 100",
|
"count": ">= 100",
|
||||||
})
|
})
|
||||||
|
|
||||||
def metadata(self):
|
|
||||||
return {"userName": self.key}
|
|
||||||
|
|
||||||
def gfycats(self):
|
def gfycats(self):
|
||||||
return GfycatAPI(self).user(self.key)
|
return GfycatAPI(self).user(self.key)
|
||||||
|
|
||||||
@@ -222,7 +219,6 @@ class GfycatAPI():
|
|||||||
|
|
||||||
def __init__(self, extractor):
|
def __init__(self, extractor):
|
||||||
self.extractor = extractor
|
self.extractor = extractor
|
||||||
self.headers = {}
|
|
||||||
|
|
||||||
def gfycat(self, gfycat_id):
|
def gfycat(self, gfycat_id):
|
||||||
endpoint = "/v1/gfycats/" + gfycat_id
|
endpoint = "/v1/gfycats/" + gfycat_id
|
||||||
@@ -242,7 +238,7 @@ class GfycatAPI():
|
|||||||
def collections(self, user):
|
def collections(self, user):
|
||||||
endpoint = "/v1/users/{}/collections".format(user)
|
endpoint = "/v1/users/{}/collections".format(user)
|
||||||
params = {"count": 100}
|
params = {"count": 100}
|
||||||
return self._pagination_collections(endpoint, params)
|
return self._pagination(endpoint, params, "gfyCollections")
|
||||||
|
|
||||||
def search(self, query):
|
def search(self, query):
|
||||||
endpoint = "/v1/gfycats/search"
|
endpoint = "/v1/gfycats/search"
|
||||||
@@ -251,28 +247,12 @@ class GfycatAPI():
|
|||||||
|
|
||||||
def _call(self, endpoint, params=None):
|
def _call(self, endpoint, params=None):
|
||||||
url = self.API_ROOT + endpoint
|
url = self.API_ROOT + endpoint
|
||||||
return self.extractor.request(
|
return self.extractor.request(url, params=params).json()
|
||||||
url, params=params, headers=self.headers).json()
|
|
||||||
|
|
||||||
def _pagination(self, endpoint, params):
|
def _pagination(self, endpoint, params, key="gfycats"):
|
||||||
while True:
|
while True:
|
||||||
data = self._call(endpoint, params)
|
data = self._call(endpoint, params)
|
||||||
gfycats = data["gfycats"]
|
yield from data[key]
|
||||||
|
|
||||||
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"]
|
|
||||||
|
|
||||||
def _pagination_collections(self, endpoint, params):
|
|
||||||
while True:
|
|
||||||
data = self._call(endpoint, params)
|
|
||||||
yield from data["gfyCollections"]
|
|
||||||
|
|
||||||
if not data["cursor"]:
|
if not data["cursor"]:
|
||||||
return
|
return
|
||||||
|
|||||||
Reference in New Issue
Block a user