sigh
This commit is contained in:
@@ -1,12 +1,11 @@
|
||||
{
|
||||
"name": "AnimeKai Streamer",
|
||||
"version": "1.2.1",
|
||||
"version": "1.3.0",
|
||||
"author": "Animex",
|
||||
"description": "Resolves AnimeKai.to streams using backend encrypted APIs. Fully optimized for Cloud Tunneling.",
|
||||
"description": "Resolves AnimeKai.to streams using backend encrypted APIs via Cloud Tunnel. Supports Sub and Dub. No Selenium.",
|
||||
"type": ["ANIME_STREAMER"],
|
||||
"requirements":["httpx", "beautifulsoup4"]
|
||||
"requirements": ["httpx", "beautifulsoup4"]
|
||||
}
|
||||
|
||||
---
|
||||
import re
|
||||
import httpx
|
||||
@@ -36,37 +35,37 @@ HEADERS = {
|
||||
# ENCRYPT / DECRYPT HELPERS
|
||||
# =========================
|
||||
|
||||
async def enc_kai(client, text: str) -> str:
|
||||
r = await client.get(
|
||||
async def enc_kai(text: str) -> str:
|
||||
r = await httpx.get(
|
||||
f"{ENC_API}/enc-kai",
|
||||
params={"text": text},
|
||||
headers=HEADERS,
|
||||
timeout=10
|
||||
timeout=10,
|
||||
headers=HEADERS
|
||||
)
|
||||
r.raise_for_status()
|
||||
return r.json()["result"]
|
||||
|
||||
|
||||
async def dec_kai(client, text: str) -> str:
|
||||
r = await client.post(
|
||||
async def dec_kai(text: str) -> str:
|
||||
r = await httpx.post(
|
||||
f"{ENC_API}/dec-kai",
|
||||
json={"text": text},
|
||||
headers=HEADERS,
|
||||
timeout=10
|
||||
timeout=10,
|
||||
headers=HEADERS
|
||||
)
|
||||
r.raise_for_status()
|
||||
return r.json()["result"]
|
||||
|
||||
|
||||
async def dec_mega(client, text: str) -> Dict:
|
||||
r = await client.post(
|
||||
async def dec_mega(text: str) -> Dict:
|
||||
r = await httpx.post(
|
||||
f"{ENC_API}/dec-mega",
|
||||
json={
|
||||
"text": text,
|
||||
"agent": HEADERS["User-Agent"]
|
||||
},
|
||||
headers=HEADERS,
|
||||
timeout=15
|
||||
timeout=15,
|
||||
headers=HEADERS
|
||||
)
|
||||
r.raise_for_status()
|
||||
return r.json()
|
||||
@@ -75,9 +74,9 @@ async def dec_mega(client, text: str) -> Dict:
|
||||
# INTERNAL LOGIC
|
||||
# =========================
|
||||
|
||||
async def get_anime_id(client, slug: str) -> str:
|
||||
# slug is actually mal_id (int)
|
||||
resp = await client.get(f"{DB_URL}?mal_id={slug}", headers=HEADERS)
|
||||
async def get_anime_id(slug: str) -> tuple[str, dict]:
|
||||
# slug is actually mal_id (int or str)
|
||||
resp = await httpx.get(f"{DB_URL}?mal_id={slug}", headers=HEADERS)
|
||||
resp.raise_for_status()
|
||||
data = resp.json()
|
||||
if not data or not data[0].get("info", {}).get("kai_id"):
|
||||
@@ -85,19 +84,29 @@ async def get_anime_id(client, slug: str) -> str:
|
||||
return data[0]["info"]["kai_id"], data[0]
|
||||
|
||||
|
||||
async def get_episode_token(data: dict, episode: int) -> str:
|
||||
# Always fetch the sub ("1") token for the episode
|
||||
try:
|
||||
token = data["episodes"]["1"][str(episode)]["token"]
|
||||
except Exception:
|
||||
raise RuntimeError("Episode token not found in JSON response")
|
||||
return token
|
||||
async def get_episode_token(data: dict, episode: int, dub: bool) -> str:
|
||||
# "1" = Sub, "2" = Dub in this specific database structure
|
||||
primary_key = "2" if dub else "1"
|
||||
fallback_key = "1" if dub else "2"
|
||||
|
||||
ep_str = str(episode)
|
||||
episodes_data = data.get("episodes", {})
|
||||
|
||||
# Try requested language first
|
||||
if primary_key in episodes_data and ep_str in episodes_data[primary_key]:
|
||||
return episodes_data[primary_key][ep_str]["token"]
|
||||
|
||||
# Fallback to the other language if requested isn't available
|
||||
if fallback_key in episodes_data and ep_str in episodes_data[fallback_key]:
|
||||
return episodes_data[fallback_key][ep_str]["token"]
|
||||
|
||||
raise RuntimeError(f"Episode token not found for episode {episode}")
|
||||
|
||||
|
||||
async def get_server_id(client, ep_token: str, dub: bool) -> str:
|
||||
enc = await enc_kai(client, ep_token)
|
||||
async def get_server_id(ep_token: str, dub: bool) -> List[str]:
|
||||
enc = await enc_kai(ep_token)
|
||||
|
||||
html = await client.get(
|
||||
html = await httpx.get(
|
||||
f"{BASE_URL}/ajax/links/list",
|
||||
params={"token": ep_token, "_": enc},
|
||||
headers=HEADERS
|
||||
@@ -112,7 +121,7 @@ async def get_server_id(client, ep_token: str, dub: bool) -> str:
|
||||
# Determine which language group to use
|
||||
# Default: sub (Hard Sub), or dub if requested
|
||||
lang_order = ["dub", "sub"] if dub else ["sub", "softsub", "dub"]
|
||||
all_server_ids =[]
|
||||
all_server_ids = []
|
||||
for lang in lang_order:
|
||||
group_pattern = rf'<div class="server-items lang-group" data-id="{lang}"[^>]*>(.*?)</div>'
|
||||
group_match = re.search(group_pattern, html_content, re.DOTALL)
|
||||
@@ -128,16 +137,16 @@ async def get_server_id(client, ep_token: str, dub: bool) -> str:
|
||||
return all_server_ids
|
||||
|
||||
|
||||
async def resolve_stream_url(client, server_id: str) -> dict:
|
||||
enc = await enc_kai(client, server_id)
|
||||
async def resolve_stream_url(server_id: str) -> dict:
|
||||
enc = await enc_kai(server_id)
|
||||
|
||||
view = await client.get(
|
||||
view = await httpx.get(
|
||||
f"{BASE_URL}/ajax/links/view",
|
||||
params={"id": server_id, "_": enc},
|
||||
headers=HEADERS
|
||||
)
|
||||
|
||||
decrypted = await dec_kai(client, view.json()["result"])
|
||||
decrypted = await dec_kai(view.json()["result"])
|
||||
print(f"decrypted (dec_kai): {decrypted}")
|
||||
|
||||
skip = {}
|
||||
@@ -153,15 +162,15 @@ async def resolve_stream_url(client, server_id: str) -> dict:
|
||||
parsed = urllib.parse.urlparse(media_url)
|
||||
referer = f"{parsed.scheme}://{parsed.netloc}"
|
||||
|
||||
media = await client.get(media_url, headers=HEADERS)
|
||||
mega = await dec_mega(client, media.json()["result"])
|
||||
media = await httpx.get(media_url, headers=HEADERS)
|
||||
mega = await dec_mega(media.json()["result"])
|
||||
print(mega)
|
||||
|
||||
sources = mega.get("result", {}).get("sources",[])
|
||||
tracks = mega.get("result", {}).get("tracks",[])
|
||||
sources = mega.get("result", {}).get("sources", [])
|
||||
tracks = mega.get("result", {}).get("tracks", [])
|
||||
|
||||
# Find captions and thumbnails
|
||||
captions =[t for t in tracks if t.get("kind") == "captions"]
|
||||
captions = [t for t in tracks if t.get("kind") == "captions"]
|
||||
thumbnails = [t for t in tracks if t.get("kind") == "thumbnails"]
|
||||
|
||||
if not sources:
|
||||
@@ -180,32 +189,25 @@ async def resolve_stream_url(client, server_id: str) -> dict:
|
||||
# PUBLIC MODULE API
|
||||
# =========================
|
||||
|
||||
async def get_iframe_source(
|
||||
mal_id: int,
|
||||
episode: int,
|
||||
dub: bool
|
||||
) -> Optional[str]:
|
||||
async def get_iframe_source(mal_id: int, episode: int, dub: bool) -> Optional[str]:
|
||||
"""
|
||||
Returns resolved m3u8 stream URL using the injected Tunneling HybridClient
|
||||
Returns resolved m3u8 stream URL integrated into the player link
|
||||
"""
|
||||
# The 'httpx' variable resolves to the injected HybridClient dynamically
|
||||
# replaced during module processing in app.py's `get_iframe_source` endpoint.
|
||||
client = httpx
|
||||
|
||||
try:
|
||||
ani_id, anime_json = await get_anime_id(client, mal_id)
|
||||
ani_id, anime_json = await get_anime_id(str(mal_id))
|
||||
print(f"ani_id: {ani_id}")
|
||||
|
||||
ep_token = await get_episode_token(anime_json, episode)
|
||||
# Pass the dub parameter so it checks for the correct sub/dub token
|
||||
ep_token = await get_episode_token(anime_json, episode, dub)
|
||||
print(f"ep_token: {ep_token}")
|
||||
|
||||
server_ids = await get_server_id(client, ep_token, dub)
|
||||
server_ids = await get_server_id(ep_token, dub)
|
||||
print(f"server_ids: {server_ids}")
|
||||
|
||||
for sid in server_ids:
|
||||
try:
|
||||
print(f"Trying server_id: {sid}")
|
||||
result = await resolve_stream_url(client, sid)
|
||||
result = await resolve_stream_url(sid)
|
||||
|
||||
if result:
|
||||
url = result["url"]
|
||||
@@ -234,13 +236,11 @@ async def get_iframe_source(
|
||||
|
||||
player_url += f"&id={mal_id}&episode={episode}&video={urllib.parse.quote(url)}"
|
||||
return player_url
|
||||
|
||||
except Exception as e:
|
||||
print(f"Failed to resolve server {sid}: {e}")
|
||||
|
||||
print("All servers failed.")
|
||||
return None
|
||||
|
||||
except Exception as e:
|
||||
import traceback
|
||||
print(f"Exception in get_iframe_source: {type(e).__name__}: {e}")
|
||||
@@ -248,12 +248,7 @@ async def get_iframe_source(
|
||||
return None
|
||||
|
||||
|
||||
async def get_download_link(
|
||||
mal_id: int,
|
||||
episode: int,
|
||||
dub: bool,
|
||||
quality: str
|
||||
) -> Optional[str]:
|
||||
async def get_download_link(mal_id: int, episode: int, dub: bool, quality: str) -> Optional[str]:
|
||||
"""
|
||||
Alias for get_iframe_source (AnimeKai uses adaptive m3u8)
|
||||
"""
|
||||
|
||||
Reference in New Issue
Block a user