2022-10-04 22:18:13 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
# 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.
|
|
|
|
|
2023-04-19 13:54:44 +02:00
|
|
|
"""Extractors for https://sturdychan.help/"""
|
2022-10-04 22:18:13 +02:00
|
|
|
|
|
|
|
from .common import Extractor, Message
|
|
|
|
from .. import text
|
|
|
|
|
2023-04-19 13:54:44 +02:00
|
|
|
BASE_PATTERN = r"(?:https?://)?(?:sturdychan.help|2chen\.(?:moe|club))"
|
|
|
|
|
2022-10-04 22:18:13 +02:00
|
|
|
|
|
|
|
class _2chenThreadExtractor(Extractor):
|
|
|
|
"""Extractor for 2chen threads"""
|
|
|
|
category = "2chen"
|
|
|
|
subcategory = "thread"
|
2023-04-19 13:54:44 +02:00
|
|
|
root = "https://sturdychan.help"
|
2022-10-04 22:18:13 +02:00
|
|
|
directory_fmt = ("{category}", "{board}", "{thread} {title}")
|
|
|
|
filename_fmt = "{time} {filename}.{extension}"
|
2022-12-04 16:19:36 +01:00
|
|
|
archive_fmt = "{board}_{thread}_{hash}_{time}"
|
2023-04-19 13:54:44 +02:00
|
|
|
pattern = BASE_PATTERN + r"/([^/?#]+)/(\d+)"
|
2022-10-04 22:18:13 +02:00
|
|
|
test = (
|
2023-04-19 13:54:44 +02:00
|
|
|
("https://sturdychan.help/tv/268929", {
|
|
|
|
"pattern": r"https://sturdychan\.help/assets/images"
|
|
|
|
r"/src/\w{40}\.\w+$",
|
2022-12-04 16:19:36 +01:00
|
|
|
"count": ">= 179",
|
2023-04-19 13:54:44 +02:00
|
|
|
"keyword": {
|
|
|
|
"board": "tv",
|
|
|
|
"date": "type:datetime",
|
|
|
|
"hash": r"re:[0-9a-f]{40}",
|
|
|
|
"name": "Anonymous",
|
|
|
|
"no": r"re:\d+",
|
|
|
|
"thread": "268929",
|
|
|
|
"time": int,
|
|
|
|
"title": "「/ttg/ #118: 🇧🇷 edition」",
|
|
|
|
"url": str,
|
|
|
|
},
|
2022-10-04 22:18:13 +02:00
|
|
|
}),
|
2023-04-19 13:54:44 +02:00
|
|
|
("https://2chen.club/tv/1"),
|
2022-12-04 16:19:36 +01:00
|
|
|
("https://2chen.moe/jp/303786"),
|
2022-10-04 22:18:13 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
def __init__(self, match):
|
|
|
|
Extractor.__init__(self, match)
|
|
|
|
self.board, self.thread = match.groups()
|
|
|
|
|
|
|
|
def items(self):
|
|
|
|
url = "{}/{}/{}".format(self.root, self.board, self.thread)
|
2022-12-04 16:19:36 +01:00
|
|
|
page = self.request(url, encoding="utf-8", notfound="thread").text
|
2022-10-04 22:18:13 +02:00
|
|
|
data = self.metadata(page)
|
|
|
|
yield Message.Directory, data
|
2022-12-15 18:05:32 +01:00
|
|
|
|
2022-10-04 22:18:13 +02:00
|
|
|
for post in self.posts(page):
|
2022-12-15 18:05:32 +01:00
|
|
|
|
|
|
|
url = post["url"]
|
|
|
|
if not url:
|
2022-10-04 22:18:13 +02:00
|
|
|
continue
|
2022-12-15 18:05:32 +01:00
|
|
|
if url[0] == "/":
|
|
|
|
url = self.root + url
|
|
|
|
post["url"] = url = url.partition("?")[0]
|
|
|
|
|
2022-10-04 22:18:13 +02:00
|
|
|
post.update(data)
|
|
|
|
post["time"] = text.parse_int(post["date"].timestamp())
|
2022-12-15 18:05:32 +01:00
|
|
|
yield Message.Url, url, text.nameext_from_url(
|
2022-10-04 22:18:13 +02:00
|
|
|
post["filename"], post)
|
|
|
|
|
|
|
|
def metadata(self, page):
|
|
|
|
board, pos = text.extract(page, 'class="board">/', '/<')
|
|
|
|
title = text.extract(page, "<h3>", "</h3>", pos)[0]
|
|
|
|
return {
|
|
|
|
"board" : board,
|
|
|
|
"thread": self.thread,
|
|
|
|
"title" : text.unescape(title),
|
|
|
|
}
|
|
|
|
|
|
|
|
def posts(self, page):
|
|
|
|
"""Return iterable with relevant posts"""
|
|
|
|
return map(self.parse, text.extract_iter(
|
|
|
|
page, 'class="glass media', '</article>'))
|
|
|
|
|
|
|
|
def parse(self, post):
|
|
|
|
extr = text.extract_from(post)
|
|
|
|
return {
|
|
|
|
"name" : text.unescape(extr("<span>", "</span>")),
|
|
|
|
"date" : text.parse_datetime(
|
|
|
|
extr("<time", "<").partition(">")[2],
|
|
|
|
"%d %b %Y (%a) %H:%M:%S"
|
|
|
|
),
|
|
|
|
"no" : extr('href="#p', '"'),
|
2022-12-04 16:19:36 +01:00
|
|
|
"url" : extr('</a><a href="', '"'),
|
2022-10-04 22:18:13 +02:00
|
|
|
"filename": text.unescape(extr('download="', '"')),
|
|
|
|
"hash" : extr('data-hash="', '"'),
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
class _2chenBoardExtractor(Extractor):
|
|
|
|
"""Extractor for 2chen boards"""
|
|
|
|
category = "2chen"
|
|
|
|
subcategory = "board"
|
2023-04-19 13:54:44 +02:00
|
|
|
root = "https://sturdychan.help"
|
|
|
|
pattern = BASE_PATTERN + r"/([^/?#]+)(?:/catalog|/?$)"
|
2022-10-04 22:18:13 +02:00
|
|
|
test = (
|
2023-04-19 13:54:44 +02:00
|
|
|
("https://sturdychan.help/co/", {
|
2022-10-04 22:18:13 +02:00
|
|
|
"pattern": _2chenThreadExtractor.pattern
|
|
|
|
}),
|
|
|
|
("https://2chen.moe/co"),
|
2022-12-15 17:51:02 +01:00
|
|
|
("https://2chen.club/tv"),
|
|
|
|
("https://2chen.moe/co/catalog"),
|
2022-10-04 22:18:13 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
def __init__(self, match):
|
|
|
|
Extractor.__init__(self, match)
|
|
|
|
self.board = match.group(1)
|
|
|
|
|
|
|
|
def items(self):
|
|
|
|
url = "{}/{}/catalog".format(self.root, self.board)
|
2022-12-04 16:19:36 +01:00
|
|
|
page = self.request(url, notfound="board").text
|
2022-10-04 22:18:13 +02:00
|
|
|
data = {"_extractor": _2chenThreadExtractor}
|
|
|
|
for thread in text.extract_iter(
|
|
|
|
page, '<figure><a href="', '"'):
|
|
|
|
yield Message.Queue, self.root + thread, data
|