From e6510d51e1e8f07dd0dfd32b69ee91b3c79d7ec8 Mon Sep 17 00:00:00 2001 From: rembo10 Date: Sat, 27 Jul 2013 16:12:07 +0530 Subject: [PATCH] Grab the right folder name from transmission --- headphones/searcher.py | 5 +-- headphones/transmission.py | 73 ++++++++++++++++++++++++++------------ 2 files changed, 54 insertions(+), 24 deletions(-) diff --git a/headphones/searcher.py b/headphones/searcher.py index 07a2190f..f9f1c2cf 100644 --- a/headphones/searcher.py +++ b/headphones/searcher.py @@ -1386,8 +1386,9 @@ def searchTorrent(albumid=None, new=False, losslessOnly=False): elif headphones.TORRENT_DOWNLOADER == 1: logger.info("Sending torrent to Transmission") - transmission.sendTorrent(bestqual[2]) - + torrentid = transmission.addTorrent(bestqual[2]) + torrent_folder_name = transmission.getTorrentFolder(torrentid) + myDB.action('UPDATE albums SET status = "Snatched" WHERE AlbumID=?', [albums[2]]) myDB.action('INSERT INTO snatched VALUES( ?, ?, ?, ?, DATETIME("NOW", "localtime"), ?, ?, ?)', [albums[2], bestqual[0], bestqual[1], bestqual[2], "Snatched", torrent_folder_name, "torrent"]) diff --git a/headphones/transmission.py b/headphones/transmission.py index 21d7e584..bace5517 100644 --- a/headphones/transmission.py +++ b/headphones/transmission.py @@ -19,6 +19,7 @@ from headphones import logger, notifiers import urllib2 import lib.simplejson as json import base64 +import time # This is just a simple script to send torrents to transmission. The # intention is to turn this into a class where we can check the state @@ -26,7 +27,52 @@ import base64 # TODO: Store the session id so we don't need to make 2 calls # Store torrent id so we can check up on it -def sendTorrent(link): +def addTorrent(link): + method = 'torrent-add' + arguments = {'filename': link, 'download-dir':headphones.DOWNLOAD_TORRENT_DIR} + + response = torrentAction(method,arguments) + + + if response['result'] == 'success': + name = response['arguments']['torrent-added']['name'] + logger.info(u"Torrent sent to Transmission successfully") + if headphones.PROWL_ENABLED and headphones.PROWL_ONSNATCH: + logger.info(u"Sending Prowl notification") + prowl = notifiers.PROWL() + prowl.notify(name,"Download started") + if headphones.PUSHOVER_ENABLED and headphones.PUSHOVER_ONSNATCH: + logger.info(u"Sending Pushover notification") + prowl = notifiers.PUSHOVER() + prowl.notify(name,"Download started") + if headphones.NMA_ENABLED and headphones.NMA_ONSNATCH: + logger.debug(u"Sending NMA notification") + nma = notifiers.NMA() + nma.notify(snatched_nzb=name) + + return response['arguments']['torrent-added']['id'] + +def getTorrentFolder(torrentid): + method = 'torrent-get' + arguments = { 'ids': torrentid, 'fields': ['name','percentDone']} + + response = torrentAction(method, arguments) + percentdone = response['arguments']['torrents'][0]['percentDone'] + torrent_folder_name = response['arguments']['torrents'][0]['name'] + print torrent_folder_name + + while percentdone == 0: + print "In the while loop" + time.sleep(5) + response = torrentAction(method, arguments) + percentdone = response['arguments']['torrents'][0]['percentDone'] + print "Attempting to get folder name, percent done: " + str(percentdone) + + torrent_folder_name = response['arguments']['torrents'][0]['name'] + print torrent_folder_name + " updated and finished!" + return torrent_folder_name + +def torrentAction(method, arguments): host = headphones.TRANSMISSION_HOST username = headphones.TRANSMISSION_USERNAME @@ -61,9 +107,8 @@ def sendTorrent(link): request.add_header('x-transmission-session-id', sessionid) - postdata = json.dumps({ 'method': 'torrent-add', - 'arguments': { 'filename': link, - 'download-dir': headphones.DOWNLOAD_TORRENT_DIR } }) + postdata = json.dumps({ 'method': method, + 'arguments': arguments }) request.add_data(postdata) @@ -72,21 +117,5 @@ def sendTorrent(link): except Exception, e: logger.error("Error sending torrent to Transmission: " + str(e)) return - - if response['result'] == 'success': - name = response['arguments']['torrent-added']['name'] - logger.info(u"Torrent sent to Transmission successfully") - if headphones.PROWL_ENABLED and headphones.PROWL_ONSNATCH: - logger.info(u"Sending Prowl notification") - prowl = notifiers.PROWL() - prowl.notify(name,"Download started") - if headphones.PUSHOVER_ENABLED and headphones.PUSHOVER_ONSNATCH: - logger.info(u"Sending Pushover notification") - prowl = notifiers.PUSHOVER() - prowl.notify(name,"Download started") - if headphones.NMA_ENABLED and headphones.NMA_ONSNATCH: - logger.debug(u"Sending NMA notification") - nma = notifiers.NMA() - nma.notify(snatched_nzb=name) - - return True + + return response