| 1 |
# -*- coding: utf-8 -*- |
| 2 |
|
| 3 |
# Copyright 2020 Leonid "Bepis" Pavel |
| 4 |
# Copyright 2023-2026 Mike Fährmann |
| 5 |
# |
| 6 |
# This program is free software; you can redistribute it and/or modify |
| 7 |
# it under the terms of the GNU General Public License version 2 as |
| 8 |
# published by the Free Software Foundation. |
| 9 |
|
| 10 |
"""Extractors for https://imgchest.com/""" |
| 11 |
|
| 12 |
from .common import GalleryExtractor, Extractor, Message |
| 13 |
from .. import text, util |
| 14 |
|
| 15 |
BASE_PATTERN = r"(?:https?://)?(?:www\.)?imgchest\.com" |
| 16 |
|
| 17 |
|
| 18 |
class ImagechestGalleryExtractor(GalleryExtractor): |
| 19 |
"""Extractor for image galleries from imgchest.com""" |
| 20 |
category = "imagechest" |
| 21 |
root = "https://imgchest.com" |
| 22 |
pattern = BASE_PATTERN + r"/p/([A-Za-z0-9]{11})" |
| 23 |
example = "https://imgchest.com/p/abcdefghijk" |
| 24 |
|
| 25 |
def __init__(self, match): |
| 26 |
self.gallery_id = match[1] |
| 27 |
url = self.root + "/p/" + self.gallery_id |
| 28 |
GalleryExtractor.__init__(self, match, url) |
| 29 |
|
| 30 |
def _init(self): |
| 31 |
if access_token := self.config("access-token"): |
| 32 |
self.api = ImagechestAPI(self, access_token) |
| 33 |
self.page_url = None |
| 34 |
self.metadata = self._metadata_api |
| 35 |
|
| 36 |
def metadata(self, page): |
| 37 |
try: |
| 38 |
data = util.json_loads(text.unescape(text.extr( |
| 39 |
page, 'data-page="', '"'))) |
| 40 |
post = data["props"]["post"] |
| 41 |
except Exception: |
| 42 |
if "<title>Not Found</title>" in page: |
| 43 |
raise self.exc.NotFoundError("gallery") |
| 44 |
self.files = () |
| 45 |
return {} |
| 46 |
|
| 47 |
self.files = post.pop("files", ()) |
| 48 |
post["gallery_id"] = self.gallery_id |
| 49 |
post["tags"] = [tag["name"] for tag in post["tags"]] |
| 50 |
|
| 51 |
return post |
| 52 |
|
| 53 |
def _metadata_api(self, page): |
| 54 |
post = self.api.post(self.gallery_id) |
| 55 |
|
| 56 |
post["date"] = self.parse_datetime_iso(post["created"]) |
| 57 |
for img in post["images"]: |
| 58 |
img["date"] = self.parse_datetime_iso(img["created"]) |
| 59 |
|
| 60 |
post["gallery_id"] = self.gallery_id |
| 61 |
post.pop("image_count", None) |
| 62 |
self.files = post.pop("images") |
| 63 |
|
| 64 |
return post |
| 65 |
|
| 66 |
def images(self, page): |
| 67 |
try: |
| 68 |
return [ |
| 69 |
(file["link"], file) |
| 70 |
for file in self.files |
| 71 |
] |
| 72 |
except Exception: |
| 73 |
return () |
| 74 |
|
| 75 |
|
| 76 |
class ImagechestUserExtractor(Extractor): |
| 77 |
"""Extractor for imgchest.com user profiles""" |
| 78 |
category = "imagechest" |
| 79 |
subcategory = "user" |
| 80 |
root = "https://imgchest.com" |
| 81 |
pattern = BASE_PATTERN + r"/u/([^/?#]+)" |
| 82 |
example = "https://imgchest.com/u/USER" |
| 83 |
|
| 84 |
def items(self): |
| 85 |
url = self.root + "/api/posts" |
| 86 |
params = { |
| 87 |
"page" : 1, |
| 88 |
"sort" : "new", |
| 89 |
"tag" : "", |
| 90 |
"q" : "", |
| 91 |
"username": text.unquote(self.groups[0]), |
| 92 |
"nsfw" : "true", |
| 93 |
} |
| 94 |
|
| 95 |
while True: |
| 96 |
try: |
| 97 |
data = self.request_json(url, params=params)["data"] |
| 98 |
except (TypeError, KeyError): |
| 99 |
return |
| 100 |
|
| 101 |
if not data: |
| 102 |
return |
| 103 |
|
| 104 |
for gallery in data: |
| 105 |
gallery["_extractor"] = ImagechestGalleryExtractor |
| 106 |
yield Message.Queue, gallery["link"], gallery |
| 107 |
|
| 108 |
params["page"] += 1 |
| 109 |
|
| 110 |
|
| 111 |
class ImagechestAPI(): |
| 112 |
"""Interface for the Image Chest API |
| 113 |
|
| 114 |
https://imgchest.com/docs/api/1.0/general/overview |
| 115 |
""" |
| 116 |
root = "https://api.imgchest.com" |
| 117 |
|
| 118 |
def __init__(self, extractor, access_token): |
| 119 |
self.extractor = extractor |
| 120 |
self.headers = {"Authorization": "Bearer " + access_token} |
| 121 |
|
| 122 |
def file(self, file_id): |
| 123 |
endpoint = "/v1/file/" + file_id |
| 124 |
return self._call(endpoint) |
| 125 |
|
| 126 |
def post(self, post_id): |
| 127 |
endpoint = "/v1/post/" + post_id |
| 128 |
return self._call(endpoint) |
| 129 |
|
| 130 |
def user(self, username): |
| 131 |
endpoint = "/v1/user/" + username |
| 132 |
return self._call(endpoint) |
| 133 |
|
| 134 |
def _call(self, endpoint): |
| 135 |
url = self.root + endpoint |
| 136 |
|
| 137 |
while True: |
| 138 |
response = self.extractor.request( |
| 139 |
url, headers=self.headers, fatal=None, allow_redirects=False) |
| 140 |
|
| 141 |
if response.status_code < 300: |
| 142 |
return response.json()["data"] |
| 143 |
|
| 144 |
elif response.status_code < 400: |
| 145 |
raise self.extractor.exc.AuthenticationError( |
| 146 |
"Invalid API access token") |
| 147 |
|
| 148 |
elif response.status_code == 429: |
| 149 |
self.extractor.wait(seconds=600) |
| 150 |
|
| 151 |
else: |
| 152 |
self.extractor.log.debug(response.text) |
| 153 |
raise self.extractor.exc.AbortExtraction("API request failed") |