2020-05-10 00:31:42 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
# Copyright 2020 Mike Fährmann
|
|
|
|
#
|
|
|
|
# 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 .gfycat import GfycatImageExtractor
|
2020-05-28 02:00:40 +02:00
|
|
|
from ..cache import cache
|
2020-05-10 00:31:42 +02:00
|
|
|
|
|
|
|
|
|
|
|
class RedgifsImageExtractor(GfycatImageExtractor):
|
|
|
|
"""Extractor for individual images from redgifs.com"""
|
|
|
|
category = "redgifs"
|
2020-05-28 02:00:40 +02:00
|
|
|
pattern = r"(?:https?://)?(?:www\.)?redgifs\.com/watch/([A-Za-z]+)"
|
2020-05-10 00:31:42 +02:00
|
|
|
test = ("https://redgifs.com/watch/foolishforkedabyssiniancat", {
|
2020-05-31 17:42:07 +02:00
|
|
|
"pattern": r"https://\w+.redgifs.com/FoolishForkedAbyssiniancat.mp4",
|
2020-05-10 00:31:42 +02:00
|
|
|
"content": "f6e03f1df9a2ff2a74092f53ee7580d2fb943533",
|
|
|
|
})
|
2020-05-28 02:00:40 +02:00
|
|
|
|
|
|
|
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()
|