| 1 |
# -*- coding: utf-8 -*- |
| 2 |
|
| 3 |
# Copyright 2021-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 Philomena sites""" |
| 10 |
|
| 11 |
from .booru import BooruExtractor |
| 12 |
from .. import text |
| 13 |
|
| 14 |
|
| 15 |
class PhilomenaExtractor(BooruExtractor): |
| 16 |
"""Base class for philomena extractors""" |
| 17 |
basecategory = "philomena" |
| 18 |
filename_fmt = "{filename}.{extension}" |
| 19 |
archive_fmt = "{id}" |
| 20 |
request_interval = (0.5, 1.5) |
| 21 |
page_start = 1 |
| 22 |
per_page = 50 |
| 23 |
|
| 24 |
def _init(self): |
| 25 |
self.api = PhilomenaAPI(self) |
| 26 |
self.svg = self.config("svg", True) |
| 27 |
self.comments = self.config("comments", False) |
| 28 |
|
| 29 |
def _file_url(self, post): |
| 30 |
try: |
| 31 |
url = post["representations"]["full"] |
| 32 |
except Exception: |
| 33 |
url = post["view_url"] |
| 34 |
|
| 35 |
if self.svg and post["format"] == "svg": |
| 36 |
return url.rpartition(".")[0] + ".svg" |
| 37 |
return url |
| 38 |
|
| 39 |
def _prepare(self, post): |
| 40 |
post["date"] = self.parse_datetime_iso(post["created_at"][:19]) |
| 41 |
|
| 42 |
if self.comments: |
| 43 |
post["comments"] = (list(self.api.comments(post["id"])) |
| 44 |
if post.get("comment_count") else ()) |
| 45 |
|
| 46 |
|
| 47 |
BASE_PATTERN = PhilomenaExtractor.update({ |
| 48 |
"derpibooru": { |
| 49 |
"root": "https://derpibooru.org", |
| 50 |
"pattern": r"(?:www\.)?derpibooru\.org", |
| 51 |
"filter_id": "56027", |
| 52 |
}, |
| 53 |
"ponybooru": { |
| 54 |
"root": "https://ponybooru.org", |
| 55 |
"pattern": r"(?:www\.)?ponybooru\.org", |
| 56 |
"filter_id": "3", |
| 57 |
}, |
| 58 |
"furbooru": { |
| 59 |
"root": "https://furbooru.org", |
| 60 |
"pattern": r"furbooru\.org", |
| 61 |
"filter_id": "2", |
| 62 |
}, |
| 63 |
}) |
| 64 |
|
| 65 |
|
| 66 |
class PhilomenaPostExtractor(PhilomenaExtractor): |
| 67 |
"""Extractor for single posts on a Philomena booru""" |
| 68 |
subcategory = "post" |
| 69 |
pattern = BASE_PATTERN + r"/(?:images/)?(\d+)" |
| 70 |
example = "https://derpibooru.org/images/12345" |
| 71 |
|
| 72 |
def posts(self): |
| 73 |
return (self.api.image(self.groups[-1]),) |
| 74 |
|
| 75 |
|
| 76 |
class PhilomenaSearchExtractor(PhilomenaExtractor): |
| 77 |
"""Extractor for Philomena search results""" |
| 78 |
subcategory = "search" |
| 79 |
directory_fmt = ("{category}", "{search_tags}") |
| 80 |
pattern = BASE_PATTERN + r"/(?:search/?\?([^#]+)|tags/([^/?#]+))" |
| 81 |
example = "https://derpibooru.org/search?q=QUERY" |
| 82 |
|
| 83 |
def __init__(self, match): |
| 84 |
PhilomenaExtractor.__init__(self, match) |
| 85 |
|
| 86 |
if q := self.groups[-1]: |
| 87 |
q = q.replace("+", " ") |
| 88 |
for old, new in ( |
| 89 |
("-colon-" , ":"), |
| 90 |
("-dash-" , "-"), |
| 91 |
("-dot-" , "."), |
| 92 |
("-plus-" , "+"), |
| 93 |
("-fwslash-", "/"), |
| 94 |
("-bwslash-", "\\"), |
| 95 |
): |
| 96 |
if old in q: |
| 97 |
q = q.replace(old, new) |
| 98 |
self.params = {"q": text.unquote(text.unquote(q))} |
| 99 |
else: |
| 100 |
self.params = text.parse_query(self.groups[-2]) |
| 101 |
|
| 102 |
def metadata(self): |
| 103 |
return {"search_tags": self.params.get("q", "")} |
| 104 |
|
| 105 |
def posts(self): |
| 106 |
return self.api.search(self.params) |
| 107 |
|
| 108 |
|
| 109 |
class PhilomenaGalleryExtractor(PhilomenaExtractor): |
| 110 |
"""Extractor for Philomena galleries""" |
| 111 |
subcategory = "gallery" |
| 112 |
directory_fmt = ("{category}", "galleries", |
| 113 |
"{gallery[id]} {gallery[title]}") |
| 114 |
pattern = BASE_PATTERN + r"/galleries/(\d+)" |
| 115 |
example = "https://derpibooru.org/galleries/12345" |
| 116 |
|
| 117 |
def metadata(self): |
| 118 |
try: |
| 119 |
return {"gallery": self.api.gallery(self.groups[-1])} |
| 120 |
except IndexError: |
| 121 |
raise self.exc.NotFoundError("gallery") |
| 122 |
|
| 123 |
def posts(self): |
| 124 |
gallery_id = "gallery_id:" + self.groups[-1] |
| 125 |
params = {"sd": "desc", "sf": gallery_id, "q": gallery_id} |
| 126 |
return self.api.search(params) |
| 127 |
|
| 128 |
|
| 129 |
class PhilomenaAPI(): |
| 130 |
"""Interface for the Philomena API |
| 131 |
|
| 132 |
https://www.derpibooru.org/pages/api |
| 133 |
""" |
| 134 |
|
| 135 |
def __init__(self, extractor): |
| 136 |
self.extractor = extractor |
| 137 |
self.root = extractor.root + "/api" |
| 138 |
|
| 139 |
def comments(self, image_id): |
| 140 |
endpoint = "/v1/json/search/comments?q=image_id:" + str(image_id) |
| 141 |
return self._pagination(endpoint, {}, "comments") |
| 142 |
|
| 143 |
def gallery(self, gallery_id): |
| 144 |
endpoint = "/v1/json/search/galleries" |
| 145 |
params = {"q": "id:" + gallery_id} |
| 146 |
return self._call(endpoint, params)["galleries"][0] |
| 147 |
|
| 148 |
def image(self, image_id): |
| 149 |
endpoint = "/v1/json/images/" + image_id |
| 150 |
return self._call(endpoint)["image"] |
| 151 |
|
| 152 |
def search(self, params): |
| 153 |
endpoint = "/v1/json/search/images" |
| 154 |
return self._pagination(endpoint, params, "images") |
| 155 |
|
| 156 |
def _call(self, endpoint, params=None): |
| 157 |
url = self.root + endpoint |
| 158 |
|
| 159 |
while True: |
| 160 |
response = self.extractor.request(url, params=params, fatal=None) |
| 161 |
|
| 162 |
if response.status_code < 400: |
| 163 |
return response.json() |
| 164 |
|
| 165 |
if response.status_code == 429: |
| 166 |
self.extractor.wait(seconds=600) |
| 167 |
continue |
| 168 |
|
| 169 |
# error |
| 170 |
self.extractor.log.debug(response.content) |
| 171 |
raise self.extractor.exc.HttpError("", response) |
| 172 |
|
| 173 |
def _pagination(self, endpoint, params, key): |
| 174 |
extr = self.extractor |
| 175 |
|
| 176 |
if api_key := extr.config("api-key"): |
| 177 |
params["key"] = api_key |
| 178 |
|
| 179 |
if filter_id := extr.config("filter"): |
| 180 |
params["filter_id"] = filter_id |
| 181 |
elif not api_key: |
| 182 |
params["filter_id"] = extr.config_instance("filter_id") or "2" |
| 183 |
|
| 184 |
params["page"] = extr.page_start |
| 185 |
params["per_page"] = extr.per_page |
| 186 |
|
| 187 |
while True: |
| 188 |
data = self._call(endpoint, params) |
| 189 |
yield from data[key] |
| 190 |
|
| 191 |
if len(data[key]) < extr.per_page: |
| 192 |
return |
| 193 |
params["page"] += 1 |