Skip to content

Commit

Permalink
Improved PTP file searching
Browse files Browse the repository at this point in the history
  • Loading branch information
Audionut committed Sep 1, 2024
1 parent 67ef332 commit bbea2f3
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 28 deletions.
16 changes: 8 additions & 8 deletions src/prep.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,20 +162,20 @@ async def update_metadata_from_tracker(self, tracker_name, tracker_instance, met
console.print(f"[yellow]No valid data found on {tracker_name}[/yellow]")

elif tracker_name == "PTP":
# console.print(f"[blue]Handling PTP tracker[/blue]")

imdb_id = None # Ensure imdb_id is defined
# Check if the PTP ID is already in meta
if meta.get('ptp') is None:
# console.print(f"[yellow]No PTP ID in meta, searching by search term[/yellow]")
imdb, ptp_torrent_id, meta['ext_torrenthash'] = await tracker_instance.get_ptp_id_imdb(search_term, search_file_folder)
# No PTP ID in meta, search by search term
imdb_id, ptp_torrent_id, ptp_torrent_hash = await tracker_instance.get_ptp_id_imdb(search_term, search_file_folder, meta)
if ptp_torrent_id:
meta['ptp'] = ptp_torrent_id
meta['imdb'] = str(imdb).zfill(7) if imdb else None
meta['imdb'] = str(imdb_id).zfill(7) if imdb_id else None
else:
ptp_torrent_id = meta['ptp']
console.print(f"[cyan]PTP ID found in meta: {ptp_torrent_id}, using it to get IMDb ID[/cyan]")
imdb, _, meta['ext_torrenthash'] = await tracker_instance.get_imdb_from_torrent_id(ptp_torrent_id)
if imdb:
meta['imdb'] = str(imdb).zfill(7)
imdb_id, _, meta['ext_torrenthash'] = await tracker_instance.get_imdb_from_torrent_id(ptp_torrent_id)
if imdb_id:
meta['imdb'] = str(imdb_id).zfill(7)
console.print(f"[green]IMDb ID found: tt{meta['imdb']}[/green]")
else:
console.print(f"[yellow]Could not find IMDb ID using PTP ID: {ptp_torrent_id}[/yellow]")
Expand Down
2 changes: 1 addition & 1 deletion src/trackers/HDB.py
Original file line number Diff line number Diff line change
Expand Up @@ -534,7 +534,7 @@ async def search_filename(self, search_term, search_file_folder, meta):
"search": bd_summary # Using the Disc Title for search
}
console.print(f"[green]Searching HDB for disc title: [bold yellow]{bd_summary}[/bold yellow]")
console.print(f"[yellow]Using this data: {data}")
# console.print(f"[yellow]Using this data: {data}")
else:
console.print(f"[red]Error: 'Disc Title' not found in {bd_summary_path}[/red]")
return hdb_imdb, hdb_tvdb, hdb_name, hdb_torrenthash, hdb_id
Expand Down
44 changes: 25 additions & 19 deletions src/trackers/PTP.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ def __init__(self, config):
("Vietnamese", "vie", "vi"): 25,
}

async def get_ptp_id_imdb(self, search_term, search_file_folder):
async def get_ptp_id_imdb(self, search_term, search_file_folder, meta):
imdb_id = ptp_torrent_id = None
filename = str(os.path.basename(search_term))
params = {
Expand All @@ -96,41 +96,46 @@ async def get_ptp_id_imdb(self, search_term, search_file_folder):
response = requests.get(url, params=params, headers=headers)
await asyncio.sleep(1)
console.print(f"[green]Searching PTP for: [bold yellow]{filename}[/bold yellow]")

try:
if response.status_code == 200:
response = response.json()
# console.print(f"[blue]Raw API Response: {response}[/blue]")

if int(response['TotalResults']) >= 1:
for movie in response['Movies']:
if len(movie['Torrents']) >= 1:
for torrent in movie['Torrents']:
if search_file_folder == 'file':
for file in torrent['FileList']:
if file['Path'] == filename:
imdb_id = movie['ImdbId']
ptp_torrent_id = torrent['Id']
dummy, ptp_torrent_hash = await self.get_imdb_from_torrent_id(ptp_torrent_id)
console.print(f'[bold green]Matched release with PTP ID: [yellow]{ptp_torrent_id}[/yellow][/bold green]')
return imdb_id, ptp_torrent_id, ptp_torrent_hash
if search_file_folder == 'folder':
if str(torrent['FilePath']) == filename:
for file in torrent['FileList']:
if file['Path'] == filename:
imdb_id = movie['ImdbId']
ptp_torrent_id = torrent['Id']
dummy, ptp_torrent_hash = await self.get_imdb_from_torrent_id(ptp_torrent_id)
dummy, ptp_torrent_hash, *_ = await self.get_imdb_from_torrent_id(ptp_torrent_id)
console.print(f'[bold green]Matched release with PTP ID: [yellow]{ptp_torrent_id}[/yellow][/bold green]')

# Call get_torrent_info and print the results
tinfo = await self.get_torrent_info(imdb_id, meta)
console.print(f"[cyan]Torrent Info: {tinfo}[/cyan]")

return imdb_id, ptp_torrent_id, ptp_torrent_hash
else:
console.print(f'[yellow]Could not find any release matching [bold yellow]{filename}[/bold yellow] on PTP')
return None, None, None
elif int(response.status_code) in [400, 401, 403]:

console.print(f'[yellow]Could not find any release matching [bold yellow]{filename}[/bold yellow] on PTP')
return None, None, None

elif response.status_code in [400, 401, 403]:
console.print(f"[bold red]PTP: {response.text}")
return None, None, None
elif int(response.status_code) == 503:

elif response.status_code == 503:
console.print("[bold yellow]PTP Unavailable (503)")
return None, None, None

else:
return None, None, None
except Exception:
pass

except Exception as e:
console.print(f'[red]An error occurred: {str(e)}[/red]')

console.print(f'[yellow]Could not find any release matching [bold yellow]{filename}[/bold yellow] on PTP')
return None, None, None

Expand Down Expand Up @@ -250,6 +255,7 @@ async def get_torrent_info(self, imdb, meta):
tinfo = {}
try:
response = response.json()
# console.print(f"[blue]Raw info API Response: {response}[/blue]")
# title, plot, art, year, tags, Countries, Languages
for key, value in response[0].items():
if value not in (None, ""):
Expand Down

0 comments on commit bbea2f3

Please sign in to comment.