|
| 1 | +import json, re, requests |
| 2 | +from typing import List, Optional |
| 3 | +from bs4 import BeautifulSoup |
| 4 | +from urllib.parse import urljoin |
| 5 | +from weeb_cli.providers.base import BaseProvider, AnimeResult, AnimeDetails, Episode, StreamLink |
| 6 | +from weeb_cli.providers.registry import register_provider |
| 7 | +from weeb_cli.services.logger import debug |
| 8 | + |
| 9 | +BASE_URL = "https://aniworld.to" |
| 10 | +AJAX_URL = "https://aniworld.to/ajax/search" |
| 11 | +STREAM_BASE = "https://aniworld.to/anime/stream/" |
| 12 | + |
| 13 | +@register_provider(name="aniworld", lang="de", region="DE") |
| 14 | +class AniWorldProvider(BaseProvider): |
| 15 | + def __init__(self): |
| 16 | + super().__init__() |
| 17 | + self.session = requests.Session() |
| 18 | + self.headers = { |
| 19 | + "User-Agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36", |
| 20 | + "Referer": "https://aniworld.to/" |
| 21 | + } |
| 22 | + |
| 23 | + def _get(self, url): |
| 24 | + try: |
| 25 | + resp = self.session.get(url, headers=self.headers, timeout=10) |
| 26 | + resp.raise_for_status() |
| 27 | + return resp.text |
| 28 | + except Exception as e: |
| 29 | + debug(f"[AniWorld] GET Error: {e}"); return "" |
| 30 | + |
| 31 | + def _post(self, url, data): |
| 32 | + try: |
| 33 | + h = self.headers.copy(); h["X-Requested-With"] = "XMLHttpRequest" |
| 34 | + resp = self.session.post(url, data=data, headers=h, timeout=10) |
| 35 | + return resp.text |
| 36 | + except Exception as e: |
| 37 | + debug(f"[AniWorld] POST Error: {e}"); return "" |
| 38 | + |
| 39 | + def search(self, query: str) -> List[AnimeResult]: |
| 40 | + res = self._post(AJAX_URL, {"keyword": query}) |
| 41 | + if not res: return [] |
| 42 | + try: |
| 43 | + data = json.loads(res) |
| 44 | + results = [] |
| 45 | + for item in data: |
| 46 | + link = item.get("link", "") |
| 47 | + if link.startswith("/anime/stream/"): |
| 48 | + slug = link.replace("/anime/stream/", "").split("/")[0] |
| 49 | + title = item.get("title", "").replace("<em>", "").replace("</em>", "") |
| 50 | + results.append(AnimeResult(id=slug, title=title, type="series")) |
| 51 | + return results |
| 52 | + except: return [] |
| 53 | + |
| 54 | + def get_details(self, anime_id: str) -> Optional[AnimeDetails]: |
| 55 | + slug = anime_id.split("/")[0] |
| 56 | + html = self._get(urljoin(STREAM_BASE, slug)) |
| 57 | + if not html: return None |
| 58 | + soup = BeautifulSoup(html, "html.parser") |
| 59 | + title = soup.find("h1", itemprop="name").text.strip() if soup.find("h1", itemprop="name") else slug |
| 60 | + season_matches = re.findall(r"staffel-(\d+)", html) |
| 61 | + unique_seasons = sorted(list(set(int(s) for s in season_matches))) |
| 62 | + if not unique_seasons: unique_seasons = [1] |
| 63 | + all_episodes = [] |
| 64 | + for s_num in unique_seasons: |
| 65 | + s_html = self._get(f"{STREAM_BASE}{slug}/staffel-{s_num}") |
| 66 | + ep_matches = re.findall(f"staffel-{s_num}/episode-(\\d+)", s_html) |
| 67 | + unique_eps = sorted(list(set(int(m) for m in ep_matches))) |
| 68 | + for e_num in unique_eps: |
| 69 | + all_episodes.append(Episode( |
| 70 | + id=f"{slug}/staffel-{s_num}/episode-{e_num}", |
| 71 | + number=e_num, title=f"Folge {e_num}" if s_num > 0 else f"Film {e_num}", |
| 72 | + season=s_num |
| 73 | + )) |
| 74 | + return AnimeDetails(id=slug, title=title, description="", cover=None, total_episodes=len(all_episodes), episodes=all_episodes) |
| 75 | + |
| 76 | + def get_episodes(self, anime_id: str, season: int = 1) -> List[Episode]: |
| 77 | + slug = anime_id.split("/")[0] |
| 78 | + html = self._get(f"{STREAM_BASE}{slug}/staffel-{season}") |
| 79 | + if not html: return [] |
| 80 | + ep_matches = re.findall(f"staffel-{season}/episode-(\\d+)", html) |
| 81 | + unique_eps = sorted(list(set(int(m) for m in ep_matches))) |
| 82 | + return [Episode(id=f"{slug}/staffel-{season}/episode-{num}", number=num, title=f"Folge {num}" if season > 0 else f"Film {num}", season=season) for num in unique_eps] |
| 83 | + |
| 84 | + def get_streams(self, anime_id: str, episode_id: str) -> List[StreamLink]: |
| 85 | + from weeb_cli.providers.extractors.voe import extract_voe |
| 86 | + from weeb_cli.providers.extractors.filemoon import extract_filemoon |
| 87 | + from weeb_cli.providers.extractors.streamtape import extract_streamtape |
| 88 | + from weeb_cli.providers.extractors.vidoza import extract_vidoza |
| 89 | + from weeb_cli.providers.extractors.doodstream import extract_doodstream |
| 90 | + html = self._get(urljoin(STREAM_BASE, episode_id)) |
| 91 | + if not html: return [] |
| 92 | + soup = BeautifulSoup(html, "html.parser") |
| 93 | + lang_map = {"1": "GerDub", "2": "GerSub", "3": "EngSub"} |
| 94 | + streams = [] |
| 95 | + hoster_items = soup.find_all("li", attrs={"data-link-target": True}) |
| 96 | + for item in hoster_items: |
| 97 | + target = item["data-link-target"] |
| 98 | + if not target.startswith("/redirect/"): continue |
| 99 | + l_key = item.get("data-lang-key", "0") |
| 100 | + lang_name = lang_map.get(l_key, "N/A") |
| 101 | + h_name = "Unknown" |
| 102 | + icon = item.find("i", class_="icon") |
| 103 | + if icon: |
| 104 | + for cls in icon.get("class", []): |
| 105 | + if cls != "icon": h_name = cls; break |
| 106 | + if h_name == "Unknown" and item.find("h4"): h_name = item.find("h4").text.strip() |
| 107 | + try: |
| 108 | + resp = self.session.get(urljoin(BASE_URL, target), headers=self.headers, timeout=10, allow_redirects=True) |
| 109 | + e_url = resp.url |
| 110 | + v_url = None; h_low = h_name.lower() |
| 111 | + if "voe" in h_low: v_url = extract_voe(e_url) |
| 112 | + elif "filemoon" in h_low: v_url = extract_filemoon(e_url) |
| 113 | + elif "streamtape" in h_low: v_url = extract_streamtape(e_url) |
| 114 | + elif "vidoza" in h_low: v_url = extract_vidoza(e_url) |
| 115 | + elif "dood" in h_low: v_url = extract_doodstream(e_url) |
| 116 | + f_url = v_url or e_url |
| 117 | + if f_url: streams.append(StreamLink(url=f_url, quality=f"[{lang_name}]", server=h_name)) |
| 118 | + except: continue |
| 119 | + return streams |
0 commit comments