Debian Codegraph @

gallery_dl/extractor/bakashots.py

raw
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://compare.bakashots.me/"""
10
11 from .common import GalleryExtractor, Extractor, Message
12 from .. import text, util
13
14 BASE_PATTERN = r"(?:https?://)?compare\.bakashots\.me"
15
16
17 class BakashotsExtractor(Extractor):
18 """Base class for bakashots extractors"""
19 category = "bakashots"
20 root = "https://compare.bakashots.me"
21
22
23 class BakashotsComparisonExtractor(GalleryExtractor, BakashotsExtractor):
24 """Extractor for bakashots comparisons"""
25 subcategory = "comparison"
26 directory_fmt = ("{category}", "{set_id} {title}")
27 filename_fmt = "{num:>02}.{extension}"
28 archive_fmt = "{set_id}_{num}"
29 pattern = BASE_PATTERN + r"(/compare\.php\?setId=(\d+))"
30 example = "https://compare.bakashots.me/compare.php?setId=12345"
31
32 def metadata(self, page):
33 extr = text.extract_from(page)
34 self.data = util.json_loads(extr("var G_imageLists =", ";\n"))
35
36 return {
37 "set_id": text.parse_int(self.groups[1]),
38 "title" : text.unescape(extr("<title>", "<")),
39 "date" : self.parse_datetime(extr(
40 ">Added: ", " ("), "%d/%m/%Y at %H:%M:%S"),
41 }
42
43 def images(self, page):
44 return [
45 (self.root + image["url"], {
46 **image,
47 "comparison_id": comparison.get("comparison_id"),
48 "extension": "png",
49 })
50 for comparison in self.data
51 for image in comparison["images"]
52 ]
53
54
55 class BakashotsSearchExtractor(BakashotsExtractor):
56 """Extractor for bakashots search results"""
57 subcategory = "search"
58 pattern = BASE_PATTERN + r"/search\.php\?([^#]+)"
59 example = "https://compare.bakashots.me/search.php?searchTherms=QUERY"
60
61 def items(self):
62 data = {"_extractor": BakashotsComparisonExtractor}
63 base = self.root + "/compare.php?setId="
64
65 url = self.root + "/search.php"
66 params = text.parse_query(self.groups[0])
67 params["page"] = text.parse_int(params.get("page"), 0)
68
69 while True:
70 page = self.request(url, params=params).text
71
72 for set_id in text.extract_iter(
73 page, 'href="/compare.php?setId=', '"'):
74 yield Message.Queue, base + set_id, data
75
76 if ">Next >><" not in page:
77 break
78 params["page"] += 1