Debian Codegraph @

gallery_dl/extractor/fileditchfiles.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://fileditchfiles.st/"""
8
9 from .common import Extractor, Message
10 from .. import text, util
11 import hashlib
12
13 BASE_PATTERN = r"(?:https?://)?(?:www\.)?fileditchfiles\.st"
14
15
16 class FileditchfilesExtractor(Extractor):
17 """Base class for fileditchfiles extractors"""
18 category = "fileditchfiles"
19 root = "https://fileditchfiles.st"
20
21
22 class FileditchfilesFileExtractor(FileditchfilesExtractor):
23 """Extractor for fileditchfiles files"""
24 subcategory = "file"
25 filename_fmt = "{id}.{extension}"
26 archive_fmt = "{id}"
27 pattern = BASE_PATTERN + r"/[^/?#]+/([^/?#]+)/([^/?#]+)"
28 example = "https://fileditchfiles.st/xyz01/ID/SLUG"
29
30 def items(self):
31 url = text.ensure_http_scheme(self.url)
32 page = self.request(url).text
33
34 if (pos := page.find('class="verifying-overlay"')) >= 0:
35 data = text.extract_all(page, (
36 ("orig_ref", '"orig_ref" value="', '"'),
37 ("pow_challenge", 'value="', '"'),
38 ("pow_ts" , 'value="', '"'),
39 ("pow_diff", 'value="', '"'),
40 ("pow_sig" , 'value="', '"'),
41 ), pos)[0]
42 diff = text.parse_int(data["pow_diff"])
43 data["pow_nonce"] = _nonce(data["pow_challenge"], diff)
44 page = self.request(url, method="POST", data=data).text
45
46 extr = text.extract_from(page)
47 file = {
48 "id": self.groups[0],
49 "slug": self.groups[1],
50 "path": extr('class="path">', "</span>"),
51 "size": text.parse_bytes(extr(
52 'class="size">', "</span>").replace(" ", "")[:-1]),
53 "downloads": text.parse_int(extr(
54 'class="dlcount">⬇ ', " downloads</span>"))
55 }
56 yield Message.Directory, "", file
57 url = "".join(util.json_loads(extr("var u = ", ".join(")))
58 yield Message.Url, url, text.nameext_from_url(url, file)
59
60
61 class FileditchfilesShorturlExtractor(FileditchfilesExtractor):
62 """Extractor for short file URls"""
63 subcategory = "shorturl"
64 pattern = r"(?:https?://)?(?:www\.)?theditch\.st/([^/?#]+)"
65 example = "https://theditch.st/ID"
66
67 def items(self):
68 page = self.request(text.ensure_http_scheme(self.url)).text
69 data = {
70 "id": self.groups[0],
71 "_extractor": FileditchfilesFileExtractor,
72 }
73 yield Message.Queue, text.extr(page, 'var u = "', '"'), data
74
75
76 def _nonce(challenge, diff):
77 def leading_zero_bits(data, need):
78 full = need >> 3
79 rem = need & 7
80 for i in range(0, full):
81 if data[i] != 0:
82 return False
83 if rem:
84 if (data[full] & ((0xFF << (8 - rem)) & 0xFF)) != 0:
85 return False
86 return True
87
88 prefix = challenge + ":"
89 nonce = 0
90 while True:
91 digest = hashlib.sha256((prefix + str(nonce)).encode()).digest() + b"0"
92 if leading_zero_bits(digest, diff):
93 return nonce
94 nonce += 1