From 275cceeb6a69d6dff40be03bf26d14d044cc3896 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mike=20F=C3=A4hrmann?= Date: Thu, 28 May 2020 02:00:40 +0200 Subject: [PATCH] [redgifs] fix extraction (#724) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit … and prepare for more potential extractors --- gallery_dl/extractor/redgifs.py | 39 ++++++++++++++++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) diff --git a/gallery_dl/extractor/redgifs.py b/gallery_dl/extractor/redgifs.py index 2ca9feb0..8a0cae48 100644 --- a/gallery_dl/extractor/redgifs.py +++ b/gallery_dl/extractor/redgifs.py @@ -9,13 +9,50 @@ """Extractors for https://redgifs.com/""" from .gfycat import GfycatImageExtractor +from ..cache import cache class RedgifsImageExtractor(GfycatImageExtractor): """Extractor for individual images from redgifs.com""" category = "redgifs" - pattern = r"(?:https?://)?redgifs\.com/watch/([A-Za-z]+)" + pattern = r"(?:https?://)?(?:www\.)?redgifs\.com/watch/([A-Za-z]+)" test = ("https://redgifs.com/watch/foolishforkedabyssiniancat", { "pattern": "https://giant.gfycat.com/FoolishForkedAbyssiniancat.mp4", "content": "f6e03f1df9a2ff2a74092f53ee7580d2fb943533", }) + + def _get_info(self, gfycat_id): + api = RedgifsAPI(self) + return api.gfycat(gfycat_id) + + +class RedgifsAPI(): + + def __init__(self, extractor): + self.extractor = extractor + self.headers = {} + + def gfycat(self, gfycat_id): + endpoint = "v1/gfycats/" + gfycat_id + return self._call(endpoint)["gfyItem"] + + @cache(maxage=3600) + def _authenticate_impl(self): + url = "https://weblogin.redgifs.com/oauth/webtoken" + headers = { + "Referer": "https://www.redgifs.com/", + "Origin" : "https://www.redgifs.com", + } + data = { + "access_key": "dBLwVuGn9eq4dtXLs8WSfpjcYFY7bPQe" + "AqGPSFgqeW5B9uzj2cMVhF63pTFF4Rg9", + } + + response = self.extractor.request( + url, method="POST", headers=headers, json=data) + return "Bearer " + response.json()["access_token"] + + def _call(self, endpoint): + self.headers["Authorization"] = self._authenticate_impl() + url = "https://napi.redgifs.com/" + endpoint + return self.extractor.request(url, headers=self.headers).json()