Debian Codegraph @

gallery_dl/extractor/imageshack.py

raw symbols SCIP
1 # -*- coding: utf-8 -*-
2
3 # Copyright 2026 Mike Fährmann
4 #
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License version 2 as
7 # published by the Free Software Foundation.
8
9 """Extractors for https://imageshack.com/"""
10
11 from .common import Extractor, Message
12 from .. import text
13 import time
14
15 BASE_PATTERN = r"(?:https?://)?(?:www\.)?imageshack\.com"
16
17
18 class ImageshackExtractor(Extractor):
19 """Base class for imageshack extractors"""
20 category = "imageshack"
21 root = "https://imageshack.com"
22 directory_fmt = ("{category}", "{owner[username]}")
23 filename_fmt = "{date:%Y-%m-%d} {id}{title:? - //[:230]}.{extension}"
24 archive_fmt = "{id}"
25 offset = 0
26
27 def items(self):
28 for img in self.images():
29 img["date"] = self.parse_timestamp(img["creation_date"])
30 url = text.ensure_http_scheme(img["direct_link"])
31
32 if name := img["original_filename"]:
33 text.nameext_from_name(name, img)
34 if not img["extension"]:
35 img["extension"] = text.ext_from_url(url)
36 else:
37 text.nameext_from_url(url, img)
38
39 yield Message.Directory, "", img
40 yield Message.Url, url, img
41
42 def request_api(self, endpoint, params):
43 url = f"{self.root}/rest_api/{endpoint}"
44 params["ts"] = int(time.time())
45 headers = {
46 "Accept" : "application/json, text/javascript, */*; q=0.01",
47 "Content-Type": "application/json",
48 "X-Requested-With": "XMLHttpRequest",
49 "Priority" : "u=0",
50 }
51 return self.request_json(url, params=params, headers=headers)["result"]
52
53 def _pagination(self, endpoint, params, callback=None):
54 while True:
55 data = self.request_api(endpoint, params)
56
57 if callback is not None:
58 callback(data)
59 callback = None
60 self.kwdict["total"] = total = data.get("total")
61
62 yield from data["images"]
63
64 try:
65 if data["offset"] >= total:
66 break
67 except Exception as exc:
68 self.log.traceback(exc)
69 break
70 params["offset"] += params["limit"]
71
72 def skip_posts(self, n):
73 self.offset += n
74 return n
75
76 skip_files = skip_posts
77
78
79 class ImageshackImageExtractor(ImageshackExtractor):
80 subcategory = "image"
81 pattern = BASE_PATTERN + r"/i/([^/?#]+)"
82 example = "https://imageshack.com/i/ID"
83
84 def images(self):
85 img = self.request_api("/v2/images/" + self.groups[0], {})
86 return (img,)
87
88 skip_files = skip_posts = None
89
90
91 class ImageshackGalleryExtractor(ImageshackExtractor):
92 subcategory = "gallery"
93 directory_fmt = ("{category}", "{owner[username]}",
94 "{gallery[title]} ({gallery[id]})")
95 archive_fmt = "{gallery[id]}/{id}"
96 pattern = BASE_PATTERN + r"/a/([^/?#]+)"
97 example = "https://imageshack.com/a/ID"
98
99 def images(self):
100 return self._pagination("/v2/albums/" + self.groups[0], {
101 "limit" : 40,
102 "offset" : self.offset,
103 "hide_empty" : "true",
104 "show_private": "false",
105 }, self._parse_gallery)
106
107 def _parse_gallery(self, data):
108 self.kwdict["gallery"] = {
109 "id" : data.get("id"),
110 "title" : data.get("title"),
111 "description": data.get("description"),
112 "owner" : data.get("owner"),
113 "date" : self.parse_timestamp(data.get("creation_date")),
114 }
115
116
117 class ImageshackUserExtractor(ImageshackExtractor):
118 subcategory = "user"
119 pattern = BASE_PATTERN + r"/user/([^/?#]+)"
120 example = "https://imageshack.com/user/USER"
121
122 def images(self):
123 return self._pagination("/v2/images", {
124 "username" : self.groups[0],
125 "limit" : 40,
126 "offset" : self.offset,
127 "hide_empty": "true",
128 })