| 1 |
|
| 2 |
|
| 3 |
n |
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
"""Extractors for https://www.2chan.net/""" |
| 10 |
|
| 11 |
from .common import Extractor, Message |
| 12 |
from .. import text |
| 13 |
|
| 14 |
|
| 15 |
class _2chanThreadExtractor(Extractor): |
| 16 |
"""Extractor for 2chan threads""" |
| 17 |
category = "2chan" |
| 18 |
subcategory = "thread" |
| 19 |
directory_fmt = ("{category}", "{board_name}", "{thread}") |
| 20 |
filename_fmt = "{tim}.{extension}" |
| 21 |
archive_fmt = "{board}_{thread}_{tim}" |
| 22 |
pattern = r"(?:https?://)?([\w-]+)\.2chan\.net/([^/?#]+)/res/(\d+)" |
| 23 |
example = "https://dec.2chan.net/12/res/12345.htm" |
| 24 |
|
| 25 |
def __init__(self, match): |
| 26 |
Extractor.__init__(self, match) |
| 27 |
self.server, self.board, self.thread = match.groups() |
| 28 |
|
| 29 |
def items(self): |
| 30 |
url = (f"https://{self.server}.2chan.net" |
| 31 |
f"/{self.board}/res/{self.thread}.htm") |
| 32 |
page = self.request(url).text |
| 33 |
data = self.metadata(page) |
| 34 |
yield Message.Directory, "", data |
| 35 |
for post in self.posts(page): |
| 36 |
if "filename" not in post: |
| 37 |
continue |
| 38 |
post.update(data) |
| 39 |
url = (f"https://{post['server']}.2chan.net" |
| 40 |
f"/{post['board']}/src/{post['filename']}") |
| 41 |
yield Message.Url, url, post |
| 42 |
|
| 43 |
def metadata(self, page): |
| 44 |
"""Collect metadata for extractor-job""" |
| 45 |
title, _, boardname = text.extr( |
| 46 |
page, "<title>", "</title>").rpartition(" - ") |
| 47 |
return { |
| 48 |
"server": self.server, |
| 49 |
"title": title, |
| 50 |
"board": self.board, |
| 51 |
"board_name": boardname[:-4], |
| 52 |
"thread": self.thread, |
| 53 |
} |
| 54 |
|
| 55 |
def posts(self, page): |
| 56 |
"""Build a list of all post-objects""" |
| 57 |
page = text.extr( |
| 58 |
page, '<div class="thre"', '<div style="clear:left"></div>') |
| 59 |
return [ |
| 60 |
self.parse(post) |
| 61 |
for post in page.split('<table border=0>') |
| 62 |
] |
| 63 |
|
| 64 |
def parse(self, post): |
| 65 |
"""Build post-object by extracting data from an HTML post""" |
| 66 |
data = self._extract_post(post) |
| 67 |
if data["name"]: |
| 68 |
data["name"] = data["name"].strip() |
| 69 |
path = text.extr(post, '<a href="/', '"') |
| 70 |
if path and not path.startswith("bin/jump"): |
| 71 |
self._extract_image(post, data) |
| 72 |
data["tim"], _, data["extension"] = data["filename"].partition(".") |
| 73 |
data["time"] = data["tim"][:-3] |
| 74 |
data["ext"] = "." + data["extension"] |
| 75 |
return data |
| 76 |
|
| 77 |
def _extract_post(self, post): |
| 78 |
return text.extract_all(post, ( |
| 79 |
("post", 'class="csb">' , '<'), |
| 80 |
("name", 'class="cnm">' , '<'), |
| 81 |
("now" , 'class="cnw">' , '<'), |
| 82 |
("no" , 'class="cno">No.', '<'), |
| 83 |
(None , '<blockquote', ''), |
| 84 |
("com" , '>', '</blockquote>'), |
| 85 |
))[0] |
| 86 |
|
| 87 |
def _extract_image(self, post, data): |
| 88 |
text.extract_all(post, ( |
| 89 |
(None , '_blank', ''), |
| 90 |
("filename", '>', '<'), |
| 91 |
("fsize" , '(', ' '), |
| 92 |
), 0, data) |