| 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 https://4archive.org/""" |
| 8 |
|
| 9 |
from .common import Extractor, Message |
| 10 |
from .. import text, dt |
| 11 |
|
| 12 |
|
| 13 |
class _4archiveThreadExtractor(Extractor): |
| 14 |
"""Extractor for 4archive threads""" |
| 15 |
category = "4archive" |
| 16 |
subcategory = "thread" |
| 17 |
directory_fmt = ("{category}", "{board}", "{thread} {title}") |
| 18 |
filename_fmt = "{no} {filename}.{extension}" |
| 19 |
archive_fmt = "{board}_{thread}_{no}" |
| 20 |
root = "https://4archive.org" |
| 21 |
referer = False |
| 22 |
pattern = r"(?:https?://)?4archive\.org/board/([^/?#]+)/thread/(\d+)" |
| 23 |
example = "https://4archive.org/board/a/thread/12345/" |
| 24 |
|
| 25 |
def __init__(self, match): |
| 26 |
Extractor.__init__(self, match) |
| 27 |
self.board, self.thread = match.groups() |
| 28 |
|
| 29 |
def items(self): |
| 30 |
url = f"{self.root}/board/{self.board}/thread/{self.thread}" |
| 31 |
page = self.request(url).text |
| 32 |
data = self.metadata(page) |
| 33 |
posts = self.posts(page) |
| 34 |
|
| 35 |
if not data["title"]: |
| 36 |
data["title"] = posts[0]["com"][:50] |
| 37 |
|
| 38 |
for post in posts: |
| 39 |
post.update(data) |
| 40 |
post["time"] = int(dt.to_ts(post["date"])) |
| 41 |
yield Message.Directory, "", post |
| 42 |
if "url" in post: |
| 43 |
yield Message.Url, post["url"], text.nameext_from_url( |
| 44 |
post["filename"], post) |
| 45 |
|
| 46 |
def metadata(self, page): |
| 47 |
return { |
| 48 |
"board" : self.board, |
| 49 |
"thread": text.parse_int(self.thread), |
| 50 |
"title" : text.unescape(text.extr( |
| 51 |
page, 'class="subject">', "</span>")) |
| 52 |
} |
| 53 |
|
| 54 |
def posts(self, page): |
| 55 |
return [ |
| 56 |
self.parse(post) |
| 57 |
for post in page.split('class="postContainer')[1:] |
| 58 |
] |
| 59 |
|
| 60 |
def parse(self, post): |
| 61 |
extr = text.extract_from(post) |
| 62 |
data = { |
| 63 |
"name": extr('class="name">', "</span>"), |
| 64 |
"date": self.parse_datetime_iso( |
| 65 |
(extr('class="dateTime">', "<") or |
| 66 |
extr('class="dateTime postNum" >', "<")).strip()), |
| 67 |
"no" : text.parse_int(extr(">Post No.", "<")), |
| 68 |
} |
| 69 |
if 'class="file"' in post: |
| 70 |
extr('class="fileText"', ">File: <a") |
| 71 |
data.update({ |
| 72 |
"url" : extr('href="', '"'), |
| 73 |
"filename": extr('alt="Image: ', '"'), |
| 74 |
"size" : text.parse_bytes(extr(" (", ", ")[:-1]), |
| 75 |
"width" : text.parse_int(extr("", "x")), |
| 76 |
"height" : text.parse_int(extr("", "px")), |
| 77 |
}) |
| 78 |
extr("<blockquote ", "") |
| 79 |
data["com"] = text.unescape(text.remove_html( |
| 80 |
extr(">", "</blockquote>"))) |
| 81 |
return data |
| 82 |
|
| 83 |
|
| 84 |
class _4archiveBoardExtractor(Extractor): |
| 85 |
"""Extractor for 4archive boards""" |
| 86 |
category = "4archive" |
| 87 |
subcategory = "board" |
| 88 |
root = "https://4archive.org" |
| 89 |
pattern = r"(?:https?://)?4archive\.org/board/([^/?#]+)(?:/(\d+))?/?$" |
| 90 |
example = "https://4archive.org/board/a/" |
| 91 |
|
| 92 |
def __init__(self, match): |
| 93 |
Extractor.__init__(self, match) |
| 94 |
self.board = match[1] |
| 95 |
self.num = text.parse_int(match[2], 1) |
| 96 |
|
| 97 |
def items(self): |
| 98 |
data = {"_extractor": _4archiveThreadExtractor} |
| 99 |
while True: |
| 100 |
url = f"{self.root}/board/{self.board}/{self.num}" |
| 101 |
page = self.request(url).text |
| 102 |
if 'class="thread"' not in page: |
| 103 |
return |
| 104 |
for thread in text.extract_iter(page, 'class="thread" id="t', '"'): |
| 105 |
url = f"{self.root}/board/{self.board}/thread/{thread}" |
| 106 |
yield Message.Queue, url, data |
| 107 |
self.num += 1 |