From 5c381b941061d36d3e4ccae40d76b3edfccc3e85 Mon Sep 17 00:00:00 2001 From: delphiactual Date: Thu, 1 May 2014 21:58:05 -0600 Subject: [PATCH] Added Labeling based off of hash for utorrent and fixed a typo in postprocessor.py --- headphones/postprocessor.py | 2 +- headphones/utorrent.py | 50 ++++++++++++++++--------------------- 2 files changed, 23 insertions(+), 29 deletions(-) diff --git a/headphones/postprocessor.py b/headphones/postprocessor.py index 8bec9830..db5019e1 100644 --- a/headphones/postprocessor.py +++ b/headphones/postprocessor.py @@ -352,7 +352,7 @@ def doPostProcessing(albumid, albumpath, release, tracks, downloaded_track_list, headphones.MOVE_FILES: if not os.access(downloaded_track, os.W_OK): - logger.error("Track file is not writeable, which is equired for some post processing steps: %s", downloaded_track.decode(headphones.SYS_ENCODING, 'replace')) + logger.error("Track file is not writeable, which is required for some post processing steps: %s", downloaded_track.decode(headphones.SYS_ENCODING, 'replace')) return #start encoding diff --git a/headphones/utorrent.py b/headphones/utorrent.py index 7be48355..7aad16f5 100644 --- a/headphones/utorrent.py +++ b/headphones/utorrent.py @@ -69,36 +69,30 @@ def addTorrent(link): logger.error("Error sending torrent to uTorrent") return - # Not really sure how to ID these? Title seems safest) - # Also, not sure when the torrent will pop up in the list, so we'll make sure it exists and is 1% downloaded - tries = 0 - folder = None - while tries < 10: + if link.startswith('magnet'): + tor_hash = re.findall('urn:btih:([\w]{32,40})', link)[0] + if len(tor_hash) == 32: + tor_hash = b16encode(b32decode(tor_hash)).lower() + else: + info = bdecode(link.content)["info"] + tor_hash = sha1(bencode(info)).hexdigest() + + params = {'action':'setprops', 'hash':tor_hash,'s':'label', 'v':label, 'token':token} + response = request.request_json(host, params=params, auth=auth, cookies=cookies) + if not response: + logger.error("Error setting torrent label in uTorrent") + return - # NOW WE WILL CHECK UTORRENT FOR THE FOLDER NAME & SET THE LABEL - params = {'list':'1', 'token':token} + # folder info can probably be cleaned up with getprops + folder = None - response = request.request_json(host, params=params, auth=auth, cookies=cookies) - - if not response: - logger.error("Error getting torrent information from uTorrent") - return + params = {'list':'1', 'token':token} + response = request.request_json(host, params=params, auth=auth, cookies=cookies) + if not response: + logger.error("Error getting torrent information from uTorrent") + return for torrent in response['torrents']: + folder = os.path.basename(torrent[26]) - if torrent[19] == link and torrent[4] > 1: - folder = os.path.basename(torrent[26]) - tor_hash = torrent[0] - params = {'action':'setprops', 'hash':tor_hash,'s':'label', 'v':label, 'token':token} - response = request.request_json(host, params=params, auth=auth, cookies=cookies) - break - - if folder: - break - else: - time.sleep(5) - tries += 1 - - return folder - - + return folder \ No newline at end of file