From 29b5305ab49921d6ddacee69e97ad7cd9d9bce68 Mon Sep 17 00:00:00 2001 From: rembo10 Date: Wed, 30 Apr 2014 21:58:46 -0700 Subject: [PATCH] Slowly making progress --- headphones/searcher.py | 29 +++++++++++++++++++++++++++++ headphones/utorrent.py | 23 +++++++++++++++-------- 2 files changed, 44 insertions(+), 8 deletions(-) diff --git a/headphones/searcher.py b/headphones/searcher.py index f4b8ada7..32aa5974 100644 --- a/headphones/searcher.py +++ b/headphones/searcher.py @@ -696,6 +696,35 @@ def send_to_downloader(data, bestqual, album): except Exception, e: logger.exception("Unhandled exception") + else: + logger.info("Sending torrent to uTorrent") + + # rutracker needs cookies to be set, pass the .torrent file instead of url + if bestqual[3] == 'rutracker.org': + file_or_url = rutracker.get_torrent(bestqual[2]) + else: + file_or_url = bestqual[2] + + torrentid = utorrent.addTorrent(file_or_url) + + if not torrentid: + logger.error("Error sending torrent to uTorrent. Are you sure it's running?") + return + + folder_name = utorrent.getTorrentFolder(torrentid) + if folder_name: + logger.info('Torrent folder name: %s' % folder_name) + else: + logger.error('Torrent folder name could not be determined') + return + + # remove temp .torrent file created above + if bestqual[3] == 'rutracker.org': + try: + shutil.rmtree(os.path.split(file_or_url)[0]) + except Exception, e: + logger.exception("Unhandled exception") + myDB = db.DBConnection() myDB.action('UPDATE albums SET status = "Snatched" WHERE AlbumID=?', [album['AlbumID']]) myDB.action('INSERT INTO snatched VALUES( ?, ?, ?, ?, DATETIME("NOW", "localtime"), ?, ?, ?)', [album['AlbumID'], bestqual[0], bestqual[1], bestqual[2], "Snatched", folder_name, kind]) diff --git a/headphones/utorrent.py b/headphones/utorrent.py index 5b870ea2..a1509a04 100644 --- a/headphones/utorrent.py +++ b/headphones/utorrent.py @@ -29,14 +29,23 @@ from headphones import logger, notifiers, request # Store torrent id so we can check up on it def addTorrent(link): - method = 'torrent-add' - arguments = {'filename': link, 'download-dir': headphones.DOWNLOAD_TORRENT_DIR} - response = torrentAction(method,arguments) + if link.startswith("magnet") or link.startswith("http") or link.endswith(".torrent"): + method = None + params = {'action':'add-url', 's':link} + files = None + else: + method = "post" + params = {'action':'add-file'} + files = {'torrent_file':{'music.torrent', link}} + + response = torrentAction(method,params,files) if not response: return False + print response + if response['result'] == 'success': if 'torrent-added' in response['arguments']: name = response['arguments']['torrent-added']['name'] @@ -75,7 +84,7 @@ def getTorrentFolder(torrentid): return torrent_folder_name -def torrentAction(method, arguments): +def torrentAction(method=None, params=None, files=None): host = headphones.UTORRENT_HOST username = headphones.UTORRENT_USERNAME @@ -98,12 +107,10 @@ def torrentAction(method, arguments): token_request = request.request_response(host + 'token.html') token = re.findall('(.*?)