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-06-04 03:10:25 +12:00
|
|
|
import json
|
2021-03-21 14:10:06 +13:00
|
|
|
import re
|
2021-02-25 23:40:08 +13:00
|
|
|
from typing import Optional
|
2021-02-07 01:29:13 +13:00
|
|
|
|
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
|
|
|
|
from bdfr.site_downloaders.base_downloader import BaseDownloader
|
2020-05-29 06:42:11 +12:00
|
|
|
|
2021-02-25 22:32:06 +13:00
|
|
|
|
2021-02-07 14:33:19 +13:00
|
|
|
class Imgur(BaseDownloader):
|
2021-02-15 18:12:27 +13:00
|
|
|
def __init__(self, post: Submission):
|
|
|
|
super().__init__(post)
|
2021-02-07 17:46:20 +13:00
|
|
|
self.raw_data = {}
|
|
|
|
|
2021-02-26 21:57:05 +13:00
|
|
|
def find_resources(self, authenticator: Optional[SiteAuthenticator] = None) -> list[Resource]:
|
2021-03-21 14:10:06 +13:00
|
|
|
self.raw_data = self._get_data(self.post.url)
|
2020-05-29 06:42:11 +12:00
|
|
|
|
2021-03-21 14:10:06 +13:00
|
|
|
out = []
|
2023-01-22 11:36:56 +13:00
|
|
|
if "is_album" in self.raw_data:
|
|
|
|
for image in self.raw_data["images"]:
|
|
|
|
if "mp4" in image:
|
|
|
|
out.append(Resource(self.post, image["mp4"], Resource.retry_download(image["mp4"])))
|
|
|
|
else:
|
|
|
|
out.append(Resource(self.post, image["link"], Resource.retry_download(image["link"])))
|
2020-06-04 03:10:25 +12:00
|
|
|
else:
|
2023-01-22 11:36:56 +13:00
|
|
|
if "mp4" in self.raw_data:
|
|
|
|
out.append(Resource(self.post, self.raw_data["mp4"], Resource.retry_download(self.raw_data["mp4"])))
|
|
|
|
else:
|
|
|
|
out.append(Resource(self.post, self.raw_data["link"], Resource.retry_download(self.raw_data["link"])))
|
2021-02-11 12:10:40 +13:00
|
|
|
return out
|
2020-06-02 00:05:02 +12:00
|
|
|
|
2021-03-21 14:10:06 +13:00
|
|
|
@staticmethod
|
|
|
|
def _get_data(link: str) -> dict:
|
2022-09-19 15:27:17 +12:00
|
|
|
try:
|
2023-01-31 08:52:08 +13:00
|
|
|
if link.endswith("/"):
|
|
|
|
link = link.removesuffix("/")
|
2023-01-10 06:48:24 +13:00
|
|
|
if re.search(r".*/(.*?)(gallery/|a/)", link):
|
|
|
|
imgur_id = re.match(r".*/(?:gallery/|a/)(.*?)(?:/.*)?$", link).group(1)
|
2023-01-22 11:36:56 +13:00
|
|
|
link = f"https://api.imgur.com/3/album/{imgur_id}"
|
2023-01-10 06:48:24 +13:00
|
|
|
else:
|
2023-01-10 09:34:14 +13:00
|
|
|
imgur_id = re.match(r".*/(.*?)(?:_d)?(?:\..{0,})?$", link).group(1)
|
2023-01-22 11:36:56 +13:00
|
|
|
link = f"https://api.imgur.com/3/image/{imgur_id}"
|
2022-09-19 15:27:17 +12:00
|
|
|
except AttributeError:
|
2022-12-03 18:11:17 +13:00
|
|
|
raise SiteDownloaderError(f"Could not extract Imgur ID from {link}")
|
2021-03-28 13:15:21 +13:00
|
|
|
|
2023-01-22 11:36:56 +13:00
|
|
|
headers = {
|
|
|
|
"referer": "https://imgur.com/",
|
|
|
|
"origin": "https://imgur.com",
|
|
|
|
"content-type": "application/json",
|
|
|
|
"Authorization": "Client-ID 546c25a59c58ad7",
|
|
|
|
}
|
|
|
|
res = Imgur.retrieve_url(link, headers=headers)
|
2021-04-06 13:04:08 +12:00
|
|
|
|
|
|
|
try:
|
2023-01-22 11:36:56 +13:00
|
|
|
image_dict = json.loads(res.text)
|
2021-04-06 13:04:08 +12:00
|
|
|
except json.JSONDecodeError as e:
|
2023-01-22 11:36:56 +13:00
|
|
|
raise SiteDownloaderError(f"Could not parse received response as JSON: {e}")
|
2021-03-21 14:10:06 +13:00
|
|
|
|
2023-01-22 11:36:56 +13:00
|
|
|
return image_dict["data"]
|