Skip to content

Commit

Permalink
update btn/bhd id fetching
Browse files Browse the repository at this point in the history
  • Loading branch information
Audionut committed Mar 5, 2025
1 parent b72d0e9 commit 9ce8e73
Showing 1 changed file with 55 additions and 34 deletions.
89 changes: 55 additions & 34 deletions src/btnid.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,14 @@ async def get_btn_torrents(btn_api, btn_id, meta):
if first_torrent:
if "ImdbID" in first_torrent:
meta["imdb_id"] = first_torrent["ImdbID"]
print("BTN IMDb ID:", meta.get("imdb_id"))
return int(first_torrent["ImdbID"])
if "TvdbID" in first_torrent:
meta["tvdb_id"] = first_torrent["TvdbID"]
print("BTN TVDb ID:", meta.get("tvdb_id"))
return int(first_torrent["TvdbID"])

print("BTN IMDb ID:", meta.get("imdb_id"))
print("BTN TVDb ID:", meta.get("tvdb_id"))
print("No IMDb or TVDb ID found.")
return meta


Expand All @@ -52,41 +55,59 @@ async def get_bhd_torrents(bhd_api, bhd_rss_key, info_hash, meta, only_id=False)

headers = {"Content-Type": "application/json"}

async with httpx.AsyncClient() as client:
response = await client.post(post_query_url, headers=headers, json=post_data)
data = response.json()

if "results" in data and data["results"]:
first_result = data["results"][0]
name = first_result.get("name", "").lower()
internal = bool(first_result.get("internal", False))
description = first_result.get("description", "")
imdb_id = first_result.get("imdb_id", "").replace("tt", "") if first_result.get("imdb_id") else None
tmdb_id = first_result.get("tmdb_id", "") if first_result.get("tmdb_id") else None
meta["imdb_id"] = imdb_id
meta['category'], meta['tmdb_id'] = await parse_tmdb_id(tmdb_id, meta.get('category'))
if not only_id and internal and ("framestor" in name or "flux" in name):
bbcode = BBCODE()
imagelist = []
if "framestor" in name:
meta['framestor'] = True
description, imagelist = bbcode.clean_bhd_description(description, meta)
meta['description'] = description
meta['image_list'] = imagelist
try:
async with httpx.AsyncClient() as client:
response = await client.post(post_query_url, headers=headers, json=post_data, timeout=10)
response.raise_for_status()
data = response.json()
except (httpx.RequestError, httpx.HTTPStatusError) as e:
print(f"[ERROR] Failed to fetch BHD data: {e}")
return meta

results = data.get("results", [])
if not results:
print("[WARNING] No results found in BHD API response.")
return meta

first_result = results[0]
name = first_result.get("name", "").lower()
internal = bool(first_result.get("internal", False))
description = first_result.get("description", "")

imdb_id = first_result.get("imdb_id", "").replace("tt", "") if first_result.get("imdb_id") else 0
meta["imdb_id"] = int(imdb_id or 0)

raw_tmdb_id = first_result.get("tmdb_id", "")
meta["category"], parsed_tmdb_id = await parse_tmdb_id(raw_tmdb_id, meta.get("category"))
meta["tmdb_manual"] = int(parsed_tmdb_id or 0)

if only_id:
return meta["imdb_id"] or meta["tmdb_manual"] or 0

if not only_id and internal and ("framestor" in name or "flux" in name):
bbcode = BBCODE()
imagelist = []
if "framestor" in name:
meta["framestor"] = True
description, imagelist = bbcode.clean_bhd_description(description, meta)
meta["description"] = description
meta["image_list"] = imagelist

print("BHD IMDb ID:", meta.get("imdb_id"))
print("BHD TMDb ID:", meta.get("tmdb_id"))
return meta
print("BHD TMDb ID:", meta.get("tmdb_manual"))

return meta["imdb_id"] or meta["tmdb_manual"] or 0


async def parse_tmdb_id(id, category):
id = id.lower().lstrip()
if id.startswith('tv'):
id = id.split('/')[1]
async def parse_tmdb_id(tmdb_id, category):
"""Parses TMDb ID, ensures correct formatting, and assigns category."""
tmdb_id = str(tmdb_id).strip().lower()

if tmdb_id.startswith('tv/') and '/' in tmdb_id:
tmdb_id = tmdb_id.split('/')[1]
category = 'TV'
elif id.startswith('movie'):
id = id.split('/')[1]
elif tmdb_id.startswith('movie/') and '/' in tmdb_id:
tmdb_id = tmdb_id.split('/')[1]
category = 'MOVIE'
else:
id = id
return category, id

return category, tmdb_id

0 comments on commit 9ce8e73

Please sign in to comment.