Debian Codegraph @

gallery_dl/extractor/kokonotsuba.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 kokonotsuba imageboards"""
10
11 from .common import BaseExtractor, Message
12 from .. import text
13 import itertools
14
15
16 class KokonotsubaExtractor(BaseExtractor):
17 """Base class for kokonotsuba extractors"""
18 basecategory = "kokonotsuba"
19
20
21 BASE_PATTERN = KokonotsubaExtractor.update({
22 "gurochan": {
23 "root": None,
24 "pattern": r"boards\.guro\.(?:st|cx)?",
25 },
26 })
27
28
29 class KokonotsubaThreadExtractor(KokonotsubaExtractor):
30 """Extractor for kokonotsuba threads"""
31 subcategory = "thread"
32 directory_fmt = ("{category}", "{board}", "{thread} {title}")
33 filename_fmt = "{date} {file_name}.{extension}"
34 archive_fmt = "{board}_{thread}_{file_id}"
35 pattern = (BASE_PATTERN + r"/([^/?#]+)/"
36 r"(?:uid:([^/?#]+)|koko\.php\?res=(\d+)(?:&page=(\d+))?)")
37 example = "https://boards.guro.st/BOARD/koko.php?res=12345"
38
39 def items(self):
40 board = self.groups[-4]
41 thread_uid = self.groups[-3]
42 thread = self.groups[-2]
43 pnum = text.parse_int(self.groups[-1], 1)
44
45 if thread_uid is None:
46 url = f"{self.root}/{board}/koko.php?res={thread}&page={pnum}"
47 page = self.request(url).text
48 thread_uid = text.extr(page, f'_{thread}" data-thread-uid="', '"')
49
50 posts = self._pagination(board, thread_uid, pnum-1)
51 op = next(posts)
52
53 if thread is None:
54 thread = op.get("parent_post_number")
55 self.kwdict = {
56 "board" : board,
57 "thread" : thread,
58 "thread_uid": thread_uid,
59 "title" : op.get("subject") or op.get("comment", "")[:50],
60 }
61
62 base = f"{self.root}/{board}/src/"
63 needle = '<a href="' + base
64 for post in itertools.chain((op,), posts):
65 files = post.pop("attachments", ())
66 post["date"] = self.parse_datetime_iso(post.get("timestamp"))
67 post["count"] = len(files)
68 yield Message.Directory, "", post
69
70 if files: # assume 1 file per post
71 post.update(files[0])
72 name = text.extr(post["html"], needle, '"')
73 text.nameext_from_name(name, post)
74 yield Message.Url, base + name, post
75
76 def _pagination(self, board, thread, pnum=0):
77 url = f"{self.root}/{board}/koko.php"
78 params = {
79 "mode" : "module",
80 "load" : "postApi",
81 "pageName" : "thread",
82 "thread_uid": thread,
83 "page" : pnum,
84 }
85
86 op = True
87 while True:
88 data = self.request_json(url, params=params)
89
90 posts = data["posts"]
91 if op:
92 op = False
93 yield posts[0]
94 yield from posts[1:]
95
96 if data["post_count"] <= 500:
97 break
98 params["page"] += 1
99
100
101 class KokonotsubaBoardExtractor(KokonotsubaExtractor):
102 """Extractor for kokonotsuba boards"""
103 subcategory = "board"
104 pattern = BASE_PATTERN + r"/([^/?#]+)(?:/(?:koko\.php\?page=)?(\d+))?"
105 example = "https://boards.guro.st/art/"
106
107 def items(self):
108 board = self.groups[-2]
109 pnum = text.parse_int(self.groups[-1], 1)
110 base = f"{self.root}/{board}/uid:"
111 for thread in self._pagination(board, pnum-1):
112 thread["_extractor"] = KokonotsubaThreadExtractor
113 yield Message.Queue, base + thread['thread_uid'], thread
114
115 def _pagination(self, board, pnum=0):
116 url = f"{self.root}/{board}/koko.php"
117 params = {
118 "mode" : "module",
119 "load" : "postApi",
120 "pageName": "threads",
121 "page" : pnum,
122 }
123
124 while True:
125 data = self.request_json(url, params=params)
126
127 yield from data["threads"]
128
129 if data["thread_count"] < data["threads_per_page"]:
130 break
131 params["page"] += 1