| 1 |
|
| 2 |
|
| 3 |
n |
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
"""Extractors for https://warosu.org/""" |
| 10 |
|
| 11 |
from .common import Extractor, Message |
| 12 |
from .. import text |
| 13 |
|
| 14 |
|
| 15 |
class WarosuThreadExtractor(Extractor): |
| 16 |
"""Extractor for threads on warosu.org""" |
| 17 |
category = "warosu" |
| 18 |
subcategory = "thread" |
| 19 |
root = "https://warosu.org" |
| 20 |
directory_fmt = ("{category}", "{board}", "{thread} - {title}") |
| 21 |
filename_fmt = "{tim} {filename}.{extension}" |
| 22 |
archive_fmt = "{board}_{thread}_{tim}" |
| 23 |
pattern = r"(?:https?://)?(?:www\.)?warosu\.org/([^/]+)/thread/(\d+)" |
| 24 |
example = "https://warosu.org/a/thread/12345" |
| 25 |
|
| 26 |
def __init__(self, match): |
| 27 |
Extractor.__init__(self, match) |
| 28 |
self.board, self.thread = match.groups() |
| 29 |
|
| 30 |
def items(self): |
| 31 |
url = f"{self.root}/{self.board}/thread/{self.thread}" |
| 32 |
page = self.request(url).text |
| 33 |
data = self.metadata(page) |
| 34 |
posts = self.posts(page) |
| 35 |
|
| 36 |
if not data["title"]: |
| 37 |
data["title"] = text.unescape(text.remove_html( |
| 38 |
posts[0]["com"]))[:50] |
| 39 |
|
| 40 |
yield Message.Directory, "", data |
| 41 |
for post in posts: |
| 42 |
if "image" in post: |
| 43 |
for key in ("w", "h", "no", "time", "tim"): |
| 44 |
post[key] = text.parse_int(post[key]) |
| 45 |
dt = self.parse_timestamp(post["time"]) |
| 46 |
|
| 47 |
post["now"] = dt.strftime(f"%a, %b {dt.day}, %Y %H:%M:%S") |
| 48 |
post.update(data) |
| 49 |
yield Message.Url, post["image"], post |
| 50 |
|
| 51 |
def metadata(self, page): |
| 52 |
return { |
| 53 |
"board" : self.board, |
| 54 |
"thread" : self.thread, |
| 55 |
"board_name": text.extr( |
| 56 |
page, "<title>", "</title>").split(" - ")[1].strip(), |
| 57 |
"title" : text.unescape(text.extr( |
| 58 |
page, 'property="og:title" content="', '"')), |
| 59 |
} |
| 60 |
|
| 61 |
def posts(self, page): |
| 62 |
"""Build a list of all post objects""" |
| 63 |
page = text.extr(page, '<div class="content"', "</form>") |
| 64 |
needle = "<table>" |
| 65 |
return [self.parse(post) for post in page.split(needle)] |
| 66 |
|
| 67 |
def parse(self, post): |
| 68 |
"""Build post object by extracting data from an HTML post""" |
| 69 |
data = self._extract_post(post) |
| 70 |
if '<span class="fileinfo' in post and \ |
| 71 |
self._extract_image(post, data): |
| 72 |
part = data["image"].rpartition("/")[2] |
| 73 |
data["tim"], _, data["extension"] = part.partition(".") |
| 74 |
data["ext"] = "." + data["extension"] |
| 75 |
return data |
| 76 |
|
| 77 |
def _extract_post(self, post): |
| 78 |
extr = text.extract_from(post) |
| 79 |
return { |
| 80 |
"no" : extr('id="p', '"'), |
| 81 |
"name": extr('class="postername ">', "<").strip(), |
| 82 |
"time": extr('class="posttime" title="', '000"'), |
| 83 |
"com" : text.unescape(text.remove_html(extr( |
| 84 |
"<blockquote>", "</blockquote>").strip())), |
| 85 |
} |
| 86 |
|
| 87 |
def _extract_image(self, post, data): |
| 88 |
extr = text.extract_from(post) |
| 89 |
extr('<span class="fileinfo', "") |
| 90 |
data["fsize"] = extr("File: ", ", ") |
| 91 |
data["w"] = extr("", "x") |
| 92 |
data["h"] = extr("", ", ") |
| 93 |
data["filename"] = text.unquote(extr( |
| 94 |
"", "<").rstrip().rpartition(".")[0]) |
| 95 |
extr("<br", "") |
| 96 |
|
| 97 |
if url := extr("<a href=", ">"): |
| 98 |
url = url.strip("\"'") |
| 99 |
if url[0] == "/": |
| 100 |
data["image"] = self.root + url |
| 101 |
elif "warosu." not in url: |
| 102 |
return False |
| 103 |
else: |
| 104 |
data["image"] = url |
| 105 |
return True |
| 106 |
return False |