| 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://audiochan.com/""" |
| 10 |
|
| 11 |
from .common import Extractor, Message |
| 12 |
from .. import text |
| 13 |
|
| 14 |
BASE_PATTERN = r"(?:https?://)?(?:www\.)?audiochan\.com" |
| 15 |
|
| 16 |
|
| 17 |
class AudiochanExtractor(Extractor): |
| 18 |
"""Base class for audiochan extractors""" |
| 19 |
category = "audiochan" |
| 20 |
root = "https://audiochan.com" |
| 21 |
root_api = "https://api.audiochan.com" |
| 22 |
directory_fmt = ("{category}", "{user[display_name]}") |
| 23 |
filename_fmt = "{title} ({slug}).{extension}" |
| 24 |
archive_fmt = "{audioFile[id]}" |
| 25 |
|
| 26 |
def _init(self): |
| 27 |
self.user = False |
| 28 |
self.headers_api = { |
| 29 |
"content-type": "application/json", |
| 30 |
} |
| 31 |
self.headers_dl = { |
| 32 |
"Accept": "audio/webm,audio/ogg,audio/wav,audio/*;q=0.9," |
| 33 |
"application/ogg;q=0.7,video/*;q=0.6,*/*;q=0.5", |
| 34 |
"Sec-Fetch-Dest" : "audio", |
| 35 |
"Sec-Fetch-Mode" : "no-cors", |
| 36 |
"Sec-Fetch-Site" : "same-site", |
| 37 |
"Accept-Encoding": "identity", |
| 38 |
} |
| 39 |
|
| 40 |
def items(self): |
| 41 |
for post in self.posts(): |
| 42 |
post["date"] = self.parse_datetime_iso(post["created_at"]) |
| 43 |
post["date_updated"] = self.parse_datetime_iso(post["updated_at"]) |
| 44 |
|
| 45 |
if file := post.get("audioFile"): |
| 46 |
post["_http_headers"] = self.headers_dl |
| 47 |
post["description"] = self._extract_description( |
| 48 |
post["description"]) |
| 49 |
else: |
| 50 |
post["description"] = post.pop("teaser", "") |
| 51 |
|
| 52 |
tags = [] |
| 53 |
for tag in post["tags"]: |
| 54 |
if "tag" in tag: |
| 55 |
tag = tag["tag"] |
| 56 |
tags.append(f"{tag['category']}:{tag['name']}") |
| 57 |
post["tags"] = tags |
| 58 |
|
| 59 |
if self.user: |
| 60 |
for credit in post["credits"]: |
| 61 |
if user := credit.get("user"): |
| 62 |
post["user"] = user |
| 63 |
break |
| 64 |
|
| 65 |
yield Message.Directory, "", post |
| 66 |
if file: |
| 67 |
text.nameext_from_name(file["filename"], post) |
| 68 |
yield Message.Url, self._extract_url(post), post |
| 69 |
|
| 70 |
def request_api(self, endpoint, params=None): |
| 71 |
url = self.root_api + endpoint |
| 72 |
return self.request_json(url, params=params, headers=self.headers_api) |
| 73 |
|
| 74 |
def _pagination(self, endpoint, params, key=None): |
| 75 |
params["page"] = 1 |
| 76 |
params["limit"] = "12" |
| 77 |
|
| 78 |
while True: |
| 79 |
data = self.request_api(endpoint, params) |
| 80 |
if key is not None: |
| 81 |
data = data[key] |
| 82 |
|
| 83 |
yield from data["data"] |
| 84 |
|
| 85 |
if not data["has_more"]: |
| 86 |
break |
| 87 |
|
| 88 |
try: |
| 89 |
if cursor := data["meta"]["pagination"].get("next_cursor"): |
| 90 |
params["cursor"] = cursor |
| 91 |
params.pop("page", None) |
| 92 |
else: |
| 93 |
params["page"] += 1 |
| 94 |
except Exception: |
| 95 |
break |
| 96 |
|
| 97 |
def _extract_url(self, post): |
| 98 |
file = post["audioFile"] |
| 99 |
if url := file.get("url"): |
| 100 |
return url |
| 101 |
|
| 102 |
base = f"{self.root_api}/audios/{post['id']}" |
| 103 |
data = {"file_id": file.get("source_audio_file_id") or file["id"]} |
| 104 |
data["play_intent"] = self.request_json( |
| 105 |
base + "/play-intent", method="POST", headers=self.headers_api, |
| 106 |
json=data)["play_intent"] |
| 107 |
return self.request_json( |
| 108 |
base + "/stream-url", method="POST", headers=self.headers_api, |
| 109 |
json=data)["url"] |
| 110 |
|
| 111 |
def _extract_description(self, description, texts=None): |
| 112 |
if texts is None: |
| 113 |
texts = [] |
| 114 |
|
| 115 |
if "text" in description: |
| 116 |
texts.append(description["text"]) |
| 117 |
elif "content" in description: |
| 118 |
for desc in description["content"]: |
| 119 |
self._extract_description(desc, texts) |
| 120 |
|
| 121 |
return texts |
| 122 |
|
| 123 |
|
| 124 |
class AudiochanAudioExtractor(AudiochanExtractor): |
| 125 |
subcategory = "audio" |
| 126 |
pattern = BASE_PATTERN + r"/a/([^/?#]+)" |
| 127 |
example = "https://audiochan.com/a/SLUG" |
| 128 |
|
| 129 |
def posts(self): |
| 130 |
self.user = True |
| 131 |
audio = self.request_api("/audios/slug/" + self.groups[0]) |
| 132 |
return (audio,) |
| 133 |
|
| 134 |
|
| 135 |
class AudiochanUserExtractor(AudiochanExtractor): |
| 136 |
subcategory = "user" |
| 137 |
pattern = BASE_PATTERN + r"/u/([^/?#]+)" |
| 138 |
example = "https://audiochan.com/u/USER" |
| 139 |
|
| 140 |
def posts(self): |
| 141 |
endpoint = "/users/" + self.groups[0] |
| 142 |
self.kwdict["user"] = self.request_api(endpoint)["data"] |
| 143 |
|
| 144 |
params = { |
| 145 |
"type": "all", |
| 146 |
"content_mode": "all", |
| 147 |
"sort": "new", |
| 148 |
} |
| 149 |
return self._pagination(endpoint + "/content", params) |
| 150 |
|
| 151 |
|
| 152 |
class AudiochanCollectionExtractor(AudiochanExtractor): |
| 153 |
subcategory = "collection" |
| 154 |
pattern = BASE_PATTERN + r"/c/([^/?#]+)" |
| 155 |
example = "https://audiochan.com/c/SLUG" |
| 156 |
|
| 157 |
def posts(self): |
| 158 |
slug = self.groups[0] |
| 159 |
endpoint = "/playlists/" + slug |
| 160 |
self.kwdict["collection"] = col = self.request_api(endpoint) |
| 161 |
col.pop("audios", None) |
| 162 |
col.pop("items", None) |
| 163 |
|
| 164 |
endpoint = f"/playlists/slug/{slug}/audios" |
| 165 |
return self._pagination(endpoint, {}) |
| 166 |
|
| 167 |
|
| 168 |
class AudiochanSearchExtractor(AudiochanExtractor): |
| 169 |
subcategory = "search" |
| 170 |
pattern = BASE_PATTERN + r"/search/?\?([^#]+)" |
| 171 |
example = "https://audiochan.com/search?q=QUERY" |
| 172 |
|
| 173 |
def posts(self): |
| 174 |
self.user = True |
| 175 |
|
| 176 |
params = text.parse_query(self.groups[0]) |
| 177 |
type = params.pop("tab", "audios") |
| 178 |
params.setdefault("type", type) |
| 179 |
params.setdefault("sort", "new") |
| 180 |
params["count_mode"] = "none" |
| 181 |
|
| 182 |
self.kwdict["search_tags"] = params.get("q") |
| 183 |
return self._pagination("/search", params, type) |