| 1 |
# -*- coding: utf-8 -*- |
| 2 |
|
| 3 |
# Copyright 2022-2026 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 https://toyhou.se/""" |
| 10 |
|
| 11 |
from .common import Extractor, Message |
| 12 |
from .. import text, util |
| 13 |
|
| 14 |
BASE_PATTERN = r"(?:https?://)?(?:www\.)?toyhou\.se" |
| 15 |
|
| 16 |
|
| 17 |
class ToyhouseExtractor(Extractor): |
| 18 |
"""Base class for toyhouse extractors""" |
| 19 |
category = "toyhouse" |
| 20 |
root = "https://toyhou.se" |
| 21 |
directory_fmt = ("{category}", "{user|artists!S}") |
| 22 |
archive_fmt = "{id}" |
| 23 |
|
| 24 |
def __init__(self, match): |
| 25 |
Extractor.__init__(self, match) |
| 26 |
self.user = match[1] |
| 27 |
self.offset = 0 |
| 28 |
|
| 29 |
def items(self): |
| 30 |
metadata = self.metadata() |
| 31 |
|
| 32 |
for post in util.advance(self.posts(), self.offset): |
| 33 |
if metadata: |
| 34 |
post.update(metadata) |
| 35 |
text.nameext_from_url(post["url"], post) |
| 36 |
post["id"], _, post["hash"] = post["filename"].partition("_") |
| 37 |
yield Message.Directory, "", post |
| 38 |
yield Message.Url, post["url"], post |
| 39 |
|
| 40 |
def posts(self): |
| 41 |
return () |
| 42 |
|
| 43 |
def metadata(self): |
| 44 |
return None |
| 45 |
|
| 46 |
def skip_files(self, num): |
| 47 |
self.offset += num |
| 48 |
return num |
| 49 |
|
| 50 |
def _parse_post(self, post, needle='<a href="'): |
| 51 |
extr = text.extract_from(post) |
| 52 |
data = { |
| 53 |
"url": extr(needle, '"'), |
| 54 |
"detail": "\n".join(text.split_html(extr( |
| 55 |
'class="image-detail', 'class="image-credit'))[2:-2]), |
| 56 |
"date": (v := extr('', "</div")) and |
| 57 |
self.parse_datetime( |
| 58 |
v[v.find('mb-1">')+6:], "%d %b %Y, %I:%M:%S %p"), |
| 59 |
"artists": text.split_html(extr( |
| 60 |
'<div class="artist-credit', |
| 61 |
'<div class="image-character'))[1:], |
| 62 |
"characters": text.split_html( |
| 63 |
extr('', 'class="image-') or |
| 64 |
extr('', 'id="footer"'))[2:-1], |
| 65 |
} |
| 66 |
|
| 67 |
url = data["url"] |
| 68 |
if "/watermarks/" in url: |
| 69 |
data["status"] = "watermark" |
| 70 |
elif "/thumbnails/" in url: |
| 71 |
data["status"] = "thumbnail" |
| 72 |
else: |
| 73 |
data["status"] = "original" |
| 74 |
|
| 75 |
return data |
| 76 |
|
| 77 |
def _pagination(self, path): |
| 78 |
url = self.root + path |
| 79 |
params = {"page": 1} |
| 80 |
|
| 81 |
while True: |
| 82 |
page = self.request(url, params=params).text |
| 83 |
|
| 84 |
cnt = 0 |
| 85 |
for post in text.extract_iter( |
| 86 |
page, '<li class="gallery-item">', '</li>'): |
| 87 |
cnt += 1 |
| 88 |
yield self._parse_post(post) |
| 89 |
|
| 90 |
if not cnt and params["page"] == 1: |
| 91 |
if self._accept_content_warning(page): |
| 92 |
continue |
| 93 |
return |
| 94 |
|
| 95 |
if cnt < 18: |
| 96 |
return |
| 97 |
params["page"] += 1 |
| 98 |
|
| 99 |
def _accept_content_warning(self, page): |
| 100 |
pos = page.find(' name="_token"') + 1 |
| 101 |
token, pos = text.extract(page, ' value="', '"', pos) |
| 102 |
user , pos = text.extract(page, ' value="', '"', pos) |
| 103 |
if not token or not user: |
| 104 |
return False |
| 105 |
|
| 106 |
data = {"_token": token, "user": user} |
| 107 |
self.request(self.root + "/~account/warnings/accept", |
| 108 |
method="POST", data=data, allow_redirects=False) |
| 109 |
return True |
| 110 |
|
| 111 |
|
| 112 |
class ToyhouseArtExtractor(ToyhouseExtractor): |
| 113 |
"""Extractor for artworks of a toyhouse user""" |
| 114 |
subcategory = "art" |
| 115 |
pattern = BASE_PATTERN + r"/([^/?#]+)/art" |
| 116 |
example = "https://www.toyhou.se/USER/art" |
| 117 |
|
| 118 |
def posts(self): |
| 119 |
return self._pagination(f"/{self.user}/art") |
| 120 |
|
| 121 |
def metadata(self): |
| 122 |
return {"user": self.user} |
| 123 |
|
| 124 |
|
| 125 |
class ToyhouseImageExtractor(ToyhouseExtractor): |
| 126 |
"""Extractor for individual toyhouse images""" |
| 127 |
subcategory = "image" |
| 128 |
pattern = (r"(?:https?://)?(?:" |
| 129 |
r"(?:www\.)?toyhou\.se/~images|" |
| 130 |
r"f\d+\.toyhou\.se/file/[^/?#]+/(?:image|watermark|thumbnail)s" |
| 131 |
r")/(\d+)") |
| 132 |
example = "https://toyhou.se/~images/12345" |
| 133 |
|
| 134 |
def posts(self): |
| 135 |
url = f"{self.root}/~images/{self.user}" |
| 136 |
return (self._parse_post( |
| 137 |
self.request(url).text, '<img class="mw-100" src="'),) |