summaryrefslogtreecommitdiffstats
path: root/gallery_dl/extractor/imgur.py
diff options
context:
space:
mode:
authorLibravatarUnit 193 <unit193@unit193.net>2023-05-22 01:03:31 -0400
committerLibravatarUnit 193 <unit193@unit193.net>2023-05-22 01:03:31 -0400
commitbff1d7a3fc1f184002ea6c6f77de675885bda3eb (patch)
treeeead9e450ed44ac46393ad6db6df8d2f5d7a2bf0 /gallery_dl/extractor/imgur.py
parent33d4eae5a6df8aaf6757f52ae25f514ff1211c62 (diff)
New upstream version 1.25.4.upstream/1.25.4
Diffstat (limited to 'gallery_dl/extractor/imgur.py')
-rw-r--r--gallery_dl/extractor/imgur.py80
1 files changed, 66 insertions, 14 deletions
diff --git a/gallery_dl/extractor/imgur.py b/gallery_dl/extractor/imgur.py
index f8f1600..4c29d98 100644
--- a/gallery_dl/extractor/imgur.py
+++ b/gallery_dl/extractor/imgur.py
@@ -47,8 +47,13 @@ class ImgurExtractor(Extractor):
image_ex = ImgurImageExtractor
for item in items:
- item["_extractor"] = album_ex if item["is_album"] else image_ex
- yield Message.Queue, item["link"], item
+ if item["is_album"]:
+ url = "https://imgur.com/a/" + item["id"]
+ item["_extractor"] = album_ex
+ else:
+ url = "https://imgur.com/" + item["id"]
+ item["_extractor"] = image_ex
+ yield Message.Queue, url, item
class ImgurImageExtractor(ImgurExtractor):
@@ -272,7 +277,7 @@ class ImgurUserExtractor(ImgurExtractor):
("https://imgur.com/user/Miguenzo", {
"range": "1-100",
"count": 100,
- "pattern": r"https?://(i.imgur.com|imgur.com/a)/[\w.]+",
+ "pattern": r"https://imgur\.com(/a)?/\w+$",
}),
("https://imgur.com/user/Miguenzo/posts"),
("https://imgur.com/user/Miguenzo/submitted"),
@@ -285,17 +290,41 @@ class ImgurUserExtractor(ImgurExtractor):
class ImgurFavoriteExtractor(ImgurExtractor):
"""Extractor for a user's favorites"""
subcategory = "favorite"
- pattern = BASE_PATTERN + r"/user/([^/?#]+)/favorites"
+ pattern = BASE_PATTERN + r"/user/([^/?#]+)/favorites/?$"
test = ("https://imgur.com/user/Miguenzo/favorites", {
"range": "1-100",
"count": 100,
- "pattern": r"https?://(i.imgur.com|imgur.com/a)/[\w.]+",
+ "pattern": r"https://imgur\.com(/a)?/\w+$",
})
def items(self):
return self._items_queue(self.api.account_favorites(self.key))
+class ImgurFavoriteFolderExtractor(ImgurExtractor):
+ """Extractor for a user's favorites folder"""
+ subcategory = "favorite-folder"
+ pattern = BASE_PATTERN + r"/user/([^/?#]+)/favorites/folder/(\d+)"
+ test = (
+ ("https://imgur.com/user/mikf1/favorites/folder/11896757/public", {
+ "pattern": r"https://imgur\.com(/a)?/\w+$",
+ "count": 3,
+ }),
+ ("https://imgur.com/user/mikf1/favorites/folder/11896741/private", {
+ "pattern": r"https://imgur\.com(/a)?/\w+$",
+ "count": 5,
+ }),
+ )
+
+ def __init__(self, match):
+ ImgurExtractor.__init__(self, match)
+ self.folder_id = match.group(2)
+
+ def items(self):
+ return self._items_queue(self.api.account_favorites_folder(
+ self.key, self.folder_id))
+
+
class ImgurSubredditExtractor(ImgurExtractor):
"""Extractor for a subreddits's imgur links"""
subcategory = "subreddit"
@@ -303,7 +332,7 @@ class ImgurSubredditExtractor(ImgurExtractor):
test = ("https://imgur.com/r/pics", {
"range": "1-100",
"count": 100,
- "pattern": r"https?://(i.imgur.com|imgur.com/a)/[\w.]+",
+ "pattern": r"https://imgur\.com(/a)?/\w+$",
})
def items(self):
@@ -317,7 +346,7 @@ class ImgurTagExtractor(ImgurExtractor):
test = ("https://imgur.com/t/animals", {
"range": "1-100",
"count": 100,
- "pattern": r"https?://(i.imgur.com|imgur.com/a)/[\w.]+",
+ "pattern": r"https://imgur\.com(/a)?/\w+$",
})
def items(self):
@@ -331,7 +360,7 @@ class ImgurSearchExtractor(ImgurExtractor):
test = ("https://imgur.com/search?q=cute+cat", {
"range": "1-100",
"count": 100,
- "pattern": r"https?://(i.imgur.com|imgur.com/a)/[\w.]+",
+ "pattern": r"https://imgur\.com(/a)?/\w+$",
})
def items(self):
@@ -346,15 +375,18 @@ class ImgurAPI():
"""
def __init__(self, extractor):
self.extractor = extractor
- self.headers = {
- "Authorization": "Client-ID " + (
- extractor.config("client-id") or "546c25a59c58ad7"),
- }
+ self.client_id = extractor.config("client-id") or "546c25a59c58ad7"
+ self.headers = {"Authorization": "Client-ID " + self.client_id}
def account_favorites(self, account):
endpoint = "/3/account/{}/gallery_favorites".format(account)
return self._pagination(endpoint)
+ def account_favorites_folder(self, account, folder_id):
+ endpoint = "/3/account/{}/folders/{}/favorites".format(
+ account, folder_id)
+ return self._pagination_v2(endpoint)
+
def gallery_search(self, query):
endpoint = "/3/gallery/search"
params = {"q": query}
@@ -386,12 +418,12 @@ class ImgurAPI():
endpoint = "/post/v1/posts/" + gallery_hash
return self._call(endpoint)
- def _call(self, endpoint, params=None):
+ def _call(self, endpoint, params=None, headers=None):
while True:
try:
return self.extractor.request(
"https://api.imgur.com" + endpoint,
- params=params, headers=self.headers,
+ params=params, headers=(headers or self.headers),
).json()
except exception.HttpError as exc:
if exc.status not in (403, 429) or \
@@ -410,3 +442,23 @@ class ImgurAPI():
return
yield from data
num += 1
+
+ def _pagination_v2(self, endpoint, params=None, key=None):
+ if params is None:
+ params = {}
+ params["client_id"] = self.client_id
+ params["page"] = 0
+ params["sort"] = "newest"
+
+ headers = {
+ "Referer": "https://imgur.com/",
+ "Origin": "https://imgur.com",
+ }
+
+ while True:
+ data = self._call(endpoint, params, headers)["data"]
+ if not data:
+ return
+ yield from data
+
+ params["page"] += 1