Debian Codegraph @

gallery_dl/extractor/cyberdrop.py

raw symbols SCIP
1 # -*- coding: utf-8 -*-
2
3 # This program is free software; you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License version 2 as
5 # published by the Free Software Foundation.
6
7 """Extractors for https://cyberdrop.cr/"""
8
9 from . import lolisafe
10 from .common import Message
11 from .. import text
12
13 BASE_PATTERN = r"(?:https?://)?(?:www\.)?cyberdrop\.(?:cr|me|to)"
14
15
16 class CyberdropAlbumExtractor(lolisafe.LolisafeAlbumExtractor):
17 """Extractor for cyberdrop albums"""
18 category = "cyberdrop"
19 root = "https://cyberdrop.cr"
20 root_api = "https://api.cyberdrop.cr"
21 pattern = BASE_PATTERN + r"/a/([^/?#]+)"
22 example = "https://cyberdrop.cr/a/ID"
23
24 def items(self):
25 files, data = self.fetch_album(self.album_id)
26
27 yield Message.Directory, "", data
28 for data["num"], file in enumerate(files, 1):
29 file.update(data)
30 text.nameext_from_url(file["name"], file)
31 file["name"], sep, file["id"] = file["filename"].rpartition("-")
32 yield Message.Url, file["url"], file
33
34 def fetch_album(self, album_id):
35 url = f"{self.root}/a/{album_id}"
36 page = self.request(url).text
37 extr = text.extract_from(page)
38
39 desc = extr('property="og:description" content="', '"')
40 if desc.startswith("A privacy-focused censorship-resistant file "
41 "sharing platform free for everyone."):
42 desc = ""
43 extr('id="title"', "")
44
45 album = {
46 "album_id" : album_id,
47 "album_name" : text.unescape(extr('title="', '"')),
48 "album_size" : text.parse_bytes(extr(
49 '<p class="title">', "B")),
50 "date" : self.parse_datetime(extr(
51 '<p class="title">', '<'), "%d.%m.%Y"),
52 "description": text.unescape(text.unescape( # double
53 desc.rpartition(" [R")[0])),
54 }
55
56 file_ids = list(text.extract_iter(page, 'id="file" href="/f/', '"'))
57 album["count"] = len(file_ids)
58 return self._extract_files(file_ids), album
59
60 def _extract_files(self, file_ids):
61 for file_id in file_ids:
62 try:
63 url = f"{self.root_api}/api/file/info/{file_id}"
64 file = self.request_json(url)
65 auth = self.request_json(file["auth_url"])
66 file["url"] = auth["url"]
67 except Exception as exc:
68 self.log.warning("%s (%s: %s)",
69 file_id, exc.__class__.__name__, exc)
70 continue
71
72 yield file
73
74
75 class CyberdropMediaExtractor(CyberdropAlbumExtractor):
76 """Extractor for cyberdrop media links"""
77 subcategory = "media"
78 directory_fmt = ("{category}",)
79 pattern = BASE_PATTERN + r"/[ef]/([^/?#]+)"
80 example = "https://cyberdrop.cr/f/ID"
81
82 def fetch_album(self, album_id):
83 return self._extract_files((album_id,)), {
84 "album_id" : "",
85 "album_name" : "",
86 "album_size" : -1,
87 "description": "",
88 "count" : 1,
89 }