1
0
mirror of https://github.com/mikf/gallery-dl.git synced 2025-02-01 12:01:41 +01:00

276 lines
8.8 KiB
Python
Raw Normal View History

2020-05-10 00:31:42 +02:00
# -*- coding: utf-8 -*-
2023-05-29 23:03:45 +02:00
# Copyright 2020-2023 Mike Fährmann
2020-05-10 00:31:42 +02:00
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as
# published by the Free Software Foundation.
"""Extractors for https://redgifs.com/"""
from .common import Extractor, Message
from .. import text
from ..cache import memcache
2020-05-10 00:31:42 +02:00
class RedgifsExtractor(Extractor):
"""Base class for redgifs extractors"""
2020-05-10 00:31:42 +02:00
category = "redgifs"
2023-05-29 23:03:45 +02:00
filename_fmt = \
"{category}_{gallery:?//[:11]}{num:?_/_/>02}{id}.{extension}"
2021-11-04 21:31:20 +01:00
archive_fmt = "{id}"
root = "https://www.redgifs.com"
def __init__(self, match):
Extractor.__init__(self, match)
self.key = match.group(1)
def _init(self):
2023-02-16 20:10:22 +05:30
self.api = RedgifsAPI(self)
formats = self.config("format")
if formats is None:
2021-11-04 21:31:20 +01:00
formats = ("hd", "sd", "gif")
elif isinstance(formats, str):
2021-11-04 21:31:20 +01:00
formats = (formats, "hd", "sd", "gif")
self.formats = formats
def items(self):
metadata = self.metadata()
2023-05-29 23:03:45 +02:00
for gif in self.gifs():
2023-05-29 23:03:45 +02:00
gallery = gif.get("gallery")
if gallery:
gifs = self.api.gallery(gallery)["gifs"]
enum = 1
cnt = len(gifs)
else:
gifs = (gif,)
enum = 0
cnt = 1
gif.update(metadata)
2023-05-29 23:03:45 +02:00
gif["count"] = cnt
gif["date"] = text.parse_timestamp(gif.get("createDate"))
yield Message.Directory, gif
2023-05-29 23:03:45 +02:00
for num, gif in enumerate(gifs, enum):
gif["_fallback"] = formats = self._formats(gif)
url = next(formats, None)
2023-05-29 23:03:45 +02:00
if not url:
self.log.warning(
"Skipping '%s' (format not available)", gif["id"])
continue
2023-05-29 23:03:45 +02:00
gif["num"] = num
gif["count"] = cnt
yield Message.Url, url, gif
def _formats(self, gif):
2021-11-04 21:31:20 +01:00
urls = gif["urls"]
for fmt in self.formats:
url = urls.get(fmt)
if url:
2022-09-01 21:45:36 +02:00
url = url.replace("//thumbs2.", "//thumbs3.", 1)
text.nameext_from_url(url, gif)
yield url
def metadata(self):
return {}
def gifs(self):
return ()
class RedgifsUserExtractor(RedgifsExtractor):
"""Extractor for redgifs user profiles"""
subcategory = "user"
directory_fmt = ("{category}", "{userName}")
pattern = (r"(?:https?://)?(?:\w+\.)?redgifs\.com/users/([^/?#]+)/?"
r"(?:\?([^#]+))?$")
example = "https://www.redgifs.com/users/USER"
def __init__(self, match):
RedgifsExtractor.__init__(self, match)
self.query = match.group(2)
def metadata(self):
return {"userName": self.key}
def gifs(self):
order = text.parse_query(self.query).get("order")
return self.api.user(self.key, order or "new")
2023-02-16 20:10:22 +05:30
class RedgifsCollectionExtractor(RedgifsExtractor):
"""Extractor for an individual user collection"""
subcategory = "collection"
directory_fmt = (
"{category}", "{collection[userName]}", "{collection[folderName]}")
archive_fmt = "{collection[folderId]}_{id}"
2023-02-16 20:10:22 +05:30
pattern = (r"(?:https?://)?(?:www\.)?redgifs\.com/users"
r"/([^/?#]+)/collections/([^/?#]+)")
example = "https://www.redgifs.com/users/USER/collections/ID"
2023-02-16 20:10:22 +05:30
def __init__(self, match):
RedgifsExtractor.__init__(self, match)
self.collection_id = match.group(2)
def metadata(self):
collection = self.api.collection_info(self.key, self.collection_id)
collection["userName"] = self.key
return {"collection": collection}
2023-02-16 20:10:22 +05:30
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/?$")
example = "https://www.redgifs.com/users/USER/collections"
2023-02-16 20:10:22 +05:30
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
2023-07-16 21:22:06 +05:30
class RedgifsNichesExtractor(RedgifsExtractor):
"""Extractor for redgifs niches"""
subcategory = "niches"
2023-10-16 16:51:30 +05:30
pattern = (r"(?:https?://)?(?:www\.)?redgifs\.com/niches/([^/?#]+)/?"
r"(?:\?([^#]+))?$")
example = "https://www.redgifs.com/niches/NAME"
2023-07-16 21:22:06 +05:30
2023-10-16 16:51:30 +05:30
def __init__(self, match):
RedgifsExtractor.__init__(self, match)
self.query = match.group(2)
2023-07-16 21:22:06 +05:30
def gifs(self):
2023-10-16 16:51:30 +05:30
order = text.parse_query(self.query).get("order")
return self.api.niches(self.key, order or "new")
2023-07-16 21:22:06 +05:30
class RedgifsSearchExtractor(RedgifsExtractor):
"""Extractor for redgifs search results"""
subcategory = "search"
directory_fmt = ("{category}", "Search", "{search}")
pattern = (r"(?:https?://)?(?:\w+\.)?redgifs\.com"
r"/(?:gifs/([^/?#]+)|browse)(?:/?\?([^#]+))?")
example = "https://www.redgifs.com/gifs/TAG"
def __init__(self, match):
RedgifsExtractor.__init__(self, match)
self.search, self.query = match.groups()
def metadata(self):
self.params = text.parse_query(self.query)
if self.search:
self.params["tags"] = text.unquote(self.search)
return {"search": (self.params.get("tags") or
self.params.get("order") or
"trending")}
def gifs(self):
2023-02-16 20:10:22 +05:30
return self.api.search(self.params)
class RedgifsImageExtractor(RedgifsExtractor):
"""Extractor for individual gifs from redgifs.com"""
subcategory = "image"
pattern = (r"(?:https?://)?(?:"
2023-01-30 20:06:43 +05:30
r"(?:\w+\.)?redgifs\.com/(?:watch|ifr)|"
r"(?:\w+\.)?gfycat\.com(?:/gifs/detail|/\w+)?|"
r"(?:www\.)?gifdeliverynetwork\.com|"
2024-07-28 02:34:17 -04:00
r"i\.redgifs\.com/i)/([A-Za-z0-9]+)")
example = "https://redgifs.com/watch/ID"
def gifs(self):
2023-02-16 20:10:22 +05:30
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):
self.extractor = extractor
2022-10-13 18:56:11 +02:00
self.headers = {
"authorization" : None,
"content-type" : "application/json",
"x-customheader": extractor.root + "/",
"Origin" : extractor.root,
2022-10-13 18:56:11 +02:00
}
def gif(self, gif_id):
endpoint = "/v2/gifs/" + gif_id.lower()
2021-11-04 21:31:20 +01:00
return self._call(endpoint)["gif"]
2023-05-29 23:03:45 +02:00
def gallery(self, gallery_id):
endpoint = "/v2/gallery/" + gallery_id
return self._call(endpoint)
def user(self, user, order="new"):
2021-11-04 21:31:20 +01:00
endpoint = "/v2/users/{}/search".format(user.lower())
params = {"order": order}
return self._pagination(endpoint, params)
2023-02-16 20:10:22 +05:30
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)
2023-02-16 20:10:22 +05:30
def collections(self, user):
endpoint = "/v2/users/{}/collections".format(user)
return self._pagination(endpoint, key="collections")
2023-10-16 16:51:30 +05:30
def niches(self, niche, order):
2023-07-16 21:22:06 +05:30
endpoint = "/v2/niches/{}/gifs".format(niche)
2023-10-16 16:51:30 +05:30
params = {"count": 30, "order": order}
return self._pagination(endpoint, params)
2023-07-16 21:22:06 +05:30
def search(self, params):
2021-11-04 21:31:20 +01:00
endpoint = "/v2/gifs/search"
params["search_text"] = params.pop("tags", None)
return self._pagination(endpoint, params)
def _call(self, endpoint, params=None):
url = self.API_ROOT + endpoint
self.headers["authorization"] = self._auth()
2022-10-13 18:56:11 +02:00
return self.extractor.request(
url, params=params, headers=self.headers).json()
2023-02-16 20:10:22 +05:30
def _pagination(self, endpoint, params=None, key="gifs"):
if params is None:
params = {}
2021-11-04 21:31:20 +01:00
params["page"] = 1
while True:
data = self._call(endpoint, params)
2023-02-16 20:10:22 +05:30
yield from data[key]
2021-11-04 21:31:20 +01:00
if params["page"] >= data["pages"]:
return
2021-11-04 21:31:20 +01:00
params["page"] += 1
2022-10-13 18:56:11 +02:00
@memcache(maxage=600)
def _auth(self):
# https://github.com/Redgifs/api/wiki/Temporary-tokens
url = self.API_ROOT + "/v2/auth/temporary"
self.headers["authorization"] = None
return "Bearer " + self.extractor.request(
url, headers=self.headers).json()["token"]