| 1 |
# -*- coding: utf-8 -*- |
| 2 |
|
| 3 |
# Copyright 2025 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://sizebooru.com/""" |
| 10 |
|
| 11 |
from .booru import BooruExtractor |
| 12 |
from .. import text |
| 13 |
|
| 14 |
BASE_PATTERN = r"(?:https?://)?(?:www\.)?sizebooru\.com" |
| 15 |
|
| 16 |
|
| 17 |
class SizebooruExtractor(BooruExtractor): |
| 18 |
"""Base class for sizebooru extractors""" |
| 19 |
category = "sizebooru" |
| 20 |
root = "https://sizebooru.com" |
| 21 |
filename_fmt = "{id}.{extension}" |
| 22 |
archive_fmt = "{id}" |
| 23 |
page_start = 1 |
| 24 |
request_interval = (0.5, 1.5) |
| 25 |
|
| 26 |
def _init(self): |
| 27 |
if self.config("metadata", False): |
| 28 |
self._prepare = self._prepare_metadata |
| 29 |
|
| 30 |
def _file_url(self, post): |
| 31 |
post["file_url"] = url = f"{self.root}/Picture/{post['id']}" |
| 32 |
return url |
| 33 |
|
| 34 |
def _prepare(self, post): |
| 35 |
post_id = post["id"] |
| 36 |
post["id"] = text.parse_int(post_id) |
| 37 |
post["filename"] = post_id |
| 38 |
if not post["extension"]: |
| 39 |
post["extension"] = "jpg" |
| 40 |
|
| 41 |
def _prepare_metadata(self, post): |
| 42 |
post_id = post["id"] |
| 43 |
url = f"{self.root}/Details/{post_id}" |
| 44 |
extr = text.extract_from(self.request(url).text) |
| 45 |
|
| 46 |
post.update({ |
| 47 |
"id" : text.parse_int(post_id), |
| 48 |
"date" : self.parse_datetime( |
| 49 |
extr("<b>Posted Date:</b> ", "<"), "%m/%d/%Y"), |
| 50 |
"date_approved": self.parse_datetime( |
| 51 |
extr("<b>Approved Date:</b> ", "<"), "%m/%d/%Y"), |
| 52 |
"approver" : text.remove_html(extr("<b>Approved By:</b>", "</")), |
| 53 |
"uploader" : text.remove_html(extr("<b>Posted By:</b>", "</")), |
| 54 |
"artist" : None |
| 55 |
if (artist := extr("<b>Artist:</b> ", "</")) == "N/A" else # noqa: E131 E501 |
| 56 |
text.remove_html(artist), # noqa: E131 |
| 57 |
"views" : text.parse_int(extr("<b>Views:</b>", "<")), |
| 58 |
"source" : text.extr(extr( |
| 59 |
"<b>Source Link:</b>", "</"), ' href="', '"') or None, |
| 60 |
"tags" : text.split_html(extr( |
| 61 |
"<h6>Related Tags</h6>", "</ul>")), |
| 62 |
"favorite" : text.split_html(extr( |
| 63 |
"<h6>Favorited By</h6>", "</ul>")), |
| 64 |
}) |
| 65 |
|
| 66 |
post["filename"], _, ext = extr('" alt="', '"').rpartition(".") |
| 67 |
if not post["extension"]: |
| 68 |
post["extension"] = ext.lower() |
| 69 |
|
| 70 |
return post |
| 71 |
|
| 72 |
def _pagination(self, url, callback=None): |
| 73 |
params = { |
| 74 |
"pageNo" : self.page_start, |
| 75 |
"pageSize": self.per_page, |
| 76 |
} |
| 77 |
|
| 78 |
page = self.request(url, params=params).text |
| 79 |
if callback is not None: |
| 80 |
callback(page) |
| 81 |
|
| 82 |
while True: |
| 83 |
thumb = None |
| 84 |
for thumb in text.extract_iter( |
| 85 |
page, '<a href="/Details/', ';base64'): |
| 86 |
yield { |
| 87 |
"id" : thumb[:thumb.find('?')], |
| 88 |
"extension": thumb[thumb.rfind("/")+1:], |
| 89 |
} |
| 90 |
|
| 91 |
if "disabled" in text.extr(page, 'area-label="Next"', ">") or \ |
| 92 |
thumb is None: |
| 93 |
return |
| 94 |
params["pageNo"] += 1 |
| 95 |
page = self.request(url, params=params).text |
| 96 |
|
| 97 |
|
| 98 |
class SizebooruPostExtractor(SizebooruExtractor): |
| 99 |
"""Extractor for sizebooru posts""" |
| 100 |
subcategory = "post" |
| 101 |
pattern = BASE_PATTERN + r"/Details/(\d+)" |
| 102 |
example = "https://sizebooru.com/Details/12345" |
| 103 |
|
| 104 |
def posts(self): |
| 105 |
return ({"id": self.groups[0], "extension": None},) |
| 106 |
|
| 107 |
|
| 108 |
class SizebooruTagExtractor(SizebooruExtractor): |
| 109 |
"""Extractor for sizebooru tag searches""" |
| 110 |
subcategory = "tag" |
| 111 |
directory_fmt = ("{category}", "{search_tags}") |
| 112 |
pattern = BASE_PATTERN + r"/Search/([^/?#]+)" |
| 113 |
example = "https://sizebooru.com/Search/TAG" |
| 114 |
|
| 115 |
def posts(self): |
| 116 |
tag = self.groups[0] |
| 117 |
self.kwdict["search_tags"] = text.unquote(tag) |
| 118 |
return self._pagination(f"{self.root}/Search/{tag}") |
| 119 |
|
| 120 |
|
| 121 |
class SizebooruGalleryExtractor(SizebooruExtractor): |
| 122 |
"""Extractor for sizebooru galleries""" |
| 123 |
subcategory = "gallery" |
| 124 |
directory_fmt = ("{category}", "{gallery_name} ({gallery_id})") |
| 125 |
pattern = BASE_PATTERN + r"/Galleries/List/(\d+)" |
| 126 |
example = "https://sizebooru.com/Galleries/List/123" |
| 127 |
|
| 128 |
def posts(self): |
| 129 |
gid = self.groups[0] |
| 130 |
self.kwdict["gallery_id"] = text.parse_int(gid) |
| 131 |
return self._pagination( |
| 132 |
f"{self.root}/Galleries/List/{gid}", self._extract_name) |
| 133 |
|
| 134 |
def _extract_name(self, page): |
| 135 |
self.kwdict["gallery_name"] = text.unescape(text.extr( |
| 136 |
page, "<title>Gallery: ", " - Size Booru<")) |
| 137 |
|
| 138 |
|
| 139 |
class SizebooruUserExtractor(SizebooruExtractor): |
| 140 |
"""Extractor for a sizebooru user's uploads""" |
| 141 |
subcategory = "user" |
| 142 |
directory_fmt = ("{category}", "Uploads {user}") |
| 143 |
pattern = BASE_PATTERN + r"/Profile/Uploads/([^/?#]+)" |
| 144 |
example = "https://sizebooru.com/Profile/Uploads/USER" |
| 145 |
|
| 146 |
def posts(self): |
| 147 |
user = self.groups[0] |
| 148 |
self.kwdict["user"] = text.unquote(user) |
| 149 |
return self._pagination(f"{self.root}/Profile/Uploads/{user}",) |
| 150 |
|
| 151 |
|
| 152 |
class SizebooruFavoriteExtractor(SizebooruExtractor): |
| 153 |
"""Extractor for a sizebooru user's favorites""" |
| 154 |
subcategory = "favorite" |
| 155 |
directory_fmt = ("{category}", "Favorites {user}") |
| 156 |
pattern = BASE_PATTERN + r"/Profile/Favorites/([^/?#]+)" |
| 157 |
example = "https://sizebooru.com/Profile/Favorites/USER" |
| 158 |
|
| 159 |
def posts(self): |
| 160 |
user = self.groups[0] |
| 161 |
self.kwdict["user"] = text.unquote(user) |
| 162 |
return self._pagination(f"{self.root}/Profile/Favorites/{user}",) |