[O] Reuse existing

This commit is contained in:
2026-03-09 02:12:07 -04:00
parent 4c4615e784
commit 7494a71403
+83 -66
View File
@@ -80,82 +80,99 @@ def process_imdb_workflow(imdb_id: str, dl_dir: str = "/data/QB", jellyfin_base_
else: else:
jellyfin_dir = f"{jellyfin_base_dir}/Movie" jellyfin_dir = f"{jellyfin_base_dir}/Movie"
print(f"\n=== [1] Searching Torrents for {imdb_id} ===") print(f"\n=== [0.5] Checking if torrent already exists in qBittorrent ===")
imdb_url = f"https://www.imdb.com/title/{imdb_id}/"
search_res = search_mteam_torrents(imdb_url)
# Extract the torrent list
if "data" in search_res and isinstance(search_res["data"], dict) and "data" in search_res["data"]:
torrents = search_res["data"]["data"]
elif "data" in search_res and isinstance(search_res["data"], list):
torrents = search_res["data"]
elif isinstance(search_res, list):
torrents = search_res
else:
torrents = []
if not torrents:
print("No torrents found.")
return
# Format the torrents text
formatted_torrents = []
for t in torrents:
if isinstance(t, dict):
formatted_torrents.append(format_mteam_torrent(t))
torrents_text = "\n\n".join(formatted_torrents)
print(f"\n=== [2] Selecting best torrents using LLM ===")
selected_ids_str = select_best_torrents(torrents_text)
selected_ids = [tid.strip() for tid in selected_ids_str.split() if tid.strip()]
print(f"Selected torrent IDs: {selected_ids}")
if not selected_ids:
print("No torrents selected.")
return
qb = get_qb_client() qb = get_qb_client()
jellyfin_base = Path(jellyfin_dir) / f"{title_dir} [{imdb_id}]" jellyfin_base = Path(jellyfin_dir) / f"{title_dir} [{imdb_id}]"
new_name = f"{year} {title} [{imdb_id}]".strip()
for tid in selected_ids: existing_torrents = qb.torrents_info()
print(f"\n=== [3] Downloading .torrent for ID: {tid} ===") existing_t_hash = None
torrent_bytes = generate_mteam_download_token(tid) for t in existing_torrents:
if f"[{imdb_id}]" in t.name:
existing_t_hash = t.hash
break
print(f"\n=== [4] Adding torrent to qBittorrent ===") hashes_to_process = []
download_torrent(qb, torrent_bytes, dl_dir)
# Parse local hash directly instead of hoping qB orders correctly if existing_t_hash:
t_hash = get_torrent_hash(torrent_bytes) print(f"Found existing torrent with hash {existing_t_hash}, skipping search and download.")
if not t_hash: hashes_to_process.append((existing_t_hash, "existing"))
print(f"Could not compute hash for {tid}, skipping!") else:
continue print(f"\n=== [1] Searching Torrents for {imdb_id} ===")
imdb_url = f"https://www.imdb.com/title/{imdb_id}/"
search_res = search_mteam_torrents(imdb_url)
print(f"\n=== [5] Waiting for download to finish ===") # Extract the torrent list
# Wait slightly for qB to process the adding request if "data" in search_res and isinstance(search_res["data"], dict) and "data" in search_res["data"]:
time.sleep(3) torrents = search_res["data"]["data"]
elif "data" in search_res and isinstance(search_res["data"], list):
print(f"Tracking torrent Hash: {t_hash}") torrents = search_res["data"]
elif isinstance(search_res, list):
torrents = search_res
else:
torrents = []
new_name = f"{year} {title}".strip() if not torrents:
rename_torrent_and_folder(qb, t_hash, new_name) print("No torrents found.")
return
while True: # Format the torrents text
info = qb.torrents_info(hashes=t_hash) formatted_torrents = []
if not info: for t in torrents:
print("Torrent disappeared from qB!") if isinstance(t, dict):
break formatted_torrents.append(format_mteam_torrent(t))
torrents_text = "\n\n".join(formatted_torrents)
t_info = info[0]
progress = t_info.progress print(f"\n=== [2] Selecting best torrents using LLM ===")
state = t_info.state selected_ids_str = select_best_torrents(torrents_text)
print(f"Progress: {progress * 100:.1f}% (State: {state})") selected_ids = [tid.strip() for tid in selected_ids_str.split() if tid.strip()]
print(f"Selected torrent IDs: {selected_ids}")
if not selected_ids:
print("No torrents selected.")
return
for tid in selected_ids:
print(f"\n=== [3] Downloading .torrent for ID: {tid} ===")
torrent_bytes = generate_mteam_download_token(tid)
print(f"\n=== [4] Adding torrent to qBittorrent ===")
download_torrent(qb, torrent_bytes, dl_dir)
# Parse local hash directly instead of hoping qB orders correctly
t_hash = get_torrent_hash(torrent_bytes)
if not t_hash:
print(f"Could not compute hash for {tid}, skipping!")
continue
print(f"\n=== [5] Waiting for download to finish ===")
# Wait slightly for qB to process the adding request
time.sleep(3)
# Progress of 1.0 means 100%. Alternatively, check the state. print(f"Tracking torrent Hash: {t_hash}")
if progress >= 1.0 or state in ('uploading', 'pausedUP', 'stalledUP', 'forcedUP'):
break
time.sleep(5)
print("Download complete!")
rename_torrent_and_folder(qb, t_hash, new_name)
while True:
info = qb.torrents_info(hashes=t_hash)
if not info:
print("Torrent disappeared from qB!")
break
t_info = info[0]
progress = t_info.progress
state = t_info.state
print(f"Progress: {progress * 100:.1f}% (State: {state})")
# Progress of 1.0 means 100%. Alternatively, check the state.
if progress >= 1.0 or state in ('uploading', 'pausedUP', 'stalledUP', 'forcedUP'):
break
time.sleep(5)
print("Download complete!")
hashes_to_process.append((t_hash, tid))
for t_hash, tid in hashes_to_process:
print(f"\n=== [6] Generating rename mapping using LLM ===") print(f"\n=== [6] Generating rename mapping using LLM ===")
file_tree = get_torrent_file_tree(qb, t_hash) file_tree = get_torrent_file_tree(qb, t_hash)