| 1 |
# -*- coding: utf-8 -*- |
| 2 |
|
| 3 |
# Copyright 2022-2025 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 2chen boards""" |
| 10 |
|
| 11 |
from .common import BaseExtractor, Message |
| 12 |
from .. import text |
| 13 |
|
| 14 |
|
| 15 |
class _2chenExtractor(BaseExtractor): |
| 16 |
basecategory = "2chen" |
| 17 |
|
| 18 |
|
| 19 |
BASE_PATTERN = _2chenExtractor.update({ |
| 20 |
"sturdychan": { |
| 21 |
"root": "https://sturdychan.help", |
| 22 |
"pattern": r"(?:sturdychan\.help|2chen\.(?:moe|club))", |
| 23 |
}, |
| 24 |
"schan": { |
| 25 |
"root": "https://schan.help/", |
| 26 |
"pattern": r"schan\.help", |
| 27 |
}, |
| 28 |
}) |
| 29 |
|
| 30 |
|
| 31 |
class _2chenThreadExtractor(_2chenExtractor): |
| 32 |
"""Extractor for 2chen threads""" |
| 33 |
subcategory = "thread" |
| 34 |
directory_fmt = ("{category}", "{board}", "{thread} {title}") |
| 35 |
filename_fmt = "{time} {filename}.{extension}" |
| 36 |
archive_fmt = "{board}_{thread}_{no}_{time}" |
| 37 |
pattern = BASE_PATTERN + r"/([^/?#]+)/(\d+)" |
| 38 |
example = "https://sturdychan.help/a/12345/" |
| 39 |
|
| 40 |
def items(self): |
| 41 |
board = self.groups[-2] |
| 42 |
thread = self.kwdict["thread"] = self.groups[-1] |
| 43 |
url = f"{self.root}/{board}/{thread}" |
| 44 |
page = self.request(url, encoding="utf-8", notfound=True).text |
| 45 |
|
| 46 |
self.kwdict["board"], pos = text.extract( |
| 47 |
page, 'class="board">/', '/<') |
| 48 |
self.kwdict["title"] = text.unescape(text.extract( |
| 49 |
page, "<h3>", "</h3>", pos)[0]) |
| 50 |
|
| 51 |
yield Message.Directory, "", {} |
| 52 |
for post in self.posts(page): |
| 53 |
url = post["url"] |
| 54 |
if not url: |
| 55 |
continue |
| 56 |
if url[0] == "/": |
| 57 |
url = self.root + url |
| 58 |
post["url"] = url = url.partition("?")[0] |
| 59 |
|
| 60 |
post["time"] = text.parse_int(post["date"].timestamp()) |
| 61 |
yield Message.Url, url, text.nameext_from_url( |
| 62 |
post["filename"], post) |
| 63 |
|
| 64 |
def posts(self, page): |
| 65 |
"""Return iterable with relevant posts""" |
| 66 |
return map(self.parse, text.extract_iter( |
| 67 |
page, 'class="glass media', '</article>')) |
| 68 |
|
| 69 |
def parse(self, post): |
| 70 |
extr = text.extract_from(post) |
| 71 |
return { |
| 72 |
"name" : text.unescape(extr("<span>", "</span>")), |
| 73 |
"date" : self.parse_datetime( |
| 74 |
extr("<time", "<").partition(">")[2], |
| 75 |
"%d %b %Y (%a) %H:%M:%S" |
| 76 |
), |
| 77 |
"no" : extr('href="#p', '"'), |
| 78 |
"filename": text.unescape(extr('download="', '"')), |
| 79 |
"url" : text.extr(extr("<figure>", "</"), 'href="', '"'), |
| 80 |
"hash" : extr('data-hash="', '"'), |
| 81 |
} |
| 82 |
|
| 83 |
|
| 84 |
class _2chenBoardExtractor(_2chenExtractor): |
| 85 |
"""Extractor for 2chen boards""" |
| 86 |
subcategory = "board" |
| 87 |
pattern = BASE_PATTERN + r"/([^/?#]+)(?:/catalog|/?$)" |
| 88 |
example = "https://sturdychan.help/a/" |
| 89 |
|
| 90 |
def items(self): |
| 91 |
url = f"{self.root}/{self.groups[-1]}/catalog" |
| 92 |
page = self.request(url, notfound=True).text |
| 93 |
data = {"_extractor": _2chenThreadExtractor} |
| 94 |
for thread in text.extract_iter( |
| 95 |
page, '<figure><a href="', '"'): |
| 96 |
yield Message.Queue, self.root + thread, data |