2021-02-07 17:46:20 +13:00
|
|
|
#!/usr/bin/env python3
|
2022-12-20 12:32:37 +13:00
|
|
|
# -*- coding: utf-8 -*-
|
2021-02-07 17:46:20 +13:00
|
|
|
|
2020-05-29 06:42:11 +12:00
|
|
|
import json
|
2021-03-20 01:28:41 +13:00
|
|
|
import re
|
2021-02-25 23:40:08 +13:00
|
|
|
from typing import Optional
|
2021-02-06 21:35:50 +13:00
|
|
|
|
2022-12-03 18:11:17 +13:00
|
|
|
import requests
|
2021-02-11 12:10:40 +13:00
|
|
|
from praw.models import Submission
|
2020-05-29 06:42:11 +12:00
|
|
|
|
2021-04-23 23:06:16 +12:00
|
|
|
from bdfr.exceptions import SiteDownloaderError
|
2021-04-12 19:58:32 +12:00
|
|
|
from bdfr.resource import Resource
|
|
|
|
from bdfr.site_authenticator import SiteAuthenticator
|
2021-04-28 20:50:18 +12:00
|
|
|
from bdfr.site_downloaders.base_downloader import BaseDownloader
|
2021-02-25 22:32:06 +13:00
|
|
|
|
2020-05-29 06:42:11 +12:00
|
|
|
|
2021-04-28 20:50:18 +12:00
|
|
|
class Redgifs(BaseDownloader):
|
2021-02-15 18:12:27 +13:00
|
|
|
def __init__(self, post: Submission):
|
|
|
|
super().__init__(post)
|
2021-02-25 22:32:06 +13:00
|
|
|
|
2021-02-26 21:57:05 +13:00
|
|
|
def find_resources(self, authenticator: Optional[SiteAuthenticator] = None) -> list[Resource]:
|
2022-02-18 15:04:37 +13:00
|
|
|
media_urls = self._get_link(self.post.url)
|
2022-02-20 18:48:02 +13:00
|
|
|
return [Resource(self.post, m, Resource.retry_download(m), None) for m in media_urls]
|
2020-05-29 06:42:11 +12:00
|
|
|
|
2021-02-25 22:32:06 +13:00
|
|
|
@staticmethod
|
2022-12-25 14:52:45 +13:00
|
|
|
def _get_id(url: str) -> str:
|
2021-04-06 13:04:08 +12:00
|
|
|
try:
|
2022-12-20 05:02:06 +13:00
|
|
|
if url.endswith("/"):
|
|
|
|
url = url.removesuffix("/")
|
2023-01-25 22:43:06 +13:00
|
|
|
redgif_id = re.match(r".*/(.*?)(?:#.*|\?.*|\..{0,})?$", url).group(1).lower()
|
2022-12-27 14:25:20 +13:00
|
|
|
if redgif_id.endswith("-mobile"):
|
|
|
|
redgif_id = redgif_id.removesuffix("-mobile")
|
2021-04-06 13:04:08 +12:00
|
|
|
except AttributeError:
|
2022-12-03 18:11:17 +13:00
|
|
|
raise SiteDownloaderError(f"Could not extract Redgifs ID from {url}")
|
2022-12-25 14:52:45 +13:00
|
|
|
return redgif_id
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def _get_link(url: str) -> set[str]:
|
|
|
|
redgif_id = Redgifs._get_id(url)
|
2021-04-06 13:04:08 +12:00
|
|
|
|
2022-12-03 18:11:17 +13:00
|
|
|
auth_token = json.loads(Redgifs.retrieve_url("https://api.redgifs.com/v2/auth/temporary").text)["token"]
|
2022-11-28 12:07:43 +13:00
|
|
|
if not auth_token:
|
2022-12-03 18:11:17 +13:00
|
|
|
raise SiteDownloaderError("Unable to retrieve Redgifs API token")
|
2022-11-28 12:07:43 +13:00
|
|
|
|
2022-10-25 05:45:26 +13:00
|
|
|
headers = {
|
2022-12-03 18:11:17 +13:00
|
|
|
"referer": "https://www.redgifs.com/",
|
|
|
|
"origin": "https://www.redgifs.com",
|
|
|
|
"content-type": "application/json",
|
|
|
|
"Authorization": f"Bearer {auth_token}",
|
2022-10-25 05:45:26 +13:00
|
|
|
}
|
|
|
|
|
2022-12-03 18:11:17 +13:00
|
|
|
content = Redgifs.retrieve_url(f"https://api.redgifs.com/v2/gifs/{redgif_id}", headers=headers)
|
2020-05-29 06:42:11 +12:00
|
|
|
|
|
|
|
if content is None:
|
2022-12-03 18:11:17 +13:00
|
|
|
raise SiteDownloaderError("Could not read the page source")
|
2021-04-06 13:04:08 +12:00
|
|
|
|
|
|
|
try:
|
2022-02-18 15:04:37 +13:00
|
|
|
response_json = json.loads(content.text)
|
2021-04-06 13:04:08 +12:00
|
|
|
except json.JSONDecodeError as e:
|
2022-12-03 18:11:17 +13:00
|
|
|
raise SiteDownloaderError(f"Received data was not valid JSON: {e}")
|
2020-05-29 06:42:11 +12:00
|
|
|
|
2022-02-18 15:04:37 +13:00
|
|
|
out = set()
|
|
|
|
try:
|
2022-12-03 18:11:17 +13:00
|
|
|
if response_json["gif"]["type"] == 1: # type 1 is a video
|
|
|
|
if requests.get(response_json["gif"]["urls"]["hd"], headers=headers).ok:
|
|
|
|
out.add(response_json["gif"]["urls"]["hd"])
|
2022-11-28 12:07:43 +13:00
|
|
|
else:
|
2022-12-03 18:11:17 +13:00
|
|
|
out.add(response_json["gif"]["urls"]["sd"])
|
|
|
|
elif response_json["gif"]["type"] == 2: # type 2 is an image
|
|
|
|
if response_json["gif"]["gallery"]:
|
2022-02-18 15:04:37 +13:00
|
|
|
content = Redgifs.retrieve_url(
|
2022-12-03 18:11:17 +13:00
|
|
|
f'https://api.redgifs.com/v2/gallery/{response_json["gif"]["gallery"]}'
|
|
|
|
)
|
2022-02-18 15:04:37 +13:00
|
|
|
response_json = json.loads(content.text)
|
2022-12-03 18:11:17 +13:00
|
|
|
out = {p["urls"]["hd"] for p in response_json["gifs"]}
|
2022-02-18 15:04:37 +13:00
|
|
|
else:
|
2022-12-03 18:11:17 +13:00
|
|
|
out.add(response_json["gif"]["urls"]["hd"])
|
2022-02-18 15:04:37 +13:00
|
|
|
else:
|
|
|
|
raise KeyError
|
|
|
|
except (KeyError, AttributeError):
|
2022-12-03 18:11:17 +13:00
|
|
|
raise SiteDownloaderError("Failed to find JSON data in page")
|
2022-02-18 15:04:37 +13:00
|
|
|
|
2022-09-17 12:41:17 +12:00
|
|
|
# Update subdomain if old one is returned
|
2022-12-03 18:11:17 +13:00
|
|
|
out = {re.sub("thumbs2", "thumbs3", link) for link in out}
|
|
|
|
out = {re.sub("thumbs3", "thumbs4", link) for link in out}
|
2021-03-20 01:28:41 +13:00
|
|
|
return out
|