| 1 |
# -*- coding: utf-8 -*- |
| 2 |
|
| 3 |
# Copyright 2025-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://www.bellazon.com/""" |
| 10 |
|
| 11 |
from .common import Extractor, Message |
| 12 |
from .. import text |
| 13 |
|
| 14 |
BASE_PATTERN = r"(?:https?://)?(?:www\.)?bellazon\.com/main" |
| 15 |
|
| 16 |
|
| 17 |
class BellazonExtractor(Extractor): |
| 18 |
"""Base class for bellazon extractors""" |
| 19 |
category = "bellazon" |
| 20 |
root = "https://www.bellazon.com/main" |
| 21 |
directory_fmt = ("{category}", "{thread[section]}", |
| 22 |
"{thread[title]} ({thread[id]})") |
| 23 |
filename_fmt = "{post[id]}_{num:>02}_{id}_{filename}.{extension}" |
| 24 |
archive_fmt = "{post[id]}/{id}_{filename}" |
| 25 |
|
| 26 |
def items(self): |
| 27 |
native = (self.root + "/", self.root[6:] + "/") |
| 28 |
quotes = self.config("quoted", False) |
| 29 |
extract_urls = text.re( |
| 30 |
r'(?s)<(' |
| 31 |
r'(?:video .*?<source [^>]*?src|a [^>]*?href)="([^"]+).*?</a>' |
| 32 |
r'|img [^>]*?src="([^"]+)"[^>]*>' |
| 33 |
r')' |
| 34 |
).findall |
| 35 |
|
| 36 |
for post in self.posts(): |
| 37 |
if quotes: |
| 38 |
urls = extract_urls(post["content"]) |
| 39 |
else: |
| 40 |
urls = extract_urls(self._remove_quotes(post["content"])) |
| 41 |
|
| 42 |
data = {"post": post} |
| 43 |
post["count"] = data["count"] = len(urls) |
| 44 |
|
| 45 |
yield Message.Directory, "", data |
| 46 |
data["num"] = data["num_internal"] = data["num_external"] = 0 |
| 47 |
for info, url, url_img in urls: |
| 48 |
if url_img: |
| 49 |
url = text.unescape( |
| 50 |
text.extr(info, 'data-full-image="', '"') or url_img) |
| 51 |
else: |
| 52 |
url = text.unescape(url) |
| 53 |
|
| 54 |
if url.startswith(native): |
| 55 |
if ( |
| 56 |
"/main/style_emoticons/" in url or |
| 57 |
"/uploads/emoticons/" in url or |
| 58 |
"/public/style_" in url or |
| 59 |
"/profile/" in url or |
| 60 |
"/topic/" in url |
| 61 |
): |
| 62 |
continue |
| 63 |
data["num"] += 1 |
| 64 |
data["num_internal"] += 1 |
| 65 |
if not (alt := text.extr(info, ' alt="', '"')) or ( |
| 66 |
alt.startswith("post-") and "_thumb." in alt): |
| 67 |
dc = text.nameext_from_url(url, data.copy()) |
| 68 |
else: |
| 69 |
dc = data.copy() |
| 70 |
dc["name"] = name = text.unescape(alt) |
| 71 |
dc["filename"] = name.partition(".")[0] |
| 72 |
|
| 73 |
dc["id"] = text.extr(info, 'data-fileid="', '"') |
| 74 |
if ext := text.extr(info, 'data-fileext="', '"'): |
| 75 |
dc["extension"] = ext |
| 76 |
elif ("/core/interface/file/attachment.php" in url or |
| 77 |
"/main/index.php?" in url): |
| 78 |
if not dc["id"]: |
| 79 |
params = text.parse_query(url[url.find("?")+1:]) |
| 80 |
dc["id"] = (params.get("id") or |
| 81 |
params.get("attach_id")) |
| 82 |
if name := text.extr(info, ">", "<").strip(): |
| 83 |
dc["name"] = name = text.unescape(name) |
| 84 |
text.nameext_from_name(name, dc) |
| 85 |
else: |
| 86 |
dc["extension"] = text.ext_from_url(url) |
| 87 |
|
| 88 |
if url[0] == "/": |
| 89 |
url = "https:" + url |
| 90 |
yield Message.Url, url, dc |
| 91 |
|
| 92 |
else: |
| 93 |
data["num"] += 1 |
| 94 |
data["num_external"] += 1 |
| 95 |
yield Message.Queue, url, data |
| 96 |
|
| 97 |
def _pagination(self, base, pnum=None): |
| 98 |
base = self.root + base |
| 99 |
|
| 100 |
if pnum is None: |
| 101 |
url = base + "/" |
| 102 |
pnum = 1 |
| 103 |
else: |
| 104 |
url = f"{base}/page/{pnum}/" |
| 105 |
pnum = None |
| 106 |
|
| 107 |
while True: |
| 108 |
page = self.request(url).text |
| 109 |
|
| 110 |
yield page |
| 111 |
|
| 112 |
if pnum is None or ' rel="next" ' not in page or text.extr( |
| 113 |
page, " rel=\"next\" data-page='", "'") == str(pnum): |
| 114 |
return |
| 115 |
pnum += 1 |
| 116 |
url = f"{base}/page/{pnum}/" |
| 117 |
|
| 118 |
def _pagination_reverse(self, base, pnum=None): |
| 119 |
base = self.root + base |
| 120 |
|
| 121 |
url = f"{base}/page/{'9999' if pnum is None else pnum}/" |
| 122 |
with self.request(url) as response: |
| 123 |
parts = response.url.rsplit("/", 3) |
| 124 |
pnum = text.parse_int(parts[2]) if parts[1] == "page" else 1 |
| 125 |
page = response.text |
| 126 |
|
| 127 |
while True: |
| 128 |
yield page |
| 129 |
|
| 130 |
pnum -= 1 |
| 131 |
if pnum > 1: |
| 132 |
url = f"{base}/page/{pnum}/" |
| 133 |
elif pnum == 1: |
| 134 |
url = base + "/" |
| 135 |
else: |
| 136 |
return |
| 137 |
|
| 138 |
page = self.request(url).text |
| 139 |
|
| 140 |
def _parse_thread(self, page): |
| 141 |
schema = self._extract_jsonld(page) |
| 142 |
author = schema["author"] |
| 143 |
stats = schema["interactionStatistic"] |
| 144 |
url_t = schema["url"] |
| 145 |
url_a = author.get("url") or "" |
| 146 |
|
| 147 |
path = text.split_html(text.extr( |
| 148 |
page, '<nav class="ipsBreadcrumb', "</nav>"))[2:-1] |
| 149 |
|
| 150 |
thread = { |
| 151 |
"url" : url_t, |
| 152 |
"path" : path, |
| 153 |
"title": schema["headline"], |
| 154 |
"views": stats[0]["userInteractionCount"], |
| 155 |
"posts": stats[1]["userInteractionCount"], |
| 156 |
"date" : self.parse_datetime_iso(schema["datePublished"]), |
| 157 |
"date_updated": self.parse_datetime_iso(schema["dateModified"]), |
| 158 |
"description" : text.unescape(schema["text"]).strip(), |
| 159 |
"section" : path[-2], |
| 160 |
"author" : author["name"], |
| 161 |
"author_url" : url_a, |
| 162 |
} |
| 163 |
|
| 164 |
thread["id"], _, slug = \ |
| 165 |
url_t.rsplit("/", 2)[1].partition("-") |
| 166 |
thread["slug"] = text.unquote(slug) |
| 167 |
|
| 168 |
if url_a: |
| 169 |
thread["author_id"], _, thread["author_slug"] = \ |
| 170 |
url_a.rsplit("/", 2)[1].partition("-") |
| 171 |
else: |
| 172 |
thread["author_id"] = thread["author_slug"] = "" |
| 173 |
|
| 174 |
return thread |
| 175 |
|
| 176 |
def _parse_post(self, html): |
| 177 |
extr = text.extract_from(html) |
| 178 |
|
| 179 |
post = { |
| 180 |
"id": extr('id="elComment_', '"'), |
| 181 |
"author_url": extr(" href='", "'"), |
| 182 |
"date": self.parse_datetime_iso(extr("datetime='", "'")), |
| 183 |
"content": extr("<!-- Post content -->", '<menu data-ips-hook='), |
| 184 |
} |
| 185 |
|
| 186 |
beg = post["content"].find(">") |
| 187 |
sig = post["content"].rfind('<div data-role="memberSignature"') |
| 188 |
end = post["content"].rfind("\n\t\t</div>", None, sig+1 or None) |
| 189 |
post["content"] = post["content"][beg+1:end].strip() |
| 190 |
|
| 191 |
if url_a := post["author_url"]: |
| 192 |
post["author_id"], _, post["author_slug"] = \ |
| 193 |
url_a.rsplit("/", 2)[1].partition("-") |
| 194 |
else: |
| 195 |
post["author_id"] = post["author_slug"] = "" |
| 196 |
|
| 197 |
return post |
| 198 |
|
| 199 |
def _remove_quotes(self, content): |
| 200 |
while "<blockquote" in content: |
| 201 |
beg = content.index("<blockquote") |
| 202 |
end = content.index("</blockquote", beg) |
| 203 |
for _ in range(content.count("<blockquote", beg+11, end)): |
| 204 |
end = content.index("</blockquote", end+13) |
| 205 |
content = content[:beg] + content[end+13:] |
| 206 |
return content |
| 207 |
|
| 208 |
|
| 209 |
class BellazonPostExtractor(BellazonExtractor): |
| 210 |
subcategory = "post" |
| 211 |
pattern = (BASE_PATTERN + r"(/topic/\d+-[^/?#]+(?:/page/\d+)?)" |
| 212 |
r"/?#(?:findC|c)omment-(\d+)") |
| 213 |
example = "https://www.bellazon.com/main/topic/123-SLUG/#findComment-12345" |
| 214 |
|
| 215 |
def posts(self): |
| 216 |
path, post_id = self.groups |
| 217 |
page = self.request(self.root + path).text |
| 218 |
|
| 219 |
pos = page.find('id="elComment_' + post_id) |
| 220 |
if pos < 0: |
| 221 |
raise self.exc.NotFoundError("post") |
| 222 |
html = text.extract(page, "<article ", "</article>", pos-100)[0] |
| 223 |
|
| 224 |
self.kwdict["thread"] = self._parse_thread(page) |
| 225 |
return (self._parse_post(html),) |
| 226 |
|
| 227 |
|
| 228 |
class BellazonThreadExtractor(BellazonExtractor): |
| 229 |
subcategory = "thread" |
| 230 |
pattern = BASE_PATTERN + r"(/topic/\d+-[^/?#]+)(?:/page/(\d+))?" |
| 231 |
example = "https://www.bellazon.com/main/topic/123-SLUG/" |
| 232 |
|
| 233 |
def posts(self): |
| 234 |
if (order := self.config("order-posts")) and \ |
| 235 |
order[0] not in ("d", "r"): |
| 236 |
pages = self._pagination(*self.groups) |
| 237 |
reverse = False |
| 238 |
else: |
| 239 |
pages = self._pagination_reverse(*self.groups) |
| 240 |
reverse = True |
| 241 |
|
| 242 |
for page in pages: |
| 243 |
if "thread" not in self.kwdict: |
| 244 |
self.kwdict["thread"] = self._parse_thread(page) |
| 245 |
posts = text.extract_iter(page, "<article ", "</article>") |
| 246 |
if reverse: |
| 247 |
posts = list(posts) |
| 248 |
posts.reverse() |
| 249 |
for html in posts: |
| 250 |
yield self._parse_post(html) |
| 251 |
|
| 252 |
|
| 253 |
class BellazonForumExtractor(BellazonExtractor): |
| 254 |
subcategory = "forum" |
| 255 |
pattern = BASE_PATTERN + r"(/forum/\d+-[^/?#]+)(?:/page/(\d+))?" |
| 256 |
example = "https://www.bellazon.com/main/forum/123-SLUG/" |
| 257 |
|
| 258 |
def items(self): |
| 259 |
data = {"_extractor": BellazonThreadExtractor} |
| 260 |
for page in self._pagination(*self.groups): |
| 261 |
for row in text.extract_iter( |
| 262 |
page, '<li data-ips-hook="topicRow"', "</"): |
| 263 |
yield Message.Queue, text.extr(row, 'href="', '"'), data |