| 1 |
# -*- coding: utf-8 -*- |
| 2 |
|
| 3 |
# This program is free software; you can redistribute it and/or modify |
| 4 |
# it under the terms of the GNU General Public License version 2 as |
| 5 |
# published by the Free Software Foundation. |
| 6 |
|
| 7 |
"""Extractors for LynxChan Imageboards""" |
| 8 |
|
| 9 |
from .common import BaseExtractor, Message |
| 10 |
from .. import text |
| 11 |
import itertools |
| 12 |
|
| 13 |
|
| 14 |
class LynxchanExtractor(BaseExtractor): |
| 15 |
"""Base class for LynxChan extractors""" |
| 16 |
basecategory = "lynxchan" |
| 17 |
|
| 18 |
|
| 19 |
BASE_PATTERN = LynxchanExtractor.update({ |
| 20 |
"bbw-chan": { |
| 21 |
"root": "https://bbw-chan.link", |
| 22 |
"pattern": r"bbw-chan\.(?:link|nl)", |
| 23 |
}, |
| 24 |
"kohlchan": { |
| 25 |
"root": "https://kohlchan.net", |
| 26 |
"pattern": r"kohlchan\.net", |
| 27 |
}, |
| 28 |
"endchan": { |
| 29 |
"root": None, |
| 30 |
"pattern": r"endchan\.(?:org|net|gg)", |
| 31 |
}, |
| 32 |
}) |
| 33 |
|
| 34 |
|
| 35 |
class LynxchanThreadExtractor(LynxchanExtractor): |
| 36 |
"""Extractor for LynxChan threads""" |
| 37 |
subcategory = "thread" |
| 38 |
directory_fmt = ("{category}", "{boardUri}", |
| 39 |
"{threadId} {subject|message[:50]}") |
| 40 |
filename_fmt = "{postId}{num:?-//} {filename}.{extension}" |
| 41 |
archive_fmt = "{boardUri}_{postId}_{num}" |
| 42 |
pattern = BASE_PATTERN + r"/([^/?#]+)/res/(\d+)" |
| 43 |
example = "https://endchan.org/a/res/12345.html" |
| 44 |
|
| 45 |
def items(self): |
| 46 |
url = f"{self.root}/{self.groups[-2]}/res/{self.groups[-1]}.json" |
| 47 |
thread = self.request_json(url) |
| 48 |
thread["postId"] = thread["threadId"] |
| 49 |
posts = thread.pop("posts", ()) |
| 50 |
|
| 51 |
yield Message.Directory, "", thread |
| 52 |
for post in itertools.chain((thread,), posts): |
| 53 |
if files := post.pop("files", ()): |
| 54 |
thread.update(post) |
| 55 |
for num, file in enumerate(files): |
| 56 |
file.update(thread) |
| 57 |
file["num"] = num |
| 58 |
url = self.root + file["path"] |
| 59 |
text.nameext_from_url(file["originalName"], file) |
| 60 |
yield Message.Url, url, file |
| 61 |
|
| 62 |
|
| 63 |
class LynxchanBoardExtractor(LynxchanExtractor): |
| 64 |
"""Extractor for LynxChan boards""" |
| 65 |
subcategory = "board" |
| 66 |
pattern = BASE_PATTERN + r"/([^/?#]+)(?:/index|/catalog|/\d+|/?$)" |
| 67 |
example = "https://endchan.org/a/" |
| 68 |
|
| 69 |
def items(self): |
| 70 |
board = self.groups[-1] |
| 71 |
url = f"{self.root}/{board}/catalog.json" |
| 72 |
for thread in self.request_json(url): |
| 73 |
url = f"{self.root}/{board}/res/{thread['threadId']}.html" |
| 74 |
thread["_extractor"] = LynxchanThreadExtractor |
| 75 |
yield Message.Queue, url, thread |