|
| 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://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.headers_api = { |
| 28 | + "content-type" : "application/json", |
| 29 | + "Origin" : self.root, |
| 30 | + "Sec-Fetch-Dest" : "empty", |
| 31 | + "Sec-Fetch-Mode" : "cors", |
| 32 | + "Sec-Fetch-Site" : "same-site", |
| 33 | + } |
| 34 | + self.headers_dl = { |
| 35 | + "Accept": "audio/webm,audio/ogg,audio/wav,audio/*;q=0.9," |
| 36 | + "application/ogg;q=0.7,video/*;q=0.6,*/*;q=0.5", |
| 37 | + # "Range" : "bytes=0-", |
| 38 | + "Sec-Fetch-Dest" : "audio", |
| 39 | + "Sec-Fetch-Mode" : "no-cors", |
| 40 | + "Sec-Fetch-Site" : "same-site", |
| 41 | + "Accept-Encoding": "identity", |
| 42 | + } |
| 43 | + |
| 44 | + def items(self): |
| 45 | + for post in self.posts(): |
| 46 | + file = post["audioFile"] |
| 47 | + |
| 48 | + post["_http_headers"] = self.headers_dl |
| 49 | + post["date"] = self.parse_datetime_iso(file["created_at"]) |
| 50 | + post["date_updated"] = self.parse_datetime_iso(file["updated_at"]) |
| 51 | + post["tags"] = [f"{tag['category']}:{tag['name']}" |
| 52 | + for tag in post["tags"]] |
| 53 | + |
| 54 | + yield Message.Directory, post |
| 55 | + text.nameext_from_name(file["filename"], post) |
| 56 | + yield Message.Url, file["url"] or file["stream_url"], post |
| 57 | + |
| 58 | + def request_api(self, endpoint, params=None): |
| 59 | + url = self.root_api + endpoint |
| 60 | + return self.request_json(url, params=params, headers=self.headers_api) |
| 61 | + |
| 62 | + def _pagination(self, endpoint, params): |
| 63 | + params["page"] = 1 |
| 64 | + params["limit"] = "12" |
| 65 | + |
| 66 | + while True: |
| 67 | + data = self.request_api(endpoint, params) |
| 68 | + |
| 69 | + yield from data["data"] |
| 70 | + |
| 71 | + if not data["has_more"]: |
| 72 | + break |
| 73 | + params["page"] += 1 |
| 74 | + |
| 75 | + |
| 76 | +class AudiochanAudioExtractor(AudiochanExtractor): |
| 77 | + subcategory = "audio" |
| 78 | + pattern = rf"{BASE_PATTERN}/a/(\w+)" |
| 79 | + example = "https://audiochan.com/a/SLUG" |
| 80 | + |
| 81 | + def posts(self): |
| 82 | + audio = self.request_api("/audios/slug/" + self.groups[0]) |
| 83 | + audio["user"] = audio["credits"][0]["user"] |
| 84 | + return (audio,) |
| 85 | + |
| 86 | + |
| 87 | +class AudiochanUserExtractor(AudiochanExtractor): |
| 88 | + subcategory = "user" |
| 89 | + pattern = rf"{BASE_PATTERN}/u/(\w+)" |
| 90 | + example = "https://audiochan.com/u/USER" |
| 91 | + |
| 92 | + def posts(self): |
| 93 | + endpoint = "/users/" + self.groups[0] |
| 94 | + self.kwdict["user"] = self.request_api(endpoint)["data"] |
| 95 | + |
| 96 | + params = { |
| 97 | + "sfw_only": "false", |
| 98 | + "sort" : "new", |
| 99 | + } |
| 100 | + return self._pagination(endpoint + "/audios", params) |
| 101 | + |
| 102 | + |
| 103 | +class AudiochanCollectionExtractor(AudiochanExtractor): |
| 104 | + subcategory = "collection" |
| 105 | + pattern = rf"{BASE_PATTERN}/c/(\w+)" |
| 106 | + example = "https://audiochan.com/c/SLUG" |
| 107 | + |
| 108 | + def posts(self): |
| 109 | + slug = self.groups[0] |
| 110 | + endpoint = "/collections/" + slug |
| 111 | + self.kwdict["collection"] = col = self.request_api(endpoint) |
| 112 | + col.pop("audios", None) |
| 113 | + |
| 114 | + endpoint = f"/collections/slug/{slug}/items" |
| 115 | + return self._pagination(endpoint, {}) |
0 commit comments