Debian Codegraph @

gallery_dl/extractor/imagebam.py

raw symbols SCIP
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://www.imagebam.com/"""
10
11 from .common import Extractor, Message
12 from .. import text
13
14
15 class ImagebamExtractor(Extractor):
16 """Base class for imagebam extractors"""
17 category = "imagebam"
18 root = "https://www.imagebam.com"
19
20 def __init__(self, match):
21 Extractor.__init__(self, match)
22 self.path = match[1]
23
24 def _init(self):
25 self.cookies.set("nsfw_inter", "1", domain="www.imagebam.com")
26 self.cookies.set("sfw_inter", "1", domain="www.imagebam.com")
27
28 def _parse_image_page(self, path):
29 page = self.request(self.root + path).text
30 url, pos = text.extract(page, '<img src="https://images', '"')
31 if not url:
32 raise self.exc.NotFoundError("image")
33 filename = text.unescape(text.extract(page, 'alt="', '"', pos)[0])
34
35 return text.nameext_from_name(filename, {
36 "url" : "https://images" + url,
37 "image_key": path.rpartition("/")[2],
38 })
39
40
41 class ImagebamGalleryExtractor(ImagebamExtractor):
42 """Extractor for imagebam galleries"""
43 subcategory = "gallery"
44 directory_fmt = ("{category}", "{title} {gallery_key}")
45 filename_fmt = "{num:>03} {filename}.{extension}"
46 archive_fmt = "{gallery_key}_{image_key}"
47 pattern = (r"(?:https?://)?(?:www\.)?imagebam\.com"
48 r"(/(?:gallery/|view/G)[a-zA-Z0-9]+)")
49 example = "https://www.imagebam.com/view/GID"
50
51 def items(self):
52 page = self.request(self.root + self.path, notfound=True).text
53
54 images = self.images(page)
55 images.reverse()
56
57 data = self.metadata(page)
58 data["count"] = len(images)
59 data["gallery_key"] = self.path.rpartition("/")[2]
60
61 yield Message.Directory, "", data
62 for data["num"], path in enumerate(images, 1):
63 image = self._parse_image_page(path)
64 image.update(data)
65 yield Message.Url, image["url"], image
66
67 def metadata(self, page):
68 return {"title": text.unescape(text.extr(
69 page, 'id="gallery-name">', '<').strip())}
70
71 def images(self, page):
72 findall = text.re(r'<a href="https://www\.imagebam\.com'
73 r'(/(?:image/|view/M)[a-zA-Z0-9]+)').findall
74 paths = []
75 while True:
76 paths += findall(page)
77 pos = page.find('rel="next" aria-label="Next')
78 if pos > 0:
79 if url := text.rextr(page, 'href="', '"', pos):
80 page = self.request(url).text
81 continue
82 return paths
83
84
85 class ImagebamImageExtractor(ImagebamExtractor):
86 """Extractor for single imagebam images"""
87 subcategory = "image"
88 archive_fmt = "{image_key}"
89 pattern = (r"(?:https?://)?(?:\w+\.)?imagebam\.com"
90 r"(/(?:image/|view/M|(?:[0-9a-f]{2}/){3})[a-zA-Z0-9]+)")
91 example = "https://www.imagebam.com/view/MID"
92
93 def items(self):
94 path = self.path
95 if path[3] == "/":
96 path = ("/view/" if path[10] == "M" else "/image/") + path[10:]
97
98 image = self._parse_image_page(path)
99 yield Message.Directory, "", image
100 yield Message.Url, image["url"], image