| 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 vichan imageboards""" |
| 10 |
|
| 11 |
from .common import BaseExtractor, Message |
| 12 |
from .. import text |
| 13 |
|
| 14 |
|
| 15 |
class VichanExtractor(BaseExtractor): |
| 16 |
"""Base class for vichan extractors""" |
| 17 |
basecategory = "vichan" |
| 18 |
|
| 19 |
|
| 20 |
BASE_PATTERN = VichanExtractor.update({ |
| 21 |
"8kun": { |
| 22 |
"root": "https://8kun.top", |
| 23 |
"pattern": r"8kun\.top", |
| 24 |
}, |
| 25 |
"smugloli": { |
| 26 |
"root": None, |
| 27 |
"pattern": r"smuglo(?:\.li|li\.net)", |
| 28 |
}, |
| 29 |
}) |
| 30 |
|
| 31 |
|
| 32 |
class VichanThreadExtractor(VichanExtractor): |
| 33 |
"""Extractor for vichan threads""" |
| 34 |
subcategory = "thread" |
| 35 |
directory_fmt = ("{category}", "{board}", "{thread} {title}") |
| 36 |
filename_fmt = "{time}{num:?-//} {filename}.{extension}" |
| 37 |
archive_fmt = "{board}_{thread}_{tim}" |
| 38 |
pattern = BASE_PATTERN + r"/([^/?#]+)/res/(\d+)" |
| 39 |
example = "https://8kun.top/a/res/12345.html" |
| 40 |
|
| 41 |
def items(self): |
| 42 |
board = self.groups[-2] |
| 43 |
thread = self.groups[-1] |
| 44 |
url = f"{self.root}/{board}/res/{thread}.json" |
| 45 |
posts = self.request_json(url)["posts"] |
| 46 |
|
| 47 |
title = posts[0].get("sub") or text.remove_html(posts[0]["com"]) |
| 48 |
process = (self._process_8kun if self.category == "8kun" else |
| 49 |
self._process) |
| 50 |
data = { |
| 51 |
"board" : board, |
| 52 |
"thread": thread, |
| 53 |
"title" : text.unescape(title)[:50], |
| 54 |
"num" : 0, |
| 55 |
} |
| 56 |
|
| 57 |
yield Message.Directory, "", data |
| 58 |
for post in posts: |
| 59 |
if "filename" in post: |
| 60 |
yield process(post, data) |
| 61 |
if "extra_files" in post: |
| 62 |
for post["num"], filedata in enumerate( |
| 63 |
post["extra_files"], 1): |
| 64 |
yield process(post, filedata) |
| 65 |
|
| 66 |
def _process(self, post, data): |
| 67 |
post.update(data) |
| 68 |
ext = post["ext"] |
| 69 |
post["extension"] = ext[1:] |
| 70 |
post["url"] = url = \ |
| 71 |
f"{self.root}/{post['board']}/src/{post['tim']}{ext}" |
| 72 |
return Message.Url, url, post |
| 73 |
|
| 74 |
def _process_8kun(self, post, data): |
| 75 |
post.update(data) |
| 76 |
ext = post["ext"] |
| 77 |
tim = post["tim"] |
| 78 |
|
| 79 |
if len(tim) > 16: |
| 80 |
url = f"https://media.128ducks.com/file_store/{tim}{ext}" |
| 81 |
else: |
| 82 |
url = f"https://media.128ducks.com/{post['board']}/src/{tim}{ext}" |
| 83 |
|
| 84 |
post["url"] = url |
| 85 |
post["extension"] = ext[1:] |
| 86 |
return Message.Url, url, post |
| 87 |
|
| 88 |
|
| 89 |
class VichanBoardExtractor(VichanExtractor): |
| 90 |
"""Extractor for vichan boards""" |
| 91 |
subcategory = "board" |
| 92 |
pattern = BASE_PATTERN + r"/([^/?#]+)(?:/index|/catalog|/\d+|/?$)" |
| 93 |
example = "https://8kun.top/a/" |
| 94 |
|
| 95 |
def items(self): |
| 96 |
board = self.groups[-1] |
| 97 |
url = f"{self.root}/{board}/threads.json" |
| 98 |
threads = self.request_json(url) |
| 99 |
|
| 100 |
for page in threads: |
| 101 |
for thread in page["threads"]: |
| 102 |
url = f"{self.root}/{board}/res/{thread['no']}.html" |
| 103 |
thread["page"] = page["page"] |
| 104 |
thread["_extractor"] = VichanThreadExtractor |
| 105 |
yield Message.Queue, url, thread |