Debian Codegraph @

gallery_dl/extractor/imgbox.py

raw
1 # -*- coding: utf-8 -*-
2
3 # Copyright 2014-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://imgbox.com/"""
10
11 from .common import Extractor, Message
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(ImgboxExtractor):
55 """Extractor for image galleries from imgbox.com"""
56 subcategory = "gallery"
57 async_mode = True
58 directory_fmt = ("{category}", "{title} - {gallery_key}")
59 filename_fmt = "{num:>03}-{filename}.{extension}"
60 archive_fmt = "{gallery_key}_{image_key}"
61 pattern = r"(?:https?://)?(?:www\.)?imgbox\.com/g/([A-Za-z0-9]{10})"
62 example = "https://imgbox.com/g/12345abcde"
63
64 def __init__(self, match):
65 ImgboxExtractor.__init__(self, match)
66 self.gallery_key = match[1]
67 self.image_keys = []
68
69 def get_job_metadata(self):
70 page = self.request(self.root + "/g/" + self.gallery_key).text
71 if "The specified gallery could not be found." in page:
72 raise self.exc.NotFoundError("gallery")
73 self.image_keys = text.re(
74 r'<a href="/([^"]+)"><img alt="').findall(page)
75
76 title = text.extr(page, "<h1>", "</h1>")
77 title, _, count = title.rpartition(" - ")
78 return {
79 "gallery_key": self.gallery_key,
80 "title": text.unescape(title),
81 "count": count[:-7],
82 }
83
84 def get_image_keys(self):
85 return self.image_keys
86
87
88 class ImgboxImageExtractor(ImgboxExtractor):
89 """Extractor for single images from imgbox.com"""
90 subcategory = "image"
91 archive_fmt = "{image_key}"
92 pattern = (r"(?:https?://)?(?:"
93 r"(?:www\.|i\.)?imgbox\.com|"
94 r"images\d+\.imgbox\.com/[0-9a-f]{2}/[0-9a-f]{2}"
95 r")/([A-Za-z0-9]{8})")
96 example = "https://imgbox.com/1234abcd"
97
98 def __init__(self, match):
99 ImgboxExtractor.__init__(self, match)
100 self.image_key = match[1]
101
102 def get_image_keys(self):
103 return (self.image_key,)
104
105 def get_image_metadata(self, page):
106 data = ImgboxExtractor.get_image_metadata(self, page)
107 if not data["filename"]:
108 raise self.exc.NotFoundError("image")
109 return data