Skip to content

Commit

Permalink
v2.0.2
Browse files Browse the repository at this point in the history
- Content not syncing when MAL ID is given instead of Kitsu ID
- Refactor stream.py
  • Loading branch information
SageTendo committed Feb 23, 2024
1 parent b32d7f6 commit 483f53a
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 38 deletions.
54 changes: 28 additions & 26 deletions app/routes/content_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from flask import Blueprint, abort
from requests import HTTPError

from app.routes import IMDB_ID_PREFIX, mal_client
from app.routes import IMDB_ID_PREFIX, mal_client, MAL_ID_PREFIX
from app.routes.catalog import get_token
from app.routes.manifest import MANIFEST
from app.routes.utils import respond_with, log_error
Expand All @@ -27,36 +27,38 @@ def addon_content_sync(user_id: str, content_type: str, content_id: str, video_h
:param video_hash: The hash of the video (ignored)
:return: JSON response
"""
if IMDB_ID_PREFIX in content_id:
return respond_with({})
mal_id = ""
current_episode = 1

if content_type not in MANIFEST['types']:
abort(404)

# Extract the anime_id and episode from the content_id
content_id = urllib.parse.unquote(content_id)
if content_id.count(':') == 1:
anime_id = content_id.replace('kitsu:', '')
current_episode = 1
else:
_, anime_id, current_episode = content_id.split(':')
current_episode = int(current_episode)

# Fetch myanimelist id from mapper
resp = httpx.get(haglund_API, params={'source': 'kitsu', 'id': int(anime_id)})
if resp.status_code >= 400:
logging.error(resp.status_code, resp.reason_phrase)
abort(404)

data = resp.json()
if data.get('myanimelist', None) is None:
logging.warning("No id for MyAnimeList found")
abort(404)
if (IMDB_ID_PREFIX in content_id) or (content_type not in MANIFEST['types']):
return respond_with({'subtitles': []})

if content_id.startswith('kitsu:'):

content_id = content_id.replace('kitsu:', '')
if content_id.count(':') == 0: # Handle movie
anime_id = content_id
else: # Handle episode
anime_id, current_episode = content_id.split(':')
current_episode = int(current_episode)

# Fetch MyAnimeList ID from mapper
resp = httpx.get(haglund_API, params={'source': 'kitsu', 'id': int(anime_id)})
if resp.status_code >= 400:
logging.error(resp.status_code, resp.reason_phrase)
abort(404)

mal_id = resp.json().get('myanimelist', None)
if mal_id is None:
logging.warning("No id for MyAnimeList found")
abort(404)

elif content_id.startswith(MAL_ID_PREFIX):
mal_id = content_id.replace(f"{MAL_ID_PREFIX}_", '')

# Get anime details
token = get_token(user_id)
mal_id = data.get('myanimelist')

resp = mal_client.get_anime_details(token, mal_id, fields='num_episodes my_list_status')
total_episodes = resp.get('num_episodes', 0)
current_status = resp.get('my_list_status', {}).get('status', None)
Expand Down
2 changes: 1 addition & 1 deletion app/routes/manifest.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

MANIFEST = {
'id': 'com.sagetendo.mal-stremio-addon',
'version': '2.0.0',
'version': '2.0.2',
'name': 'MAL-Stremio Addon',
'logo': 'https://i.imgur.com/zVYdffr.png',
'description': 'Provides users with watchlist content from MyAnimeList within Stremio. '
Expand Down
23 changes: 12 additions & 11 deletions app/routes/stream.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import httpx
from flask import Blueprint, abort

from app.routes import IMDB_ID_PREFIX, MAL_ID_PREFIX
from app.routes import MAL_ID_PREFIX
from app.routes.content_sync import haglund_API
from app.routes.utils import respond_with

Expand All @@ -19,20 +19,22 @@

@stream_bp.route('/<user_id>/stream/<content_type>/<content_id>.json')
async def addon_stream(user_id: str, content_type: str, content_id: str):
if MAL_ID_PREFIX not in content_id:
return respond_with({})

if content_type != 'movie':
"""
Provide torrentio streams for movie content
:param user_id: The id of the user requesting the content
:param content_type: The type of content
:param content_id: The id of the content
:return: JSON response
"""
content_id = urllib.parse.unquote(content_id)
if (MAL_ID_PREFIX not in content_id) or (content_type != 'movie'):
return respond_with({})

# Fetch kitsu id from mapper
content_id = urllib.parse.unquote(content_id)
content_id = content_id.replace(f"{MAL_ID_PREFIX}_", '')
content_id = int(content_id)

content_id = int(content_id.replace(f"{MAL_ID_PREFIX}_", ''))
async with httpx.AsyncClient() as client:
resp = await client.get(haglund_API, params={'source': 'myanimelist', 'id': content_id})

resp = await client.get(haglund_API, params={'source': 'myanimelist', 'id': content_id})
if resp.status_code >= 400:
logging.error(resp.status_code, resp.reason_phrase)
abort(404)
Expand All @@ -44,6 +46,5 @@ async def addon_stream(user_id: str, content_type: str, content_id: str):
abort(404)

kitsu_id = f"kitsu:{kitsu_id}"

resp = await client.get(f"{torrentio_api}/stream/{content_type}/{kitsu_id}.json")
return respond_with(resp.json())

0 comments on commit 483f53a

Please sign in to comment.