Debian Codegraph @

gallery_dl/extractor/jschan.py

raw
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 jschan Imageboards"""
8
9 from .common import BaseExtractor, Message
10 from .. import text
11 import itertools
12
13
14 class JschanExtractor(BaseExtractor):
15 basecategory = "jschan"
16
17
18 BASE_PATTERN = JschanExtractor.update({
19 "94chan": {
20 "root": "https://94chan.org",
21 "pattern": r"94chan\.org"
22 }
23 })
24
25
26 class JschanThreadExtractor(JschanExtractor):
27 """Extractor for jschan threads"""
28 subcategory = "thread"
29 directory_fmt = ("{category}", "{board}",
30 "{threadId} {subject|nomarkup[:50]}")
31 filename_fmt = "{postId}{num:?-//} {filename}.{extension}"
32 archive_fmt = "{board}_{postId}_{num}"
33 pattern = BASE_PATTERN + r"/([^/?#]+)/thread/(\d+)\.html"
34 example = "https://94chan.org/a/thread/12345.html"
35
36 def items(self):
37 url = f"{self.root}/{self.groups[-2]}/thread/{self.groups[-1]}.json"
38 thread = self.request_json(url)
39 thread["threadId"] = thread["postId"]
40 posts = thread.pop("replies", ())
41
42 yield Message.Directory, "", thread
43 for post in itertools.chain((thread,), posts):
44 if files := post.pop("files", ()):
45 thread.update(post)
46 thread["count"] = len(files)
47 for num, file in enumerate(files):
48 url = f"{self.root}/file/{file['filename']}"
49 file.update(thread)
50 file["num"] = num
51 file["siteFilename"] = file["filename"]
52 text.nameext_from_url(file["originalFilename"], file)
53 yield Message.Url, url, file
54
55
56 class JschanBoardExtractor(JschanExtractor):
57 """Extractor for jschan boards"""
58 subcategory = "board"
59 pattern = (BASE_PATTERN + r"/([^/?#]+)"
60 r"(?:/index\.html|/catalog\.html|/\d+\.html|/?$)")
61 example = "https://94chan.org/a/"
62
63 def items(self):
64 board = self.groups[-1]
65 url = f"{self.root}/{board}/catalog.json"
66 for thread in self.request_json(url):
67 url = f"{self.root}/{board}/thread/{thread['postId']}.html"
68 thread["_extractor"] = JschanThreadExtractor
69 yield Message.Queue, url, thread