| 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://cum.st/""" |
| 10 |
|
| 11 |
from .common import Extractor, Message |
| 12 |
from .. import text, util |
| 13 |
|
| 14 |
BASE_PATTERN = r"(?:https?://)?(?:www\.)?cum\.st" |
| 15 |
USER_PATTERN = BASE_PATTERN + r"/creators/([^/?#]+)/([^/?#]+)" |
| 16 |
|
| 17 |
|
| 18 |
class OnlyhavenExtractor(Extractor): |
| 19 |
"""Base class for onlyhaven extractors""" |
| 20 |
category = "onlyhaven" |
| 21 |
root = "https://cum.st" |
| 22 |
root_dl = "https://e1.cum.st/media" |
| 23 |
directory_fmt = ("{category}", "{service}", "{user}") |
| 24 |
filename_fmt = "{id}_{title[:180]}_{num:>02}_{filename[:180]}.{extension}" |
| 25 |
archive_fmt = "{service}_{user}_{id}_{num}" |
| 26 |
cookies_domain = ".cum.st" |
| 27 |
|
| 28 |
def _init(self): |
| 29 |
if domain := self.config("domain"): |
| 30 |
self.root = (text.root_from_url(self.url) if domain == "auto" else |
| 31 |
text.ensure_http_scheme(domain)) |
| 32 |
lhs, sep, rhs = self.root.partition("://") |
| 33 |
self.root_dl = f"{lhs}{sep}e1.{rhs}/media" |
| 34 |
self.cookies_domain = "." + rhs |
| 35 |
|
| 36 |
self.api = OnlyhavenAPI(self) |
| 37 |
self._find_inline = text.re( |
| 38 |
r'src="(?:https?://(?:cum\.(?:st)))?(/inline/[^"]+' |
| 39 |
r'|/[0-9a-f]{2}/[0-9a-f]{2}/[0-9a-f]{64}\.[^"]+)').findall |
| 40 |
|
| 41 |
def items(self): |
| 42 |
creator_info = {} if self.config("metadata", True) else None |
| 43 |
|
| 44 |
# prevent files from being sent with gzip compression |
| 45 |
headers = {"Accept-Encoding": "identity"} |
| 46 |
|
| 47 |
for post in self.posts(): |
| 48 |
post["_http_headers"] = headers |
| 49 |
post["date"] = self.parse_timestamp( |
| 50 |
post.get("published") or post.get("added") or 0) |
| 51 |
service = post["service"] |
| 52 |
post["user"] = creator_id = \ |
| 53 |
post.get("creatorId") or self.creator_id |
| 54 |
if chtml := post.get("captionHtml"): |
| 55 |
post["title"] = text.unescape(text.remove_html( |
| 56 |
chtml.partition("\n")[0])) |
| 57 |
else: |
| 58 |
post["title"] = (post.get( |
| 59 |
"caption") or "").partition("\n")[0].strip() |
| 60 |
|
| 61 |
if creator_info is not None: |
| 62 |
key = f"{service}_{creator_id}" |
| 63 |
if key in creator_info: |
| 64 |
creator = creator_info[key] |
| 65 |
else: |
| 66 |
try: |
| 67 |
creator = creator_info[key] = self.api.creator_profile( |
| 68 |
service, creator_id) |
| 69 |
except self.exc.HttpError: |
| 70 |
self.log.warning("%s/%s/%s: 'Creator not found'", |
| 71 |
service, creator_id, post["id"]) |
| 72 |
creator = creator_info[key] = util.NONE |
| 73 |
|
| 74 |
post["user_profile"] = creator |
| 75 |
post["username"] = creator["name"] |
| 76 |
|
| 77 |
files = [] |
| 78 |
for file in post["attachments"]: |
| 79 |
try: |
| 80 |
variant = max(file["variants"], |
| 81 |
key=lambda v: v.get("bytes", 0)) |
| 82 |
except LookupError as exc: |
| 83 |
if file.get("locked"): |
| 84 |
self.log.warning("%s/%s: 'Locked' file", |
| 85 |
post["id"], file.get("position")) |
| 86 |
else: |
| 87 |
self.log.warning("%s/%s: General error (%s: %s)", |
| 88 |
post["id"], file.get("position"), |
| 89 |
exc.__clasa__.__name__, exc) |
| 90 |
continue |
| 91 |
path = variant["name"] |
| 92 |
file["url"] = f"{self.root_dl}/{file['storageKey']}/{path}" |
| 93 |
|
| 94 |
if name := file.get("originalFilename"): |
| 95 |
text.nameext_from_name(name, file) |
| 96 |
if not file["extension"]: |
| 97 |
file["extension"] = text.ext_from_url(path) |
| 98 |
else: |
| 99 |
text.nameext_from_url(path, file) |
| 100 |
|
| 101 |
files.append(file) |
| 102 |
|
| 103 |
post["count"] = len(files) |
| 104 |
yield Message.Directory, "", post |
| 105 |
for post["num"], file in enumerate(files, 1): |
| 106 |
file["file_id"] = file.pop("id", None) |
| 107 |
post.update(file) |
| 108 |
yield Message.Url, file["url"], post |
| 109 |
|
| 110 |
def _expand(self, posts): |
| 111 |
if self.config("expand") or \ |
| 112 |
self.config("endpoint") in {"posts+", "legacy+"}: |
| 113 |
def gen(): |
| 114 |
creator_post = self.api.creator_post |
| 115 |
creator_id = self.creator_id |
| 116 |
for post in posts: |
| 117 |
yield creator_post(post["service"], |
| 118 |
post.get("creatorId") or creator_id, |
| 119 |
post["id"]) |
| 120 |
return gen() |
| 121 |
return posts |
| 122 |
|
| 123 |
|
| 124 |
class OnlyhavenUserExtractor(OnlyhavenExtractor): |
| 125 |
"""Extractor for all posts from a onlyhaven user listing""" |
| 126 |
subcategory = "user" |
| 127 |
pattern = USER_PATTERN + r"/?(?:\?([^#]+))?(?:$|\?|#)" |
| 128 |
example = "https://cum.st/creators/SERVICE/12345" |
| 129 |
|
| 130 |
def __init__(self, match): |
| 131 |
self.subcategory = match[1] |
| 132 |
OnlyhavenExtractor.__init__(self, match) |
| 133 |
|
| 134 |
def posts(self): |
| 135 |
service, self.creator_id, query = self.groups |
| 136 |
params = text.parse_query(query) |
| 137 |
|
| 138 |
return self._expand(self.api.creator_posts( |
| 139 |
service, self.creator_id, params.get("o"), |
| 140 |
params.get("q"), params.get("type"), params.get("sort"))) |
| 141 |
|
| 142 |
|
| 143 |
class OnlyhavenPostExtractor(OnlyhavenExtractor): |
| 144 |
"""Extractor for a single onlyhaven post""" |
| 145 |
subcategory = "post" |
| 146 |
pattern = USER_PATTERN + r"/post/([^/?#]+)" |
| 147 |
example = "https://cum.st/creators/SERVICE/12345/post/12345" |
| 148 |
|
| 149 |
def __init__(self, match): |
| 150 |
self.subcategory = match[1] |
| 151 |
OnlyhavenExtractor.__init__(self, match) |
| 152 |
|
| 153 |
def posts(self): |
| 154 |
service, self.creator_id, post_id = self.groups |
| 155 |
return (self.api.creator_post(service, self.creator_id, post_id),) |
| 156 |
|
| 157 |
|
| 158 |
class OnlyhavenPostsExtractor(OnlyhavenExtractor): |
| 159 |
"""Extractor for onlyhaven post listings""" |
| 160 |
subcategory = "posts" |
| 161 |
pattern = BASE_PATTERN + r"/posts(?:/?\?([^#]+))?" |
| 162 |
example = "https://cum.st/posts" |
| 163 |
|
| 164 |
def posts(self): |
| 165 |
self.creator_id = 0 |
| 166 |
params = text.parse_query(self.groups[0]) |
| 167 |
return self._expand(self.api.posts( |
| 168 |
params.get("o"), params.get("q"), |
| 169 |
params.get("service"), params.get("type"), params.get("sort"))) |
| 170 |
|
| 171 |
|
| 172 |
class OnlyhavenAPI(): |
| 173 |
"""Interface for the OnlyHaven API |
| 174 |
|
| 175 |
https://cum.st/swagger/index.html |
| 176 |
""" |
| 177 |
|
| 178 |
def __init__(self, extractor): |
| 179 |
self.extractor = extractor |
| 180 |
self.root = extractor.root + "/api" |
| 181 |
|
| 182 |
def account_favorites(self, type): |
| 183 |
endpoint = "/v1/account/favorites" |
| 184 |
params = {"type": type} |
| 185 |
return self._call(endpoint, params) |
| 186 |
|
| 187 |
def creators(self): |
| 188 |
endpoint = "/v1/creators" |
| 189 |
return self._call(endpoint) |
| 190 |
|
| 191 |
def creator_posts(self, service, creator_id, |
| 192 |
offset=0, query=None, type=None, sort=None): |
| 193 |
endpoint = f"/v1/{service}/user/{creator_id}/posts" |
| 194 |
params = {"o": offset, "q": query, "type": type, "sort": sort} |
| 195 |
return self._pagination(endpoint, params, 50, "posts") |
| 196 |
|
| 197 |
def creator_dms(self, service, creator_id): |
| 198 |
endpoint = f"/v1/{service}/user/{creator_id}/dms" |
| 199 |
return self._call(endpoint) |
| 200 |
|
| 201 |
def creator_post(self, service, creator_id, post_id): |
| 202 |
endpoint = f"/v1/{service}/user/{creator_id}/post/{post_id}" |
| 203 |
return self._call(endpoint) |
| 204 |
|
| 205 |
def creator_profile(self, service, creator_id): |
| 206 |
endpoint = f"/v1/{service}/user/{creator_id}/profile" |
| 207 |
return self._call(endpoint) |
| 208 |
|
| 209 |
def creator_links(self, service, creator_id): |
| 210 |
endpoint = f"/v1/{service}/user/{creator_id}/links" |
| 211 |
return self._call(endpoint) |
| 212 |
|
| 213 |
def posts(self, offset=0, query=None, service=None, type=None, sort=None): |
| 214 |
endpoint = "/v1/posts" |
| 215 |
params = {"o": offset, "q": query, |
| 216 |
"service": service, "type": type, "sort": sort} |
| 217 |
return self._pagination(endpoint, params, 50, "posts") |
| 218 |
|
| 219 |
def _call(self, endpoint, params=None, headers=None, fatal=True): |
| 220 |
return self.extractor.request_json( |
| 221 |
self.root + endpoint, params=params, headers=headers, |
| 222 |
encoding="utf-8", fatal=fatal) |
| 223 |
|
| 224 |
def _pagination(self, endpoint, params, batch=50, key=None): |
| 225 |
offset = text.parse_int(params.get("o")) |
| 226 |
params["o"] = offset - offset % batch |
| 227 |
params["n"] = batch |
| 228 |
|
| 229 |
while True: |
| 230 |
data = self._call(endpoint, params) |
| 231 |
|
| 232 |
if key is not None: |
| 233 |
data = data.get(key) |
| 234 |
if not data: |
| 235 |
break |
| 236 |
yield from data |
| 237 |
|
| 238 |
if len(data) < batch: |
| 239 |
break |
| 240 |
params["o"] += batch |