| 1 |
|
| 2 |
|
| 3 |
n |
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
"""Extractors for https://imgur.com/""" |
| 10 |
|
| 11 |
from .common import Extractor, Message |
| 12 |
from .. import text |
| 13 |
|
| 14 |
BASE_PATTERN = r"(?:https?://)?(?:www\.|[im]\.)?imgur\.(?:com|io)" |
| 15 |
|
| 16 |
|
| 17 |
class ImgurExtractor(Extractor): |
| 18 |
"""Base class for imgur extractors""" |
| 19 |
category = "imgur" |
| 20 |
root = "https://imgur.com" |
| 21 |
|
| 22 |
def __init__(self, match): |
| 23 |
Extractor.__init__(self, match) |
| 24 |
self.key = match[1] |
| 25 |
|
| 26 |
def _init(self): |
| 27 |
self.api = ImgurAPI(self) |
| 28 |
self.mp4 = self.config("mp4", True) |
| 29 |
|
| 30 |
def _prepare(self, image): |
| 31 |
image.update(image["metadata"]) |
| 32 |
del image["metadata"] |
| 33 |
|
| 34 |
if image["ext"] == "jpeg": |
| 35 |
image["ext"] = "jpg" |
| 36 |
elif image["is_animated"] and self.mp4 and image["ext"] == "gif": |
| 37 |
image["ext"] = "mp4" |
| 38 |
|
| 39 |
image["url"] = url = \ |
| 40 |
f"https://i.imgur.com/{image['id']}.{image['ext']}" |
| 41 |
image["date"] = self.parse_datetime_iso(image["created_at"]) |
| 42 |
image["_http_validate"] = self._validate |
| 43 |
text.nameext_from_url(url, image) |
| 44 |
|
| 45 |
return url |
| 46 |
|
| 47 |
def _validate(self, response): |
| 48 |
return (not response.history or |
| 49 |
not response.url.endswith("/removed.png")) |
| 50 |
|
| 51 |
def _items_queue(self, items): |
| 52 |
album_ex = ImgurAlbumExtractor |
| 53 |
image_ex = ImgurImageExtractor |
| 54 |
|
| 55 |
for item in items: |
| 56 |
if item["is_album"]: |
| 57 |
url = "https://imgur.com/a/" + item["id"] |
| 58 |
item["_extractor"] = album_ex |
| 59 |
else: |
| 60 |
url = "https://imgur.com/" + item["id"] |
| 61 |
item["_extractor"] = image_ex |
| 62 |
yield Message.Queue, url, item |
| 63 |
|
| 64 |
|
| 65 |
class ImgurImageExtractor(ImgurExtractor): |
| 66 |
"""Extractor for individual images on imgur.com""" |
| 67 |
subcategory = "image" |
| 68 |
filename_fmt = "{category}_{id}{title:?_//}.{extension}" |
| 69 |
archive_fmt = "{id}" |
| 70 |
pattern = (BASE_PATTERN + r"/(?!gallery|search)" |
| 71 |
r"(?:r/\w+/)?(?:[^/?#]+-)?(\w{7}|\w{5})[sbtmlh]?") |
| 72 |
example = "https://imgur.com/abcdefg" |
| 73 |
|
| 74 |
def items(self): |
| 75 |
image = self.api.image(self.key) |
| 76 |
|
| 77 |
try: |
| 78 |
del image["ad_url"] |
| 79 |
del image["ad_type"] |
| 80 |
except KeyError: |
| 81 |
pass |
| 82 |
|
| 83 |
image.update(image["media"][0]) |
| 84 |
del image["media"] |
| 85 |
url = self._prepare(image) |
| 86 |
yield Message.Directory, "", image |
| 87 |
yield Message.Url, url, image |
| 88 |
|
| 89 |
|
| 90 |
class ImgurAlbumExtractor(ImgurExtractor): |
| 91 |
"""Extractor for imgur albums""" |
| 92 |
subcategory = "album" |
| 93 |
directory_fmt = ("{category}", "{album[id]}{album[title]:? - //}") |
| 94 |
filename_fmt = "{category}_{album[id]}_{num:>03}_{id}.{extension}" |
| 95 |
archive_fmt = "{album[id]}_{id}" |
| 96 |
pattern = BASE_PATTERN + r"/a/(?:[^/?#]+-)?(\w{7}|\w{5})" |
| 97 |
example = "https://imgur.com/a/abcde" |
| 98 |
|
| 99 |
def items(self): |
| 100 |
album = self.api.album(self.key) |
| 101 |
|
| 102 |
try: |
| 103 |
images = album["media"] |
| 104 |
except KeyError: |
| 105 |
return |
| 106 |
|
| 107 |
del album["media"] |
| 108 |
count = len(images) |
| 109 |
album["date"] = self.parse_datetime_iso(album["created_at"]) |
| 110 |
|
| 111 |
try: |
| 112 |
del album["ad_url"] |
| 113 |
del album["ad_type"] |
| 114 |
except KeyError: |
| 115 |
pass |
| 116 |
|
| 117 |
for num, image in enumerate(images, 1): |
| 118 |
url = self._prepare(image) |
| 119 |
image["num"] = num |
| 120 |
image["count"] = count |
| 121 |
image["album"] = album |
| 122 |
yield Message.Directory, "", image |
| 123 |
yield Message.Url, url, image |
| 124 |
|
| 125 |
|
| 126 |
class ImgurGalleryExtractor(ImgurExtractor): |
| 127 |
"""Extractor for imgur galleries""" |
| 128 |
subcategory = "gallery" |
| 129 |
pattern = BASE_PATTERN + r"/(?:gallery|t/\w+)/(?:[^/?#]+-)?(\w{7}|\w{5})" |
| 130 |
example = "https://imgur.com/gallery/abcde" |
| 131 |
|
| 132 |
def items(self): |
| 133 |
if self.api.gallery(self.key)["is_album"]: |
| 134 |
url = f"{self.root}/a/{self.key}" |
| 135 |
extr = ImgurAlbumExtractor |
| 136 |
else: |
| 137 |
url = f"{self.root}/{self.key}" |
| 138 |
extr = ImgurImageExtractor |
| 139 |
yield Message.Queue, url, {"_extractor": extr} |
| 140 |
|
| 141 |
|
| 142 |
class ImgurUserExtractor(ImgurExtractor): |
| 143 |
"""Extractor for all images posted by a user""" |
| 144 |
subcategory = "user" |
| 145 |
pattern = (BASE_PATTERN + r"/user/(?!me(?:/|$|\?|#))" |
| 146 |
r"([^/?#]+)(?:/posts|/submitted)?/?$") |
| 147 |
example = "https://imgur.com/user/USER" |
| 148 |
|
| 149 |
def items(self): |
| 150 |
return self._items_queue(self.api.account_submissions(self.key)) |
| 151 |
|
| 152 |
|
| 153 |
class ImgurFavoriteExtractor(ImgurExtractor): |
| 154 |
"""Extractor for a user's favorites""" |
| 155 |
subcategory = "favorite" |
| 156 |
pattern = BASE_PATTERN + r"/user/([^/?#]+)/favorites/?$" |
| 157 |
example = "https://imgur.com/user/USER/favorites" |
| 158 |
|
| 159 |
def items(self): |
| 160 |
return self._items_queue(self.api.account_favorites(self.key)) |
| 161 |
|
| 162 |
|
| 163 |
class ImgurFavoriteFolderExtractor(ImgurExtractor): |
| 164 |
"""Extractor for a user's favorites folder""" |
| 165 |
subcategory = "favorite-folder" |
| 166 |
pattern = BASE_PATTERN + r"/user/([^/?#]+)/favorites/folder/(\d+)" |
| 167 |
example = "https://imgur.com/user/USER/favorites/folder/12345/TITLE" |
| 168 |
|
| 169 |
def __init__(self, match): |
| 170 |
ImgurExtractor.__init__(self, match) |
| 171 |
self.folder_id = match[2] |
| 172 |
|
| 173 |
def items(self): |
| 174 |
return self._items_queue(self.api.account_favorites_folder( |
| 175 |
self.key, self.folder_id)) |
| 176 |
|
| 177 |
|
| 178 |
class ImgurMeExtractor(ImgurExtractor): |
| 179 |
"""Extractor for your personal uploads""" |
| 180 |
subcategory = "me" |
| 181 |
pattern = BASE_PATTERN + r"/user/me(?:/posts)?(/hidden)?" |
| 182 |
example = "https://imgur.com/user/me" |
| 183 |
|
| 184 |
def items(self): |
| 185 |
if not self.cookies_check(("accesstoken",)): |
| 186 |
self.log.error("'accesstoken' cookie required") |
| 187 |
|
| 188 |
if self.groups[0]: |
| 189 |
posts = self.api.accounts_me_hiddenalbums() |
| 190 |
else: |
| 191 |
posts = self.api.accounts_me_allposts() |
| 192 |
return self._items_queue(posts) |
| 193 |
|
| 194 |
|
| 195 |
class ImgurSubredditExtractor(ImgurExtractor): |
| 196 |
"""Extractor for a subreddits's imgur links""" |
| 197 |
subcategory = "subreddit" |
| 198 |
pattern = BASE_PATTERN + r"/r/([^/?#]+)/?$" |
| 199 |
example = "https://imgur.com/r/SUBREDDIT" |
| 200 |
|
| 201 |
def items(self): |
| 202 |
return self._items_queue(self.api.gallery_subreddit(self.key)) |
| 203 |
|
| 204 |
|
| 205 |
class ImgurTagExtractor(ImgurExtractor): |
| 206 |
"""Extractor for imgur tag searches""" |
| 207 |
subcategory = "tag" |
| 208 |
pattern = BASE_PATTERN + r"/t/([^/?#]+)$" |
| 209 |
example = "https://imgur.com/t/TAG" |
| 210 |
|
| 211 |
def items(self): |
| 212 |
return self._items_queue(self.api.gallery_tag(self.key)) |
| 213 |
|
| 214 |
|
| 215 |
class ImgurSearchExtractor(ImgurExtractor): |
| 216 |
"""Extractor for imgur search results""" |
| 217 |
subcategory = "search" |
| 218 |
pattern = BASE_PATTERN + r"/search(?:/[^?#]+)?/?\?q=([^&#]+)" |
| 219 |
example = "https://imgur.com/search?q=UERY" |
| 220 |
|
| 221 |
def items(self): |
| 222 |
key = text.unquote(self.key.replace("+", " ")) |
| 223 |
return self._items_queue(self.api.gallery_search(key)) |
| 224 |
|
| 225 |
|
| 226 |
class ImgurAPI(): |
| 227 |
"""Interface for the Imgur API |
| 228 |
|
| 229 |
Ref: https://apidocs.imgur.com/ |
| 230 |
""" |
| 231 |
def __init__(self, extractor): |
| 232 |
self.extractor = extractor |
| 233 |
self.client_id = extractor.config("client-id") or "546c25a59c58ad7" |
| 234 |
self.headers = {"Authorization": "Client-ID " + self.client_id} |
| 235 |
|
| 236 |
def account_submissions(self, account): |
| 237 |
endpoint = f"/3/account/{account}/submissions" |
| 238 |
return self._pagination(endpoint) |
| 239 |
|
| 240 |
def account_favorites(self, account): |
| 241 |
endpoint = f"/3/account/{account}/gallery_favorites" |
| 242 |
return self._pagination(endpoint) |
| 243 |
|
| 244 |
def account_favorites_folder(self, account, folder_id): |
| 245 |
endpoint = f"/3/account/{account}/folders/{folder_id}/favorites" |
| 246 |
return self._pagination_v2(endpoint) |
| 247 |
|
| 248 |
def accounts_me_allposts(self): |
| 249 |
endpoint = "/post/v1/accounts/me/all_posts" |
| 250 |
params = { |
| 251 |
"include": "media,tags,account", |
| 252 |
"page" : 1, |
| 253 |
"sort" : "-created_at", |
| 254 |
} |
| 255 |
return self._pagination_v2(endpoint, params) |
| 256 |
|
| 257 |
def accounts_me_hiddenalbums(self): |
| 258 |
endpoint = "/post/v1/accounts/me/hidden_albums" |
| 259 |
params = { |
| 260 |
"include": "media,tags,account", |
| 261 |
"page" : 1, |
| 262 |
"sort" : "-created_at", |
| 263 |
} |
| 264 |
return self._pagination_v2(endpoint, params) |
| 265 |
|
| 266 |
def gallery_search(self, query): |
| 267 |
endpoint = "/3/gallery/search" |
| 268 |
params = {"q": query} |
| 269 |
return self._pagination(endpoint, params) |
| 270 |
|
| 271 |
def gallery_subreddit(self, subreddit): |
| 272 |
endpoint = "/3/gallery/r/" + subreddit |
| 273 |
return self._pagination(endpoint) |
| 274 |
|
| 275 |
def gallery_tag(self, tag): |
| 276 |
endpoint = "/3/gallery/t/" + tag |
| 277 |
return self._pagination(endpoint, key="items") |
| 278 |
|
| 279 |
def image(self, image_hash): |
| 280 |
endpoint = "/post/v1/media/" + image_hash |
| 281 |
params = {"include": "media,tags,account"} |
| 282 |
return self._call(endpoint, params) |
| 283 |
|
| 284 |
def album(self, album_hash): |
| 285 |
endpoint = "/post/v1/albums/" + album_hash |
| 286 |
params = {"include": "media,tags,account"} |
| 287 |
return self._call(endpoint, params) |
| 288 |
|
| 289 |
def gallery(self, gallery_hash): |
| 290 |
endpoint = "/post/v1/posts/" + gallery_hash |
| 291 |
return self._call(endpoint) |
| 292 |
|
| 293 |
def _call(self, endpoint, params=None): |
| 294 |
while True: |
| 295 |
try: |
| 296 |
return self.extractor.request_json( |
| 297 |
"https://api.imgur.com" + endpoint, |
| 298 |
params=params, headers=self.headers) |
| 299 |
except self.extractor.exc.HttpError as exc: |
| 300 |
if exc.status not in (403, 429) or \ |
| 301 |
b"capacity" not in exc.response.content: |
| 302 |
raise |
| 303 |
self.extractor.wait(seconds=600) |
| 304 |
|
| 305 |
def _pagination(self, endpoint, params=None, key=None): |
| 306 |
num = 0 |
| 307 |
|
| 308 |
while True: |
| 309 |
data = self._call(f"{endpoint}/{num}", params)["data"] |
| 310 |
if key: |
| 311 |
data = data[key] |
| 312 |
if not data: |
| 313 |
return |
| 314 |
yield from data |
| 315 |
num += 1 |
| 316 |
|
| 317 |
def _pagination_v2(self, endpoint, params=None, key=None): |
| 318 |
if params is None: |
| 319 |
params = {} |
| 320 |
params["client_id"] = self.client_id |
| 321 |
if "page" not in params: |
| 322 |
params["page"] = 0 |
| 323 |
if "sort" not in params: |
| 324 |
params["sort"] = "newest" |
| 325 |
|
| 326 |
while True: |
| 327 |
data = self._call(endpoint, params) |
| 328 |
if "data" in data: |
| 329 |
data = data["data"] |
| 330 |
if not data: |
| 331 |
return |
| 332 |
yield from data |
| 333 |
|
| 334 |
params["page"] += 1 |