| 1 |
# -*- coding: utf-8 -*- |
| 2 |
|
| 3 |
# Copyright 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://goonbox.cr/""" |
| 10 |
|
| 11 |
from .common import Extractor, Message |
| 12 |
from .. import text |
| 13 |
|
| 14 |
BASE_PATTERN = r"(?:https?://)?(?:www\.)?goonbox\.cr" |
| 15 |
|
| 16 |
|
| 17 |
class GoonboxExtractor(Extractor): |
| 18 |
"""Base class for goonbox extractors""" |
| 19 |
category = "goonbox" |
| 20 |
root = "https://goonbox.cr" |
| 21 |
directory_fmt = ("{category}", |
| 22 |
"{album[title]|''}{album[encoded_id]:? (/)/}") |
| 23 |
filename_fmt = "{num:?/ />03}{filename} ({encoded_id}).{extension}" |
| 24 |
archive_fmt = "{encoded_id}" |
| 25 |
|
| 26 |
def items(self): |
| 27 |
for img in self.images(): |
| 28 |
try: |
| 29 |
url = img["original_url"] |
| 30 |
except Exception: |
| 31 |
continue |
| 32 |
|
| 33 |
img["date"] = self.parse_datetime_iso( |
| 34 |
img.get("created_at")) |
| 35 |
img["date_updated"] = self.parse_datetime_iso( |
| 36 |
img.get("updated_at")) |
| 37 |
|
| 38 |
if name := img.get("original_filename"): |
| 39 |
text.nameext_from_name(name, img) |
| 40 |
if not img["extension"]: |
| 41 |
img["extension"] = text.ext_from_url(url) |
| 42 |
else: |
| 43 |
text.nameext_from_url(url, img) |
| 44 |
|
| 45 |
yield Message.Directory, "", img |
| 46 |
yield Message.Url, url, img |
| 47 |
|
| 48 |
|
| 49 |
class GoonboxImageExtractor(GoonboxExtractor): |
| 50 |
subcategory = "image" |
| 51 |
pattern = BASE_PATTERN + r"/img/([^/?#]+)" |
| 52 |
example = "https://goonbox.cr/img/ID" |
| 53 |
|
| 54 |
def images(self): |
| 55 |
url = f"{self.root}/api/images/{self.groups[0].rpartition('.')[2]}" |
| 56 |
data = self.request_json(url) |
| 57 |
|
| 58 |
if "redirect" in data: |
| 59 |
url = data["redirect"] |
| 60 |
data = self.request_json(url) |
| 61 |
|
| 62 |
img = data["image"] |
| 63 |
img["count"] = 1 |
| 64 |
img["num"] = 0 |
| 65 |
|
| 66 |
if "album_nav" in data: |
| 67 |
try: |
| 68 |
nav = data["album_nav"] |
| 69 |
img["num"] = nav["total"] - nav["position"] + 1 |
| 70 |
except Exception as exc: |
| 71 |
self.log.traceback(exc) |
| 72 |
|
| 73 |
return (img,) |
| 74 |
|
| 75 |
|
| 76 |
class GoonboxAlbumExtractor(GoonboxExtractor): |
| 77 |
subcategory = "album" |
| 78 |
pattern = BASE_PATTERN + r"/a/([^/?#]+)" |
| 79 |
example = "https://goonbox.cr/a/ID" |
| 80 |
|
| 81 |
def images(self): |
| 82 |
url = f"{self.root}/api/albums/{self.groups[0].rpartition('.')[2]}" |
| 83 |
params = None |
| 84 |
num = 0 |
| 85 |
|
| 86 |
while True: |
| 87 |
data = self.request_json(url, params=params) |
| 88 |
|
| 89 |
if "redirect" in data: |
| 90 |
url = data["redirect"] |
| 91 |
continue |
| 92 |
if "album" in data: |
| 93 |
album = data["album"] |
| 94 |
self.kwdict["album"] = album |
| 95 |
self.kwdict["count"] = num = album.get("images_count") |
| 96 |
|
| 97 |
for img in data["images"]: |
| 98 |
img["num"] = num |
| 99 |
num -= 1 |
| 100 |
yield img |
| 101 |
|
| 102 |
try: |
| 103 |
pgn = data["pagination"] |
| 104 |
if pgn["current_page"] == pgn["last_page"]: |
| 105 |
break |
| 106 |
except Exception: |
| 107 |
break |
| 108 |
|
| 109 |
if params is None: |
| 110 |
url += "/images" |
| 111 |
params = {"page": 2} |
| 112 |
else: |
| 113 |
params["page"] += 1 |