Skip to content

Commit

Permalink
Merge branch 'client-feedback'
Browse files Browse the repository at this point in the history
  • Loading branch information
Audionut committed Oct 31, 2024
2 parents e6b3223 + 3587abd commit 18b4571
Showing 1 changed file with 25 additions and 3 deletions.
28 changes: 25 additions & 3 deletions src/clients.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,12 @@ async def find_existing_torrent(self, meta):
torrenthash = None
if torrent_storage_dir is not None and os.path.exists(torrent_storage_dir):
if meta.get('torrenthash', None) is not None:
console.print("torrenthash:", torrenthash)
valid, torrent_path = await self.is_valid_torrent(meta, f"{torrent_storage_dir}/{meta['torrenthash']}.torrent", meta['torrenthash'], torrent_client, print_err=True)
if valid:
torrenthash = meta['torrenthash']
elif meta.get('ext_torrenthash', None) is not None:
console.print("ext_torrenthash:", meta.get('ext_torrenthash'))
valid, torrent_path = await self.is_valid_torrent(meta, f"{torrent_storage_dir}/{meta['ext_torrenthash']}.torrent", meta['ext_torrenthash'], torrent_client, print_err=True)
if valid:
torrenthash = meta['ext_torrenthash']
Expand All @@ -89,6 +91,7 @@ async def find_existing_torrent(self, meta):
if not torrenthash:
console.print("[bold yellow]No Valid .torrent found")
if not torrenthash:
console.print("No torrenthash in find_existing")
return None
torrent_path = f"{torrent_storage_dir}/{torrenthash}.torrent"
valid2, torrent_path = await self.is_valid_torrent(meta, torrent_path, torrenthash, torrent_client, print_err=False)
Expand All @@ -98,6 +101,7 @@ async def find_existing_torrent(self, meta):
return None

async def is_valid_torrent(self, meta, torrent_path, torrenthash, torrent_client, print_err=False):
console.print("We've moved into torrent validation")
valid = False
wrong_file = False

Expand Down Expand Up @@ -209,6 +213,8 @@ async def search_qbit_for_torrent(self, meta, client):
try:
qbt_client = qbittorrentapi.Client(host=client['qbit_url'], port=client['qbit_port'], username=client['qbit_user'], password=client['qbit_pass'], VERIFY_WEBUI_CERTIFICATE=client.get('VERIFY_WEBUI_CERTIFICATE', True))
qbt_client.auth_log_in()
if meta['debug']:
console.print("We logged into qbittorrent")
except qbittorrentapi.LoginFailed:
console.print("[bold red]INCORRECT QBIT LOGIN CREDENTIALS")
return None
Expand All @@ -230,22 +236,38 @@ async def search_qbit_for_torrent(self, meta, client):
for torrent in torrents:
try:
torrent_path = torrent.get('content_path', f"{torrent.save_path}{torrent.name}")
# console.print("Trying torrent_paths")
except AttributeError:
if meta['debug']:
console.print(torrent)
console.print_exception()
continue
if remote_path_map:
torrent_path = torrent_path.replace(remote_path, local_path)
# Replace remote path with local path only if not already mapped
if not torrent_path.startswith(local_path):
torrent_path = torrent_path.replace(remote_path, local_path)
if meta['debug']:
console.print("Replaced paths round 2:", torrent_path)

# Check if the local path was accidentally duplicated and correct it
if torrent_path.startswith(f"{local_path}/{local_path.split('/')[-1]}"):
torrent_path = torrent_path.replace(f"{local_path}/{local_path.split('/')[-1]}", local_path)
if meta['debug']:
console.print("Corrected duplicate in torrent path round 2:", torrent_path)

# Standardize path separators for the local OS
torrent_path = torrent_path.replace(os.sep, '/').replace('/', os.sep)
if meta['debug']:
console.print("Final torrent path after remote mapping round 2:", torrent_path)

if meta['is_disc'] in ("", None) and len(meta['filelist']) == 1:
if torrent_path == meta['filelist'][0] and len(torrent.files) == len(meta['filelist']):
if torrent_path.lower() == meta['filelist'][0].lower() and len(torrent.files) == len(meta['filelist']):
valid, torrent_path = await self.is_valid_torrent(meta, f"{torrent_storage_dir}/{torrent.hash}.torrent", torrent.hash, 'qbit', print_err=False)
if valid:
console.print(f"[green]Found a matching .torrent with hash: [bold yellow]{torrent.hash}")
return torrent.hash
elif meta['path'] == torrent_path:

elif os.path.normpath(meta['path']).lower() == os.path.normpath(torrent_path).lower():
valid, torrent_path = await self.is_valid_torrent(meta, f"{torrent_storage_dir}/{torrent.hash}.torrent", torrent.hash, 'qbit', print_err=False)
if valid:
console.print(f"[green]Found a matching .torrent with hash: [bold yellow]{torrent.hash}")
Expand Down

0 comments on commit 18b4571

Please sign in to comment.