weebzone commited on
Commit ·
b8cbe09
1
Parent(s): 031f7ea
`V-1.1.2`
Browse files* Fixed TMDb 404 (episode not found) errors
* Improved fallback logic for IMDb/TMDb metadata
* Enhanced error handling & logging for TV episodes
- Backend/__init__.py +1 -1
- Backend/helper/database.py +1 -1
- Backend/helper/metadata.py +62 -28
- pyproject.toml +1 -1
- uv.lock +1 -1
Backend/__init__.py
CHANGED
|
@@ -11,4 +11,4 @@ StartTime = time()
|
|
| 11 |
USE_DEFAULT_ID: str = None
|
| 12 |
db = Database()
|
| 13 |
|
| 14 |
-
__version__ = "1.1.
|
|
|
|
| 11 |
USE_DEFAULT_ID: str = None
|
| 12 |
db = Database()
|
| 13 |
|
| 14 |
+
__version__ = "1.1.2"
|
Backend/helper/database.py
CHANGED
|
@@ -9,7 +9,7 @@ from typing import Dict, List, Optional, Tuple, Any
|
|
| 9 |
from Backend.logger import LOGGER
|
| 10 |
from Backend.config import Telegram
|
| 11 |
import re
|
| 12 |
-
from Backend.helper.encrypt import decode_string
|
| 13 |
from Backend.helper.modal import Episode, MovieSchema, QualityDetail, Season, TVShowSchema
|
| 14 |
from Backend.helper.task_manager import delete_message
|
| 15 |
|
|
|
|
| 9 |
from Backend.logger import LOGGER
|
| 10 |
from Backend.config import Telegram
|
| 11 |
import re
|
| 12 |
+
from Backend.helper.encrypt import decode_string
|
| 13 |
from Backend.helper.modal import Episode, MovieSchema, QualityDetail, Season, TVShowSchema
|
| 14 |
from Backend.helper.task_manager import delete_message
|
| 15 |
|
Backend/helper/metadata.py
CHANGED
|
@@ -11,10 +11,10 @@ import Backend
|
|
| 11 |
from Backend.logger import LOGGER
|
| 12 |
from Backend.helper.encrypt import encode_string
|
| 13 |
|
|
|
|
| 14 |
DELAY = 2
|
| 15 |
tmdb = aioTMDb(key=Telegram.TMDB_API, language="en-US", region="US")
|
| 16 |
|
| 17 |
-
|
| 18 |
# ----------------- Helpers -----------------
|
| 19 |
def format_tmdb_image(path: str, size="w500", fallback="https://i.ibb.co/mrfTVzyW/Screenshot-20251006-232731.png") -> str:
|
| 20 |
return f"https://image.tmdb.org/t/p/{size}{path}" if path else fallback
|
|
@@ -29,6 +29,7 @@ def format_imdb_images(imdb_id: str) -> dict:
|
|
| 29 |
|
| 30 |
|
| 31 |
async def safe_imdb_search(title: str, type_: str) -> str | None:
|
|
|
|
| 32 |
try:
|
| 33 |
result = await search_title(query=title, type=type_)
|
| 34 |
return result["id"] if result else None
|
|
@@ -38,9 +39,13 @@ async def safe_imdb_search(title: str, type_: str) -> str | None:
|
|
| 38 |
|
| 39 |
|
| 40 |
async def safe_tmdb_search(title: str, type_: str, year=None):
|
|
|
|
| 41 |
try:
|
| 42 |
if type_ == "movie":
|
| 43 |
-
|
|
|
|
|
|
|
|
|
|
| 44 |
else:
|
| 45 |
results = await tmdb.search().tv(query=title)
|
| 46 |
return results[0] if results else None
|
|
@@ -49,7 +54,7 @@ async def safe_tmdb_search(title: str, type_: str, year=None):
|
|
| 49 |
return None
|
| 50 |
|
| 51 |
|
| 52 |
-
# ----------------- Main -----------------
|
| 53 |
async def metadata(filename: str, channel: int, msg_id) -> dict | None:
|
| 54 |
try:
|
| 55 |
parsed = PTN.parse(filename)
|
|
@@ -61,37 +66,38 @@ async def metadata(filename: str, channel: int, msg_id) -> dict | None:
|
|
| 61 |
if "excess" in parsed and any("combined" in item.lower() for item in parsed["excess"]):
|
| 62 |
LOGGER.info(f"Skipping {filename}: contains 'combined'")
|
| 63 |
return None
|
| 64 |
-
|
| 65 |
-
# Skip multipart
|
| 66 |
multipart_pattern = compile(r'(?:part|cd|disc|disk)[s._-]*\d+(?=\.\w+$)', IGNORECASE)
|
| 67 |
if multipart_pattern.search(filename):
|
| 68 |
LOGGER.info(f"Skipping {filename}: seems to be a split/multipart file")
|
| 69 |
return None
|
| 70 |
|
| 71 |
-
title
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
parsed.get("resolution"),
|
| 77 |
-
)
|
| 78 |
|
| 79 |
if not quality:
|
| 80 |
LOGGER.warning(f"Skipping {filename}: No resolution (parsed={parsed})")
|
| 81 |
return None
|
|
|
|
| 82 |
if isinstance(season, list) or isinstance(episode, list):
|
| 83 |
LOGGER.warning(f"Invalid season/episode format for {filename}: {parsed}")
|
| 84 |
return None
|
|
|
|
| 85 |
if season and not episode:
|
| 86 |
LOGGER.warning(f"Missing episode in {filename}: {parsed}")
|
| 87 |
return None
|
| 88 |
|
| 89 |
-
# Extract TMDb
|
| 90 |
default_id = None
|
| 91 |
try:
|
| 92 |
default_id = extract_tmdb_id(Backend.USE_DEFAULT_ID)
|
| 93 |
except Exception:
|
| 94 |
pass
|
|
|
|
| 95 |
if not default_id:
|
| 96 |
try:
|
| 97 |
default_id = extract_tmdb_id(filename)
|
|
@@ -104,7 +110,6 @@ async def metadata(filename: str, channel: int, msg_id) -> dict | None:
|
|
| 104 |
|
| 105 |
data = {"chat_id": channel, "msg_id": msg_id}
|
| 106 |
encoded_string = await encode_string(data)
|
| 107 |
-
|
| 108 |
|
| 109 |
try:
|
| 110 |
if season and episode:
|
|
@@ -118,11 +123,12 @@ async def metadata(filename: str, channel: int, msg_id) -> dict | None:
|
|
| 118 |
return None
|
| 119 |
|
| 120 |
|
| 121 |
-
# ----------------- TV -----------------
|
| 122 |
async def fetch_tv_metadata(title, season, episode, encoded_string, year=None, quality=None, default_id=None) -> dict | None:
|
| 123 |
imdb_id = default_id if default_id and default_id.startswith("tt") else await safe_imdb_search(title, "tvSeries")
|
| 124 |
tv_details, ep_details, use_tmdb = None, None, False
|
| 125 |
|
|
|
|
| 126 |
if imdb_id:
|
| 127 |
try:
|
| 128 |
await asyncio.sleep(DELAY)
|
|
@@ -132,22 +138,36 @@ async def fetch_tv_metadata(title, season, episode, encoded_string, year=None, q
|
|
| 132 |
except Exception as e:
|
| 133 |
LOGGER.warning(f"IMDb TV fetch failed [{imdb_id}]: {e}")
|
| 134 |
|
|
|
|
| 135 |
if not tv_details and not ep_details:
|
| 136 |
use_tmdb = True
|
| 137 |
tmdb_result = await safe_tmdb_search(title, "tv")
|
| 138 |
if not tmdb_result:
|
|
|
|
| 139 |
return None
|
|
|
|
| 140 |
tv_id = tmdb_result.id
|
| 141 |
-
|
| 142 |
-
|
|
|
|
|
|
|
|
|
|
| 143 |
|
| 144 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 145 |
return {
|
| 146 |
"tmdb_id": tv_details.id,
|
| 147 |
"imdb_id": "",
|
| 148 |
"title": tv_details.name,
|
| 149 |
-
"year": tv_details.first_air_date
|
| 150 |
-
"rate": tv_details
|
| 151 |
"description": tv_details.overview or "",
|
| 152 |
"poster": format_tmdb_image(tv_details.poster_path),
|
| 153 |
"backdrop": format_tmdb_image(tv_details.backdrop_path, "original"),
|
|
@@ -156,12 +176,17 @@ async def fetch_tv_metadata(title, season, episode, encoded_string, year=None, q
|
|
| 156 |
"media_type": "tv",
|
| 157 |
"season_number": season,
|
| 158 |
"episode_number": episode,
|
| 159 |
-
"episode_title": getattr(ep_details, "name", f"S{season}E{episode}"),
|
| 160 |
-
"episode_backdrop": format_tmdb_image(getattr(ep_details, "still_path", None), "original"),
|
| 161 |
"quality": quality,
|
| 162 |
"encoded_string": encoded_string,
|
| 163 |
}
|
| 164 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 165 |
imdb_id = tv_details.get("id", "")
|
| 166 |
images = format_imdb_images(imdb_id)
|
| 167 |
return {
|
|
@@ -178,38 +203,46 @@ async def fetch_tv_metadata(title, season, episode, encoded_string, year=None, q
|
|
| 178 |
"media_type": "tv",
|
| 179 |
"season_number": season,
|
| 180 |
"episode_number": episode,
|
| 181 |
-
"episode_title": ep_details.get("title", f"S{season}E{episode}") if ep_details else f"S{season}E{episode}",
|
| 182 |
"episode_backdrop": ep_details.get("image", "") if ep_details else "https://i.ibb.co/mrfTVzyW/Screenshot-20251006-232731.png",
|
| 183 |
"quality": quality,
|
| 184 |
"encoded_string": encoded_string,
|
| 185 |
}
|
| 186 |
|
| 187 |
|
| 188 |
-
# ----------------- Movie -----------------
|
| 189 |
async def fetch_movie_metadata(title, encoded_string, year=None, quality=None, default_id=None) -> dict | None:
|
| 190 |
imdb_id = default_id if default_id and default_id.startswith("tt") else await safe_imdb_search(f"{title} {year}" if year else title, "movie")
|
| 191 |
movie_details, use_tmdb = None, False
|
| 192 |
|
|
|
|
| 193 |
if imdb_id:
|
| 194 |
try:
|
| 195 |
movie_details = await get_detail(imdb_id=imdb_id)
|
| 196 |
except Exception as e:
|
| 197 |
LOGGER.warning(f"IMDb movie fetch failed [{title}]: {e}")
|
| 198 |
|
|
|
|
| 199 |
if not movie_details:
|
| 200 |
use_tmdb = True
|
| 201 |
tmdb_result = await safe_tmdb_search(title, "movie", year)
|
| 202 |
if not tmdb_result:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 203 |
return None
|
| 204 |
-
movie_details = await tmdb.movie(tmdb_result.id).details()
|
| 205 |
|
| 206 |
-
|
|
|
|
| 207 |
return {
|
| 208 |
"tmdb_id": movie_details.id,
|
| 209 |
"imdb_id": "",
|
| 210 |
"title": movie_details.title,
|
| 211 |
-
"year": movie_details.release_date
|
| 212 |
-
"rate": movie_details
|
| 213 |
"description": movie_details.overview or "",
|
| 214 |
"poster": format_tmdb_image(movie_details.poster_path),
|
| 215 |
"backdrop": format_tmdb_image(movie_details.backdrop_path, "original"),
|
|
@@ -220,6 +253,7 @@ async def fetch_movie_metadata(title, encoded_string, year=None, quality=None, d
|
|
| 220 |
"encoded_string": encoded_string,
|
| 221 |
}
|
| 222 |
|
|
|
|
| 223 |
imdb_id = movie_details.get("id", "")
|
| 224 |
images = format_imdb_images(imdb_id)
|
| 225 |
return {
|
|
|
|
| 11 |
from Backend.logger import LOGGER
|
| 12 |
from Backend.helper.encrypt import encode_string
|
| 13 |
|
| 14 |
+
# ----------------- Configuration -----------------
|
| 15 |
DELAY = 2
|
| 16 |
tmdb = aioTMDb(key=Telegram.TMDB_API, language="en-US", region="US")
|
| 17 |
|
|
|
|
| 18 |
# ----------------- Helpers -----------------
|
| 19 |
def format_tmdb_image(path: str, size="w500", fallback="https://i.ibb.co/mrfTVzyW/Screenshot-20251006-232731.png") -> str:
|
| 20 |
return f"https://image.tmdb.org/t/p/{size}{path}" if path else fallback
|
|
|
|
| 29 |
|
| 30 |
|
| 31 |
async def safe_imdb_search(title: str, type_: str) -> str | None:
|
| 32 |
+
"""Safely search IMDb title and return its ID."""
|
| 33 |
try:
|
| 34 |
result = await search_title(query=title, type=type_)
|
| 35 |
return result["id"] if result else None
|
|
|
|
| 39 |
|
| 40 |
|
| 41 |
async def safe_tmdb_search(title: str, type_: str, year=None):
|
| 42 |
+
"""Safely search TMDb title."""
|
| 43 |
try:
|
| 44 |
if type_ == "movie":
|
| 45 |
+
if year:
|
| 46 |
+
results = await tmdb.search().movies(query=title, year=year)
|
| 47 |
+
else:
|
| 48 |
+
results = await tmdb.search().movies(query=title)
|
| 49 |
else:
|
| 50 |
results = await tmdb.search().tv(query=title)
|
| 51 |
return results[0] if results else None
|
|
|
|
| 54 |
return None
|
| 55 |
|
| 56 |
|
| 57 |
+
# ----------------- Main Entry -----------------
|
| 58 |
async def metadata(filename: str, channel: int, msg_id) -> dict | None:
|
| 59 |
try:
|
| 60 |
parsed = PTN.parse(filename)
|
|
|
|
| 66 |
if "excess" in parsed and any("combined" in item.lower() for item in parsed["excess"]):
|
| 67 |
LOGGER.info(f"Skipping {filename}: contains 'combined'")
|
| 68 |
return None
|
| 69 |
+
|
| 70 |
+
# Skip split/multipart files
|
| 71 |
multipart_pattern = compile(r'(?:part|cd|disc|disk)[s._-]*\d+(?=\.\w+$)', IGNORECASE)
|
| 72 |
if multipart_pattern.search(filename):
|
| 73 |
LOGGER.info(f"Skipping {filename}: seems to be a split/multipart file")
|
| 74 |
return None
|
| 75 |
|
| 76 |
+
title = parsed.get("title")
|
| 77 |
+
season = parsed.get("season")
|
| 78 |
+
episode = parsed.get("episode")
|
| 79 |
+
year = parsed.get("year")
|
| 80 |
+
quality = parsed.get("resolution")
|
|
|
|
|
|
|
| 81 |
|
| 82 |
if not quality:
|
| 83 |
LOGGER.warning(f"Skipping {filename}: No resolution (parsed={parsed})")
|
| 84 |
return None
|
| 85 |
+
|
| 86 |
if isinstance(season, list) or isinstance(episode, list):
|
| 87 |
LOGGER.warning(f"Invalid season/episode format for {filename}: {parsed}")
|
| 88 |
return None
|
| 89 |
+
|
| 90 |
if season and not episode:
|
| 91 |
LOGGER.warning(f"Missing episode in {filename}: {parsed}")
|
| 92 |
return None
|
| 93 |
|
| 94 |
+
# Extract TMDb/IMDb hint
|
| 95 |
default_id = None
|
| 96 |
try:
|
| 97 |
default_id = extract_tmdb_id(Backend.USE_DEFAULT_ID)
|
| 98 |
except Exception:
|
| 99 |
pass
|
| 100 |
+
|
| 101 |
if not default_id:
|
| 102 |
try:
|
| 103 |
default_id = extract_tmdb_id(filename)
|
|
|
|
| 110 |
|
| 111 |
data = {"chat_id": channel, "msg_id": msg_id}
|
| 112 |
encoded_string = await encode_string(data)
|
|
|
|
| 113 |
|
| 114 |
try:
|
| 115 |
if season and episode:
|
|
|
|
| 123 |
return None
|
| 124 |
|
| 125 |
|
| 126 |
+
# ----------------- TV Metadata -----------------
|
| 127 |
async def fetch_tv_metadata(title, season, episode, encoded_string, year=None, quality=None, default_id=None) -> dict | None:
|
| 128 |
imdb_id = default_id if default_id and default_id.startswith("tt") else await safe_imdb_search(title, "tvSeries")
|
| 129 |
tv_details, ep_details, use_tmdb = None, None, False
|
| 130 |
|
| 131 |
+
# Try IMDb first
|
| 132 |
if imdb_id:
|
| 133 |
try:
|
| 134 |
await asyncio.sleep(DELAY)
|
|
|
|
| 138 |
except Exception as e:
|
| 139 |
LOGGER.warning(f"IMDb TV fetch failed [{imdb_id}]: {e}")
|
| 140 |
|
| 141 |
+
# IMDb failed → fallback to TMDb
|
| 142 |
if not tv_details and not ep_details:
|
| 143 |
use_tmdb = True
|
| 144 |
tmdb_result = await safe_tmdb_search(title, "tv")
|
| 145 |
if not tmdb_result:
|
| 146 |
+
LOGGER.warning(f"No TMDb result for '{title}'")
|
| 147 |
return None
|
| 148 |
+
|
| 149 |
tv_id = tmdb_result.id
|
| 150 |
+
try:
|
| 151 |
+
tv_details = await tmdb.tv(tv_id).details()
|
| 152 |
+
except Exception as e:
|
| 153 |
+
LOGGER.warning(f"TMDb TV details failed for {title}: {e}")
|
| 154 |
+
return None
|
| 155 |
|
| 156 |
+
# Fetch episode safely
|
| 157 |
+
try:
|
| 158 |
+
ep_details = await tmdb.episode(tv_id, season, episode).details()
|
| 159 |
+
except Exception as e:
|
| 160 |
+
LOGGER.warning(f"TMDb episode not found for {title} S{season}E{episode}: {e}")
|
| 161 |
+
ep_details = None
|
| 162 |
+
|
| 163 |
+
# Return TMDb-based data
|
| 164 |
+
if use_tmdb and tv_details:
|
| 165 |
return {
|
| 166 |
"tmdb_id": tv_details.id,
|
| 167 |
"imdb_id": "",
|
| 168 |
"title": tv_details.name,
|
| 169 |
+
"year": getattr(tv_details.first_air_date, "year", 0),
|
| 170 |
+
"rate": getattr(tv_details, "vote_average", 0) or 0,
|
| 171 |
"description": tv_details.overview or "",
|
| 172 |
"poster": format_tmdb_image(tv_details.poster_path),
|
| 173 |
"backdrop": format_tmdb_image(tv_details.backdrop_path, "original"),
|
|
|
|
| 176 |
"media_type": "tv",
|
| 177 |
"season_number": season,
|
| 178 |
"episode_number": episode,
|
| 179 |
+
"episode_title": getattr(ep_details, "name", f"S{season}E{episode}") if ep_details else f"{tv_details.name} S{season}E{episode}",
|
| 180 |
+
"episode_backdrop": format_tmdb_image(getattr(ep_details, "still_path", None), "original") if ep_details else "https://i.ibb.co/mrfTVzyW/Screenshot-20251006-232731.png",
|
| 181 |
"quality": quality,
|
| 182 |
"encoded_string": encoded_string,
|
| 183 |
}
|
| 184 |
|
| 185 |
+
# IMDb-based data
|
| 186 |
+
if not tv_details:
|
| 187 |
+
LOGGER.warning(f"No valid IMDb data for {title}")
|
| 188 |
+
return None
|
| 189 |
+
|
| 190 |
imdb_id = tv_details.get("id", "")
|
| 191 |
images = format_imdb_images(imdb_id)
|
| 192 |
return {
|
|
|
|
| 203 |
"media_type": "tv",
|
| 204 |
"season_number": season,
|
| 205 |
"episode_number": episode,
|
| 206 |
+
"episode_title": ep_details.get("title", f"S{season}E{episode}") if ep_details else f"{tv_details.get('title', title)} S{season}E{episode}",
|
| 207 |
"episode_backdrop": ep_details.get("image", "") if ep_details else "https://i.ibb.co/mrfTVzyW/Screenshot-20251006-232731.png",
|
| 208 |
"quality": quality,
|
| 209 |
"encoded_string": encoded_string,
|
| 210 |
}
|
| 211 |
|
| 212 |
|
| 213 |
+
# ----------------- Movie Metadata -----------------
|
| 214 |
async def fetch_movie_metadata(title, encoded_string, year=None, quality=None, default_id=None) -> dict | None:
|
| 215 |
imdb_id = default_id if default_id and default_id.startswith("tt") else await safe_imdb_search(f"{title} {year}" if year else title, "movie")
|
| 216 |
movie_details, use_tmdb = None, False
|
| 217 |
|
| 218 |
+
# Try IMDb first
|
| 219 |
if imdb_id:
|
| 220 |
try:
|
| 221 |
movie_details = await get_detail(imdb_id=imdb_id)
|
| 222 |
except Exception as e:
|
| 223 |
LOGGER.warning(f"IMDb movie fetch failed [{title}]: {e}")
|
| 224 |
|
| 225 |
+
# IMDb failed → fallback to TMDb
|
| 226 |
if not movie_details:
|
| 227 |
use_tmdb = True
|
| 228 |
tmdb_result = await safe_tmdb_search(title, "movie", year)
|
| 229 |
if not tmdb_result:
|
| 230 |
+
LOGGER.warning(f"No TMDb movie found for '{title}'")
|
| 231 |
+
return None
|
| 232 |
+
try:
|
| 233 |
+
movie_details = await tmdb.movie(tmdb_result.id).details()
|
| 234 |
+
except Exception as e:
|
| 235 |
+
LOGGER.warning(f"TMDb movie details failed for {title}: {e}")
|
| 236 |
return None
|
|
|
|
| 237 |
|
| 238 |
+
# TMDb result
|
| 239 |
+
if use_tmdb and movie_details:
|
| 240 |
return {
|
| 241 |
"tmdb_id": movie_details.id,
|
| 242 |
"imdb_id": "",
|
| 243 |
"title": movie_details.title,
|
| 244 |
+
"year": getattr(movie_details.release_date, "year", 0),
|
| 245 |
+
"rate": getattr(movie_details, "vote_average", 0) or 0,
|
| 246 |
"description": movie_details.overview or "",
|
| 247 |
"poster": format_tmdb_image(movie_details.poster_path),
|
| 248 |
"backdrop": format_tmdb_image(movie_details.backdrop_path, "original"),
|
|
|
|
| 253 |
"encoded_string": encoded_string,
|
| 254 |
}
|
| 255 |
|
| 256 |
+
# IMDb result
|
| 257 |
imdb_id = movie_details.get("id", "")
|
| 258 |
images = format_imdb_images(imdb_id)
|
| 259 |
return {
|
pyproject.toml
CHANGED
|
@@ -1,6 +1,6 @@
|
|
| 1 |
[project]
|
| 2 |
name = "Telegram-Stremio"
|
| 3 |
-
version = "1.1.
|
| 4 |
description = "A powerful, self-hosted Telegram Stremio Media Server built with FastAPI, MongoDB, and PyroFork seamlessly integrated with Stremio for automated media streaming and discovery."
|
| 5 |
readme = "README.md"
|
| 6 |
requires-python = ">=3.11"
|
|
|
|
| 1 |
[project]
|
| 2 |
name = "Telegram-Stremio"
|
| 3 |
+
version = "1.1.2"
|
| 4 |
description = "A powerful, self-hosted Telegram Stremio Media Server built with FastAPI, MongoDB, and PyroFork seamlessly integrated with Stremio for automated media streaming and discovery."
|
| 5 |
readme = "README.md"
|
| 6 |
requires-python = ">=3.11"
|
uv.lock
CHANGED
|
@@ -901,7 +901,7 @@ wheels = [
|
|
| 901 |
|
| 902 |
[[package]]
|
| 903 |
name = "telegram-stremio"
|
| 904 |
-
version = "1.1.
|
| 905 |
source = { virtual = "." }
|
| 906 |
dependencies = [
|
| 907 |
{ name = "aiofiles" },
|
|
|
|
| 901 |
|
| 902 |
[[package]]
|
| 903 |
name = "telegram-stremio"
|
| 904 |
+
version = "1.1.1"
|
| 905 |
source = { virtual = "." }
|
| 906 |
dependencies = [
|
| 907 |
{ name = "aiofiles" },
|