|
| 1 | +import re |
| 2 | +from typing import List |
| 3 | +from urllib.parse import urljoin |
| 4 | + |
| 5 | +import Levenshtein |
| 6 | +import m3u8 |
| 7 | +from anipy_api.provider import (BaseProvider, Episode, ProviderInfoResult, |
| 8 | + ProviderSearchResult, ProviderStream) |
| 9 | +from anipy_api.provider.base import LanguageTypeEnum |
| 10 | +from anipy_api.provider.filter import (BaseFilter, FilterCapabilities, Filters, |
| 11 | + MediaType, Season, Status) |
| 12 | +from anipy_api.provider.utils import parsenum |
| 13 | +from anipy_api.provider import Episode |
| 14 | +from anipy_api.error import LangTypeNotAvailableError |
| 15 | + |
| 16 | + |
| 17 | +from bs4 import BeautifulSoup |
| 18 | + |
| 19 | +from requests import Request |
| 20 | + |
| 21 | +HLS_RE = re.compile( |
| 22 | + r"(?:file\s*:\s*|source\s*=\s*)[\"']([^\"']+\.m3u8(?:\?[^\"']*)?)[\"']", |
| 23 | + re.IGNORECASE, |
| 24 | +) |
| 25 | + |
| 26 | +class AniDBAppFilter(BaseFilter): |
| 27 | + def _apply_query(self, query: str): |
| 28 | + self._request.params["q"] = query |
| 29 | + |
| 30 | + def _apply_year(self, year: int): |
| 31 | + self._request.params["year"] = year |
| 32 | + |
| 33 | + def _apply_season(self, season: Season): |
| 34 | + self._request.params["season"] = season.name.lower() |
| 35 | + |
| 36 | + def _apply_status(self, status: Status): |
| 37 | + mapping = { |
| 38 | + Status.COMPLETED: "Finished Airing", |
| 39 | + Status.ONGOING: "Currently Airing", |
| 40 | + Status.UPCOMING: "" |
| 41 | + } |
| 42 | + self._request.params["status"] = mapping[status] |
| 43 | + |
| 44 | + def _apply_media_type(self, media_type: MediaType): |
| 45 | + mapping = { |
| 46 | + MediaType.TV: "TV", |
| 47 | + MediaType.MOVIE: "Movie", |
| 48 | + MediaType.OVA: "OVA", |
| 49 | + MediaType.ONA: "ONA", |
| 50 | + MediaType.SPECIAL: "Special", |
| 51 | + MediaType.MUSIC: "Music" |
| 52 | + } |
| 53 | + self._request.params["type"] = mapping[media_type] |
| 54 | + |
| 55 | +class AniDBAppProvider(BaseProvider): |
| 56 | + NAME: str = "anidbapp" |
| 57 | + BASE_URL: str = "https://anidb.app" |
| 58 | + FILTER_CAPS: FilterCapabilities = FilterCapabilities.ALL |
| 59 | + |
| 60 | + def get_search( |
| 61 | + self, query: str, filters: Filters = Filters() |
| 62 | + ) -> List[ProviderSearchResult]: |
| 63 | + req = Request("GET", f"{self.BASE_URL}/browse") |
| 64 | + req = AniDBAppFilter(req).apply(query, filters) |
| 65 | + res = self._request_page(req) |
| 66 | + |
| 67 | + current_page = BeautifulSoup(res.text, "html.parser") |
| 68 | + pages = current_page.find("span", attrs={"class": "text-sm text-muted"}) |
| 69 | + if pages: |
| 70 | + pages = parsenum(pages.findChildren()[-1].text) |
| 71 | + else: |
| 72 | + pages = 1 |
| 73 | + |
| 74 | + results: list[ProviderSearchResult] = [] |
| 75 | + |
| 76 | + for p in range(2 if pages > 1 else 1, pages + 1): |
| 77 | + anime = current_page.find("div", attrs={"class": "anime-grid"}).findAll( |
| 78 | + "a", attrs={"class": "anime-card"} |
| 79 | + ) |
| 80 | + |
| 81 | + for a in anime: |
| 82 | + if a is None: |
| 83 | + continue |
| 84 | + |
| 85 | + name = a.p.text |
| 86 | + link = a["href"] |
| 87 | + ident = link.split("-")[-1] |
| 88 | + results.append(ProviderSearchResult( |
| 89 | + identifier=ident, name=name, languages={LanguageTypeEnum.UND} |
| 90 | + )) |
| 91 | + |
| 92 | + req.params["page"] = p |
| 93 | + res = self._request_page(req) |
| 94 | + current_page = BeautifulSoup(res.text, "html.parser") |
| 95 | + |
| 96 | + results.sort( |
| 97 | + key=lambda x: Levenshtein.ratio(query, x.name, processor=str.lower), |
| 98 | + reverse=True, |
| 99 | + ) |
| 100 | + |
| 101 | + return results |
| 102 | + |
| 103 | + def get_info(self, identifier: str) -> ProviderInfoResult: |
| 104 | + req = Request( |
| 105 | + "GET", |
| 106 | + f"{self.BASE_URL}/anime/anime-{identifier}", |
| 107 | + ) |
| 108 | + res = self._request_page(req) |
| 109 | + |
| 110 | + soup = BeautifulSoup(res.text, "html.parser") |
| 111 | + |
| 112 | + name = soup.find("h1", attrs={"class": "leading-tight"}) |
| 113 | + if name: |
| 114 | + name = name.text |
| 115 | + |
| 116 | + image = soup.find("img", attrs={"class": "object-cover"}) |
| 117 | + if image: |
| 118 | + image = image["src"] |
| 119 | + |
| 120 | + year = soup.find("a", attrs={"href": re.compile(r"\/browse\?season=\w+&year=\d+")}) |
| 121 | + if year: |
| 122 | + year = parsenum(year.text.split()[-1]) |
| 123 | + |
| 124 | + genre_container = soup.findAll("div", attrs={"class": "flex flex-wrap gap-1.5 mb-4"}) |
| 125 | + |
| 126 | + genres = genre_container[-1].findAll("a", attrs={"href": re.compile(r"\/(genres|themes)\/.+")}) |
| 127 | + genres = [g.text for g in genres] |
| 128 | + |
| 129 | + status = soup.find("a", attrs={"href": re.compile(r"\/browse\?status=.+")}) |
| 130 | + if status: |
| 131 | + mapping = { |
| 132 | + "Finished Airing": Status.COMPLETED, |
| 133 | + "Currently Airing": Status.ONGOING, |
| 134 | + } |
| 135 | + status = mapping.get(status.text, None) |
| 136 | + |
| 137 | + synopsis = soup.find("p", attrs={"class": "text-sm text-faint leading-relaxed"}) |
| 138 | + if synopsis: |
| 139 | + synopsis = synopsis.text |
| 140 | + |
| 141 | + synonyms = soup.find("dt", text=re.compile(r"Synonyms")) |
| 142 | + if synonyms: |
| 143 | + synonyms = [s.strip() for s in synonyms.findNextSibling().text.split(",")] |
| 144 | + |
| 145 | + return ProviderInfoResult( |
| 146 | + name = name, |
| 147 | + image = image, |
| 148 | + genres = genres, |
| 149 | + synopsis = synopsis, |
| 150 | + status = status, |
| 151 | + release_year = year, |
| 152 | + alternative_names = synonyms |
| 153 | + ) |
| 154 | + |
| 155 | + def get_episodes(self, identifier: str, lang: LanguageTypeEnum) -> List[Episode]: |
| 156 | + req = Request( |
| 157 | + "GET", |
| 158 | + f"{self.BASE_URL}/api/frontend/anime/{identifier}/episodes", |
| 159 | + ) |
| 160 | + res = self._request_page(req).json() |
| 161 | + return [e["number"] for e in res["episodes"]] |
| 162 | + |
| 163 | + def get_video( |
| 164 | + self, identifier: str, episode: Episode, lang: LanguageTypeEnum |
| 165 | + ) -> List[ProviderStream]: |
| 166 | + req = Request( |
| 167 | + "GET", |
| 168 | + f"{self.BASE_URL}/api/frontend/anime/{identifier}/episodes", |
| 169 | + ) |
| 170 | + res = self._request_page(req).json().get("episodes", []) |
| 171 | + episode_id = next(filter(lambda e: e["number"] == episode, res))["id"] |
| 172 | + |
| 173 | + req = Request( |
| 174 | + "GET", |
| 175 | + f"{self.BASE_URL}/api/frontend/episode/{episode_id}/languages", |
| 176 | + ) |
| 177 | + res = self._request_page(req).json().get("languages", []) |
| 178 | + if not res: |
| 179 | + return [] |
| 180 | + |
| 181 | + lang_short = "eng" if lang == LanguageTypeEnum.DUB else "jpn" |
| 182 | + |
| 183 | + embed_url = list(filter(lambda l: l["code"] == lang_short, res)) |
| 184 | + if not embed_url: |
| 185 | + raise LangTypeNotAvailableError(identifier, self.NAME, lang) |
| 186 | + else: |
| 187 | + embed_url = embed_url[0]["embed_url"] |
| 188 | + |
| 189 | + req = Request( |
| 190 | + "GET", |
| 191 | + embed_url, |
| 192 | + ) |
| 193 | + res = self._request_page(req) |
| 194 | + |
| 195 | + streams = HLS_RE.findall(res.text) |
| 196 | + if not streams: |
| 197 | + return [] |
| 198 | + |
| 199 | + sub_playlists = [] |
| 200 | + |
| 201 | + for s in streams: |
| 202 | + req = Request("GET", s) |
| 203 | + res = self._request_page(req) |
| 204 | + |
| 205 | + content = m3u8.M3U8(res.text, base_uri=urljoin(res.url, ".")) |
| 206 | + |
| 207 | + if len(content.playlists) == 0: |
| 208 | + sub_playlists.append( |
| 209 | + ProviderStream( |
| 210 | + url=s, |
| 211 | + resolution=1080, |
| 212 | + episode=episode, |
| 213 | + language=lang, |
| 214 | + container="hls", |
| 215 | + ) |
| 216 | + ) |
| 217 | + |
| 218 | + for sub_playlist in content.playlists: |
| 219 | + sub_playlists.append( |
| 220 | + ProviderStream( |
| 221 | + url=urljoin(content.base_uri, sub_playlist.uri), |
| 222 | + resolution=sub_playlist.stream_info.resolution[1], |
| 223 | + episode=episode, |
| 224 | + language=lang, |
| 225 | + container="hls", |
| 226 | + ) |
| 227 | + ) |
| 228 | + return sub_playlists |
0 commit comments