# -*- coding: utf-8 -*- # Copyright 2026 Mike Fährmann # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License version 2 as # published by the Free Software Foundation. """Extractors for kokonotsuba imageboards""" from .common import BaseExtractor, Message from .. import text import itertools class KokonotsubaExtractor(BaseExtractor): """Base class for kokonotsuba extractors""" basecategory = "kokonotsuba" BASE_PATTERN = KokonotsubaExtractor.update({ "gurochan": { "root": None, "pattern": r"boards\.guro\.(?:st|cx)?", }, }) class KokonotsubaThreadExtractor(KokonotsubaExtractor): """Extractor for kokonotsuba threads""" subcategory = "thread" directory_fmt = ("{category}", "{board}", "{thread} {title}") filename_fmt = "{date} {file_name}.{extension}" archive_fmt = "{board}_{thread}_{file_id}" pattern = (BASE_PATTERN + r"/([^/?#]+)/" r"(?:uid:([^/?#]+)|koko\.php\?res=(\d+)(?:&page=(\d+))?)") example = "https://boards.guro.st/BOARD/koko.php?res=12345" def items(self): board = self.groups[-4] thread_uid = self.groups[-3] thread = self.groups[-2] pnum = text.parse_int(self.groups[-1], 1) if thread_uid is None: url = f"{self.root}/{board}/koko.php?res={thread}&page={pnum}" page = self.request(url).text thread_uid = text.extr(page, f'_{thread}" data-thread-uid="', '"') posts = self._pagination(board, thread_uid, pnum-1) op = next(posts) if thread is None: thread = op.get("parent_post_number") self.kwdict = { "board" : board, "thread" : thread, "thread_uid": thread_uid, "title" : op.get("subject") or op.get("comment", "")[:50], } base = f"{self.root}/{board}/src/" needle = '