| 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://s3nd.pics/""" |
| 10 |
|
| 11 |
from .common import Extractor, Message |
| 12 |
from .. import text |
| 13 |
|
| 14 |
BASE_PATTERN = r"(?:https?://)?(?:www\.)?s3nd\.pics" |
| 15 |
|
| 16 |
|
| 17 |
class S3ndpicsExtractor(Extractor): |
| 18 |
"""Base class for s3ndpics extractors""" |
| 19 |
category = "s3ndpics" |
| 20 |
root = "https://s3nd.pics" |
| 21 |
root_api = root + "/api" |
| 22 |
directory_fmt = ("{category}", "{user[username]}", |
| 23 |
"{date} {title:?/ /}({id})") |
| 24 |
filename_fmt = "{num:>02}.{extension}" |
| 25 |
archive_fmt = "{id}_{num}" |
| 26 |
|
| 27 |
def items(self): |
| 28 |
base = "https://s3.s3nd.pics/s3nd-pics/" |
| 29 |
|
| 30 |
for post in self.posts(): |
| 31 |
post["id"] = post.pop("_id", None) |
| 32 |
post["user"] = post.pop("userId", None) |
| 33 |
post["date"] = self.parse_datetime_iso(post["createdAt"]) |
| 34 |
post["date_updated"] = self.parse_datetime_iso(post["updatedAt"]) |
| 35 |
|
| 36 |
files = post.pop("files", ()) |
| 37 |
post["count"] = len(files) |
| 38 |
|
| 39 |
yield Message.Directory, "", post |
| 40 |
for post["num"], file in enumerate(files, 1): |
| 41 |
post["type"] = file["type"] |
| 42 |
path = file["url"] |
| 43 |
text.nameext_from_url(path, post) |
| 44 |
yield Message.Url, base + path, post |
| 45 |
|
| 46 |
def _pagination(self, url, params): |
| 47 |
params["page"] = 1 |
| 48 |
|
| 49 |
while True: |
| 50 |
data = self.request_json(url, params=params) |
| 51 |
|
| 52 |
self.kwdict["total"] = data["pagination"]["total"] |
| 53 |
yield from data["posts"] |
| 54 |
|
| 55 |
if params["page"] >= data["pagination"]["pages"]: |
| 56 |
return |
| 57 |
params["page"] += 1 |
| 58 |
|
| 59 |
|
| 60 |
class S3ndpicsPostExtractor(S3ndpicsExtractor): |
| 61 |
subcategory = "post" |
| 62 |
pattern = BASE_PATTERN + r"/post/([0-9a-f]+)" |
| 63 |
example = "https://s3nd.pics/post/0123456789abcdef01234567" |
| 64 |
|
| 65 |
def posts(self): |
| 66 |
url = f"{self.root_api}/posts/{self.groups[0]}" |
| 67 |
return (self.request_json(url)["post"],) |
| 68 |
|
| 69 |
|
| 70 |
class S3ndpicsUserExtractor(S3ndpicsExtractor): |
| 71 |
subcategory = "user" |
| 72 |
pattern = BASE_PATTERN + r"/user/(\w+)" |
| 73 |
example = "https://s3nd.pics/user/USER" |
| 74 |
|
| 75 |
def posts(self): |
| 76 |
url = f"{self.root_api}/users/username/{self.groups[0]}" |
| 77 |
self.kwdict["user"] = user = self.request_json(url)["user"] |
| 78 |
|
| 79 |
url = self.root_api + "/posts" |
| 80 |
params = { |
| 81 |
"userId": user["_id"], |
| 82 |
"limit" : "12", |
| 83 |
"sortBy": "newest", |
| 84 |
} |
| 85 |
return self._pagination(url, params) |
| 86 |
|
| 87 |
|
| 88 |
class S3ndpicsSearchExtractor(S3ndpicsExtractor): |
| 89 |
subcategory = "search" |
| 90 |
pattern = BASE_PATTERN + r"/search/?\?([^#]+)" |
| 91 |
example = "https://s3nd.pics/search?QUERY" |
| 92 |
|
| 93 |
def posts(self): |
| 94 |
url = self.root_api + "/posts" |
| 95 |
params = text.parse_query(self.groups[0]) |
| 96 |
params.setdefault("limit", "20") |
| 97 |
self.kwdict["search_tags"] = \ |
| 98 |
params.get("tag") or params.get("tags") or params.get("q") |
| 99 |
return self._pagination(url, params) |