Repository: pymedusa/Medusa

Issue:
POSTPROCESSING failing "Could not get a valid path or file for processing" (Qbittorent + rartv/rarbg torrents)
**Describe the bug**
Postprocessing fails when using download handler. Scheduled post processing works normally. Logs show:
2022-02-05 17:04:40 DEBUG	POSTPROCESSQUEUE-POST-PROCESS :: [45b7314] Could not get a valid path or file for processing. path: [E:\New\TV], resource: [The.Daily.Show.2022.02.03.Johnny.Knoxville.1080p.WEB.h264-KOGi[rartv]]

**To Reproduce**
Snatch file using manual search.

**Expected behavior**
The file post processed (hardlinked to Plex library location)

**Screenshots**
If applicable, add screenshots to help explain your problem.
![image](https://user-images.githubusercontent.com/9789475/152652201-fa6944fb-8d5b-48d3-ba01-8808e2b640b0.png)
![image](https://user-images.githubusercontent.com/9789475/152653307-656918a9-ae64-4ea6-a128-e55ebb8a7ca2.png)
![image](https://user-images.githubusercontent.com/9789475/152653632-c40a6b25-2284-46ad-b80c-46dff23510e9.png)



**Medusa (please complete the following information):**
 - Medusa Info:	Branch: develop
 - Commit: 45b7314053c45890bfa5523efdf5c4f753752066
 - Version: 0.5.22
 - Database: 44.19
  - Python Version:	3.7.3 (v3.7.3:ef4ec6ed12, Mar 25 2019, 22:22:05) [MSC v.1916 64 bit (AMD64)]
  - SSL Version:	OpenSSL 1.1.0j 20 Nov 2018
 -  OS:	Windows-10-10.0.19041-SP0
  - Locale:	en_GB.cp1252
  - Timezone:	GMT Standard Time
 - Qbittorent 3.3.9

**Debug logs (at least 50 lines):**
General > Advanced Settings > Enable debug
<details>

```
https://www.toptal.com/developers/hastebin/exugabumot.pl
```
</details>

**Additional context**
Add any other context about the problem here.











Discussion:
Looks like this is specific to rarbg releases where the torrent name doesn't match the download directory qbittorent uses:

Without edits:
{"added_on":1644086665,"amount_left":5645554593,"auto_tmm":true,"availability":16.05500030517578,"category":"seriespp","completed":390266880,"completion_on":0,"content_path":"**E:\New\TV\Billions.S06E01.2160p.WEB.H265-GGEZ[rarbg]**","dl_limit":-1,"dlspeed":30777022,"downloaded":386107773,"downloaded_session":390376372,"eta":582,"f_l_piece_prio":false,"force_start":false,"hash":"7dc95d230f6671ce78e46b15439e2b0ac77c5c30","last_activity":1644086689,"magnet_uri":"magnet:?xt=urn:btih:7dc95d230f6671ce78e46b15439e2b0ac77c5c30&dn=Billions.S06E01.2160p.WEB.H265-GGEZ%5brarbg%5d&tr=snip","max_ratio":-1,"max_seeding_time":1,"name":"**Billions.S06E01.2160p.WEB.H265-GGEZ[rartv]**","num_complete":56,"num_incomplete":15,"num_leechs":0,"num_seeds":16,"priority":1,"progress":0.06465845316097871,"ratio":0,"ratio_limit":-2,"save_path":"E:\New\TV\","seeding_time":0,"seeding_time_limit":-2,"seen_complete":1644086689,"seq_dl":false,"size":6035821473,"state":"downloading","super_seeding":false,"tags":"","time_active":23,"total_size":6035821473,"tracker":"udp://explodie.org:6969/announce","trackers_count":25,"up_limit":-1,"uploaded":0,"uploaded_session":0,"upspeed":0},

Renaming the torrent to align to the download folder (rartv -> rarbg) fixes the issue:
[{"added_on":1644086665,"amount_left":0,"auto_tmm":true,"availability":-1,"category":"seriespp","completed":6035821473,"completion_on":1644086862,"content_path":**"E:\New\TV\Billions.S06E01.2160p.WEB.H265-GGEZ[rarbg]**","dl_limit":-1,"dlspeed":0,"downloaded":6044323624,"downloaded_session":6044323624,"eta":8640000,"f_l_piece_prio":false,"force_start":false,"hash":"7dc95d230f6671ce78e46b15439e2b0ac77c5c30","last_activity":1644086924,"magnet_uri":"magnet:?xt=urn:btih:7dc95d230f6671ce78e46b15439e2b0ac77c5c30&dn=Billions.S06E01.2160p.WEB.H265-GGEZ%5brarbg%5d&snip","max_ratio":-1,"max_seeding_time":1,"name":"**Billions.S06E01.2160p.WEB.H265-GGEZ[rarbg]**","num_complete":56,"num_incomplete":14,"num_leechs":0,"num_seeds":0,"priority":0,"progress":1,"ratio":0.00908607338328713,"ratio_limit":-2,"save_path":"E:\New\TV\","seeding_time":62,"seeding_time_limit":-2,"seen_complete":1644086893,"seq_dl":false,"size":6035821473,"state":"pausedUP","super_seeding":false,"tags":"","time_active":258,"total_size":6035821473,"tracker":"udp://explodie.org:6969/announce","trackers_count":25,"up_limit":-1,"uploaded":54919168,"uploaded_session":54968320,"upspeed":0}]

For what its worth this code always assumes resource_name + path = where the data is which is incorrect for these torrents:
https://github.com/pymedusa/Medusa/blob/729ab4f066034709137edb43440dba7c84959626/medusa/process_tv.py#L501
Not sure if this is qbittorent specific. Qbittorent does return the right content_path in its API, its just not being used.

Resolving patch:
diff --git a/medusa/clients/torrent/deluge.py b/medusa/clients/torrent/deluge.py
--- a/medusa/clients/torrent/deluge.py
+++ b/medusa/clients/torrent/deluge.py
@@ -661,7 +661,8 @@ def get_status(self, info_hash):
         client_status.progress = int(torrent['progress'])
 
         # Store destination
-        client_status.destination = torrent['download_location']
+        if torrent.get('download_location'):
+            client_status.destination = torrent['download_location']
 
         # Store resource
         client_status.resource = torrent['name']
diff --git a/medusa/clients/torrent/qbittorrent.py b/medusa/clients/torrent/qbittorrent.py
--- a/medusa/clients/torrent/qbittorrent.py
+++ b/medusa/clients/torrent/qbittorrent.py
@@ -6,6 +6,7 @@
 
 import logging
 import os
+from os.path import basename
 
 from medusa import app
 from medusa.clients.torrent.generic import GenericClient
@@ -416,10 +417,10 @@ def get_status(self, info_hash):
         client_status.destination = torrent['save_path']
 
         # Store resource
-        client_status.resource = torrent['name']
+        client_status.resource = basename(torrent['content_path'])
 
         log.info('Qbittorrent torrent: [{name}] using state: [{state}]', {
-            'name': torrent['name'], 'state': torrent['state']
+            'name': client_status.resource, 'state': torrent['state']
         })
 
         return client_status


Test patch: