Debian Codegraph @

gallery_dl/extractor/imgbox.py

raw symbols SCIP
1 # -*- coding: utf-8 -*-
2
3 # Copyright 2014-2025 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://imgbox.com/"""
10
11 from .common import Extractor, Message, AsynchronousMixin
12 from .. import text
13
14
15 class ImgboxExtractor(Extractor):
16 """Base class for imgbox extractors"""
17 category = "imgbox"
18 root = "https://imgbox.com"
19
20 def items(self):
21 data = self.get_job_metadata()
22 yield Message.Directory, "", data
23
24 for image_key in self.get_image_keys():
25 imgpage = self.request(self.root + "/" + image_key).text
26 imgdata = self.get_image_metadata(imgpage)
27 if imgdata["filename"]:
28 imgdata.update(data)
29 imgdata["image_key"] = image_key
30 text.nameext_from_url(imgdata["filename"], imgdata)
31 yield Message.Url, self.get_image_url(imgpage), imgdata
32
33 def get_job_metadata(self):
34 """Collect metadata for extractor-job"""
35 return {}
36
37 def get_image_keys(self):
38 """Return an iterable containing all image-keys"""
39 return []
40
41 def get_image_metadata(self, page):
42 """Collect metadata for a downloadable file"""
43 return text.extract_all(page, (
44 ("num" , '</a> &nbsp; ', ' of '),
45 (None , 'class="image-container"', ''),
46 ("filename" , ' title="', '"'),
47 ))[0]
48
49 def get_image_url(self, page):
50 """Extract download-url"""
51 return text.extr(page, 'property="og:image" content="', '"')
52
53
54 class ImgboxGalleryExtractor(AsynchronousMixin, ImgboxExtractor):
55 """Extractor for image galleries from imgbox.com"""
56 subcategory = "gallery"
57 directory_fmt = ("{category}", "{title} - {gallery_key}")
58 filename_fmt = "{num:>03}-{filename}.{extension}"
59 archive_fmt = "{gallery_key}_{image_key}"
60 pattern = r"(?:https?://)?(?:www\.)?imgbox\.com/g/([A-Za-z0-9]{10})"
61 example = "https://imgbox.com/g/12345abcde"
62
63 def __init__(self, match):
64 ImgboxExtractor.__init__(self, match)
65 self.gallery_key = match[1]
66 self.image_keys = []
67
68 def get_job_metadata(self):
69 page = self.request(self.root + "/g/" + self.gallery_key).text
70 if "The specified gallery could not be found." in page:
71 raise self.exc.NotFoundError("gallery")
72 self.image_keys = text.re(
73 r'<a href="/([^"]+)"><img alt="').findall(page)
74
75 title = text.extr(page, "<h1>", "</h1>")
76 title, _, count = title.rpartition(" - ")
77 return {
78 "gallery_key": self.gallery_key,
79 "title": text.unescape(title),
80 "count": count[:-7],
81 }
82
83 def get_image_keys(self):
84 return self.image_keys
85
86
87 class ImgboxImageExtractor(ImgboxExtractor):
88 """Extractor for single images from imgbox.com"""
89 subcategory = "image"
90 archive_fmt = "{image_key}"
91 pattern = (r"(?:https?://)?(?:"
92 r"(?:www\.|i\.)?imgbox\.com|"
93 r"images\d+\.imgbox\.com/[0-9a-f]{2}/[0-9a-f]{2}"
94 r")/([A-Za-z0-9]{8})")
95 example = "https://imgbox.com/1234abcd"
96
97 def __init__(self, match):
98 ImgboxExtractor.__init__(self, match)
99 self.image_key = match[1]
100
101 def get_image_keys(self):
102 return (self.image_key,)
103
104 def get_image_metadata(self, page):
105 data = ImgboxExtractor.get_image_metadata(self, page)
106 if not data["filename"]:
107 raise self.exc.NotFoundError("image")
108 return data