Debian Codegraph @

gallery_dl/extractor/lolisafe.py

raw
1 # -*- coding: utf-8 -*-
2
3 # Copyright 2021-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 lolisafe/chibisafe instances"""
10
11 from .common import BaseExtractor, Message
12 from .. import text
13
14
15 class LolisafeExtractor(BaseExtractor):
16 """Base class for lolisafe extractors"""
17 basecategory = "lolisafe"
18 directory_fmt = ("{category}", "{album_name} ({album_id})")
19 archive_fmt = "{album_id}_{id}"
20
21
22 BASE_PATTERN = LolisafeExtractor.update({
23 })
24
25
26 class LolisafeAlbumExtractor(LolisafeExtractor):
27 subcategory = "album"
28 pattern = BASE_PATTERN + "/a/([^/?#]+)"
29 example = "https://xbunkr.com/a/ID"
30
31 def __init__(self, match):
32 LolisafeExtractor.__init__(self, match)
33 self.album_id = self.groups[-1]
34
35 def _init(self):
36 domain = self.config("domain")
37 if domain == "auto":
38 self.root = text.root_from_url(self.url)
39 elif domain:
40 self.root = text.ensure_http_scheme(domain)
41
42 def items(self):
43 files, data = self.fetch_album(self.album_id)
44
45 yield Message.Directory, "", data
46 for data["num"], file in enumerate(files, 1):
47 url = file["file"]
48 file.update(data)
49
50 if "extension" not in file:
51 text.nameext_from_url(url, file)
52
53 if "name" in file:
54 name = file["name"]
55 file["name"] = name.rpartition(".")[0] or name
56 _, sep, fid = file["filename"].rpartition("-")
57 if not sep or len(fid) == 12:
58 if "id" not in file:
59 file["id"] = ""
60 file["filename"] = file["name"]
61 else:
62 file["id"] = fid
63 file["filename"] = f"{file['name']}-{fid}"
64 elif "id" in file:
65 file["name"] = file["filename"]
66 if file["filename"] != file["id"]:
67 file["filename"] = f"{file['name']}-{file['id']}"
68 else:
69 file["name"], sep, file["id"] = \
70 file["filename"].rpartition("-")
71
72 yield Message.Url, url, file
73
74 def fetch_album(self, album_id):
75 url = f"{self.root}/api/album/get/{album_id}"
76 data = self.request_json(url)
77
78 return data["files"], {
79 "album_id" : self.album_id,
80 "album_name": text.unescape(data["title"]),
81 "count" : data["count"],
82 }