| 1 |
# -*- coding: utf-8 -*- |
| 2 |
|
| 3 |
# Copyright 2022-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://bunkr.cr/""" |
| 10 |
|
| 11 |
from .common import Extractor |
| 12 |
from .lolisafe import LolisafeAlbumExtractor |
| 13 |
from .. import text, util, config |
| 14 |
import random |
| 15 |
|
| 16 |
if config.get(("extractor", "bunkr"), "tlds"): |
| 17 |
BASE_PATTERN = ( |
| 18 |
r"(?:bunkr:(?:https?://)?([^/?#]+)|" |
| 19 |
r"(?:https?://)?(?:app\.)?(bunkr+\.\w+))" |
| 20 |
) |
| 21 |
else: |
| 22 |
BASE_PATTERN = ( |
| 23 |
r"(?:bunkr:(?:https?://)?([^/?#]+)|" |
| 24 |
r"(?:https?://)?(?:app\.)?(bunkr+" |
| 25 |
r"\.(?:s[kiu]|c[ir]|fi|p[hks]|ru|la|is|to|a[cx]" |
| 26 |
r"|black|cat|media|red|site|ws|org)))" |
| 27 |
) |
| 28 |
|
| 29 |
DOMAINS = [ |
| 30 |
"bunkr.ac", |
| 31 |
"bunkr.ci", |
| 32 |
"bunkr.cr", |
| 33 |
"bunkr.fi", |
| 34 |
"bunkr.ph", |
| 35 |
"bunkr.pk", |
| 36 |
"bunkr.ps", |
| 37 |
"bunkr.si", |
| 38 |
"bunkr.sk", |
| 39 |
"bunkr.ws", |
| 40 |
"bunkr.black", |
| 41 |
"bunkr.red", |
| 42 |
"bunkr.media", |
| 43 |
"bunkr.site", |
| 44 |
] |
| 45 |
LEGACY_DOMAINS = { |
| 46 |
"bunkr.ax", |
| 47 |
"bunkr.cat", |
| 48 |
"bunkr.ru", |
| 49 |
"bunkrr.ru", |
| 50 |
"bunkr.su", |
| 51 |
"bunkrr.su", |
| 52 |
"bunkr.la", |
| 53 |
"bunkr.is", |
| 54 |
"bunkr.to", |
| 55 |
} |
| 56 |
CF_DOMAINS = set() |
| 57 |
|
| 58 |
|
| 59 |
class BunkrAlbumExtractor(LolisafeAlbumExtractor): |
| 60 |
"""Extractor for bunkr albums""" |
| 61 |
category = "bunkr" |
| 62 |
root = "https://bunkr.cr" |
| 63 |
root_api = "https://dl.bunkr.cr" |
| 64 |
root_sign = "https://glb-apisign.cdn.cr" |
| 65 |
archive_fmt = "{album_id}_{id|id_url|slug}" |
| 66 |
pattern = BASE_PATTERN + r"/a/([^/?#]+)" |
| 67 |
example = "https://bunkr.cr/a/ID" |
| 68 |
|
| 69 |
def __init__(self, match): |
| 70 |
LolisafeAlbumExtractor.__init__(self, match) |
| 71 |
domain = self.groups[0] or self.groups[1] or "bunkr.cr" |
| 72 |
if domain not in LEGACY_DOMAINS: |
| 73 |
self.root = "https://" + domain |
| 74 |
|
| 75 |
def _init(self): |
| 76 |
LolisafeAlbumExtractor._init(self) |
| 77 |
|
| 78 |
endpoint = self.config("endpoint") |
| 79 |
if not endpoint: |
| 80 |
endpoint = self.root_api + "/api/_001_v2" |
| 81 |
elif endpoint[0] == "/": |
| 82 |
endpoint = self.root_api + endpoint |
| 83 |
|
| 84 |
self.endpoint = endpoint |
| 85 |
self.offset = 0 |
| 86 |
|
| 87 |
def skip_files(self, num): |
| 88 |
self.offset = num |
| 89 |
return num |
| 90 |
|
| 91 |
def request(self, url, **kwargs): |
| 92 |
kwargs["encoding"] = "utf-8" |
| 93 |
kwargs["allow_redirects"] = False |
| 94 |
|
| 95 |
while True: |
| 96 |
try: |
| 97 |
response = Extractor.request(self, url, **kwargs) |
| 98 |
if response.status_code < 300: |
| 99 |
return response |
| 100 |
|
| 101 |
# redirect |
| 102 |
url = response.headers["Location"] |
| 103 |
if url[0] == "/": |
| 104 |
url = self.root + url |
| 105 |
continue |
| 106 |
root, path = self._split(url) |
| 107 |
if root not in CF_DOMAINS: |
| 108 |
continue |
| 109 |
self.log.debug("Redirect to known CF challenge domain '%s'", |
| 110 |
root) |
| 111 |
|
| 112 |
except self.exc.HttpError as exc: |
| 113 |
if exc.status != 403: |
| 114 |
raise |
| 115 |
|
| 116 |
# CF challenge |
| 117 |
root, path = self._split(url) |
| 118 |
CF_DOMAINS.add(root) |
| 119 |
self.log.debug("Added '%s' to CF challenge domains", root) |
| 120 |
|
| 121 |
try: |
| 122 |
DOMAINS.remove(root.rpartition("/")[2]) |
| 123 |
except ValueError: |
| 124 |
pass |
| 125 |
else: |
| 126 |
if not DOMAINS: |
| 127 |
raise self.exc.AbortExtraction( |
| 128 |
"All Bunkr domains require solving a CF challenge") |
| 129 |
|
| 130 |
# select alternative domain |
| 131 |
self.root = root = "https://" + random.choice(DOMAINS) |
| 132 |
self.log.debug("Trying '%s' as fallback", root) |
| 133 |
url = root + path |
| 134 |
|
| 135 |
def fetch_album(self, album_id): |
| 136 |
# album metadata |
| 137 |
page = self.request(f"{self.root}/a/{album_id}?advanced=1").text |
| 138 |
title = text.unescape(text.unescape(text.extr( |
| 139 |
page, 'property="og:title" content="', '"'))) |
| 140 |
|
| 141 |
# files |
| 142 |
items = text.extr( |
| 143 |
page, "window.albumFiles = [", "</script>").split("\n},\n") |
| 144 |
|
| 145 |
return self._extract_files(items), { |
| 146 |
"album_id" : album_id, |
| 147 |
"album_name" : title, |
| 148 |
"album_size" : text.extr( |
| 149 |
page, '<span class="font-semibold">(', ')'), |
| 150 |
"count" : len(items), |
| 151 |
} |
| 152 |
|
| 153 |
def _extract_files(self, items): |
| 154 |
if self.offset: |
| 155 |
items = util.advance(items, self.offset) |
| 156 |
|
| 157 |
for item in items: |
| 158 |
try: |
| 159 |
data_id = text.extr(item, " id: ", ",").strip() |
| 160 |
file = self._extract_file(data_id) |
| 161 |
|
| 162 |
file["name"] = util.json_loads(text.extr( |
| 163 |
item, 'original:', ',\n').replace("\\'", "'")) |
| 164 |
file["slug"] = util.json_loads(text.extr( |
| 165 |
item, 'slug: ', ',\n').replace("\\'", "'")) |
| 166 |
file["uuid"] = text.extr( |
| 167 |
item, 'name: "', ".") |
| 168 |
file["size"] = text.parse_int(text.extr( |
| 169 |
item, "size: ", " ,\n")) |
| 170 |
file["date"] = self.parse_datetime(text.extr( |
| 171 |
item, 'timestamp: "', '"'), "%H:%M:%S %d/%m/%Y") |
| 172 |
tn = text.extr(item, "thumbnail: ", ",\n").replace("\\'", "'") |
| 173 |
file["thumbnail"] = util.json_loads(tn) if tn else None |
| 174 |
|
| 175 |
yield file |
| 176 |
except self.exc.ControlException: |
| 177 |
raise |
| 178 |
except Exception as exc: |
| 179 |
self.log.error("%s: %s", exc.__class__.__name__, exc) |
| 180 |
self.log.debug("%s", item, exc_info=exc) |
| 181 |
if isinstance(exc, self.exc.HttpError) and \ |
| 182 |
exc.status == 400 and \ |
| 183 |
exc.response.url.startswith(self.root_api): |
| 184 |
raise self.exc.AbortExtraction("Album deleted") |
| 185 |
|
| 186 |
def _extract_file(self, data_id): |
| 187 |
headers = { |
| 188 |
"Referer": self.root_api + "/", |
| 189 |
"Origin" : self.root_api, |
| 190 |
} |
| 191 |
|
| 192 |
url = self.endpoint |
| 193 |
file = self.request_json( |
| 194 |
url, method="POST", headers=headers, json={"id": data_id}) |
| 195 |
|
| 196 |
url = self.root_sign + "/sign" |
| 197 |
sign = self.request_json( |
| 198 |
url, params={"path": file["path"]}, headers=headers) |
| 199 |
if "original" in file: |
| 200 |
sign["n"] = file["original"] |
| 201 |
|
| 202 |
del headers["Origin"] |
| 203 |
return { |
| 204 |
"file" : (f"{file['mediafiles']}{file['path']}" |
| 205 |
f"?{text.build_query(sign)}"), |
| 206 |
"id_url" : data_id, |
| 207 |
"_http_headers" : headers, |
| 208 |
"_http_validate": self._validate, |
| 209 |
} |
| 210 |
|
| 211 |
def _validate(self, response): |
| 212 |
if response.history and response.url.endswith( |
| 213 |
("/maint.mp4", "/maintenance-vid.mp4")): |
| 214 |
self.log.warning("File server in maintenance mode") |
| 215 |
return False |
| 216 |
return True |
| 217 |
|
| 218 |
def _split(self, url): |
| 219 |
pos = url.index("/", 8) |
| 220 |
return url[:pos], url[pos:] |
| 221 |
|
| 222 |
|
| 223 |
class BunkrMediaExtractor(BunkrAlbumExtractor): |
| 224 |
"""Extractor for bunkr media links""" |
| 225 |
subcategory = "media" |
| 226 |
directory_fmt = ("{category}",) |
| 227 |
pattern = BASE_PATTERN + r"(/[fvid]/[^/?#]+)" |
| 228 |
example = "https://bunkr.cr/f/FILENAME" |
| 229 |
|
| 230 |
def fetch_album(self, album_id): |
| 231 |
try: |
| 232 |
page = self.request(self.root + album_id).text |
| 233 |
data_id = text.extr(page, 'data-file-id="', '"') |
| 234 |
file = self._extract_file(data_id) |
| 235 |
file["name"] = text.unquote(text.unescape(text.extr( |
| 236 |
page, "<h1", "<").rpartition(">")[2])) |
| 237 |
file["slug"] = album_id.rpartition("/")[2] |
| 238 |
file["uuid"] = text.extr(page, "/thumbs/", ".") |
| 239 |
tn = text.extr(page, 'property="og:image" content="', '"') |
| 240 |
file["thumbnail"] = text.unescape(tn) if tn else None |
| 241 |
except Exception as exc: |
| 242 |
self.log.error("%s: %s", exc.__class__.__name__, exc) |
| 243 |
return (), {} |
| 244 |
|
| 245 |
album_id, album_name, album_size = self.cache( |
| 246 |
self._album_info, text.extr(page, ' href="../a/', '"')) |
| 247 |
return (file,), { |
| 248 |
"album_id" : album_id, |
| 249 |
"album_name": album_name, |
| 250 |
"album_size": album_size, |
| 251 |
"count" : 1, |
| 252 |
} |
| 253 |
|
| 254 |
def _album_info(self, album_id): |
| 255 |
if album_id: |
| 256 |
try: |
| 257 |
page = self.request(f"{self.root}/a/{album_id}").text |
| 258 |
return ( |
| 259 |
album_id, |
| 260 |
text.unescape(text.unescape(text.extr( |
| 261 |
page, 'property="og:title" content="', '"'))), |
| 262 |
text.extr(page, '<span class="font-semibold">(', ')'), |
| 263 |
) |
| 264 |
except Exception: |
| 265 |
pass |
| 266 |
return album_id, "", -1 |
| 267 |
|
| 268 |
|
| 269 |
class BunkrDirectLinkExtractor(BunkrMediaExtractor): |
| 270 |
subcategory = "direct-link" |
| 271 |
pattern = r"https://cdn\d*\.bunkr\.ru()()(/.+)" |
| 272 |
example = "https://cdn123.bunkr.ru/NAME-ID.EXT" |
| 273 |
|
| 274 |
def fetch_album(self, album_id): |
| 275 |
return BunkrMediaExtractor.fetch_album(self, "/f" + album_id) |