From 0a7765f639b2ec129ba5e07a7fdbca632430e949 Mon Sep 17 00:00:00 2001
From: Ruud <ruud@crashdummy.nl>
Date: Sat, 23 Feb 2013 16:36:12 +0100
Subject: [PATCH] uTorrent status support. closes #1391

Thanks to Stourwalk
---
 couchpotato/core/downloaders/utorrent/main.py | 60 ++++++++++++++++++-
 couchpotato/core/plugins/renamer/main.py      |  2 +-
 2 files changed, 59 insertions(+), 3 deletions(-)

diff --git a/couchpotato/core/downloaders/utorrent/main.py b/couchpotato/core/downloaders/utorrent/main.py
index e1e67de1..389a0a6f 100644
--- a/couchpotato/core/downloaders/utorrent/main.py
+++ b/couchpotato/core/downloaders/utorrent/main.py
@@ -6,6 +6,7 @@ from hashlib import sha1
 from multipartpost import MultipartPostHandler
 import cookielib
 import httplib
+import json
 import re
 import time
 import urllib
@@ -64,6 +65,59 @@ class uTorrent(Downloader):
             log.error('Failed to send torrent to uTorrent: %s', err)
             return False
 
+    def getAllDownloadStatus(self):
+
+        log.debug('Checking uTorrent download status.')
+
+        # Load host from config and split out port.
+        host = self.conf('host').split(':')
+        if not isInt(host[1]):
+            log.error('Config properties are not filled in correctly, port is missing.')
+            return False
+
+        try:
+            self.utorrent_api = uTorrentAPI(host[0], port = host[1], username = self.conf('username'), password = self.conf('password'))
+        except Exception, err:
+            log.error('Failed to get uTorrent object: %s', err)
+            return False
+
+        data = ''
+        try:
+            data = self.utorrent_api.get_status()
+            queue = json.loads(data)
+            if queue.get('error'):
+                log.error('Error getting data from uTorrent: %s', queue.get('error'))
+                return False
+
+        except Exception, err:
+            log.error('Failed to get status from uTorrent: %s', err)
+            return False
+
+        if queue.get('torrents', []) == []:
+            log.debug('Nothing in queue')
+            return False
+
+        statuses = []
+
+        # Get torrents
+        for item in queue.get('torrents', []):
+
+            # item[21] = Paused | Downloading | Seeding | Finished
+            status = 'busy'
+            if item[21] == 'Finished' or item[21] == 'Seeding':
+                status = 'completed'
+
+            statuses.append({
+                'id': item[0],
+                'name': item[2],
+                'status':  status,
+                'original_status': item[1],
+                'timeleft': item[10],
+            })
+
+        return statuses
+
+
 
 class uTorrentAPI(object):
 
@@ -94,9 +148,7 @@ class uTorrentAPI(object):
         try:
             open_request = self.opener.open(request)
             response = open_request.read()
-            log.debug('response: %s', response)
             if response:
-                log.debug('uTorrent action successfull')
                 return response
             else:
                 log.debug('Unknown failure sending command to uTorrent. Return text is: %s', response)
@@ -133,3 +185,7 @@ class uTorrentAPI(object):
     def pause_torrent(self, hash):
         action = "action=pause&hash=%s" % hash
         return self._request(action)
+
+    def get_status(self):
+        action = "list=1"
+        return self._request(action)
diff --git a/couchpotato/core/plugins/renamer/main.py b/couchpotato/core/plugins/renamer/main.py
index be6e4eaa..054e1423 100644
--- a/couchpotato/core/plugins/renamer/main.py
+++ b/couchpotato/core/plugins/renamer/main.py
@@ -566,7 +566,7 @@ class Renamer(Plugin):
 
                         found = False
                         for item in statuses:
-                            if item['name'] == nzbname or getImdb(item['name']) == movie_dict['library']['identifier']:
+                            if item['name'] == nzbname or rel_dict['info']['name'] in item['name'] or getImdb(item['name']) == movie_dict['library']['identifier']:
 
                                 timeleft = 'N/A' if item['timeleft'] == -1 else item['timeleft']
                                 log.debug('Found %s: %s, time to go: %s', (item['name'], item['status'].upper(), timeleft))
-- 
GitLab