From bd74d21c839b526377bb234ab3df2895d8812cc4 Mon Sep 17 00:00:00 2001 From: Noam Date: Fri, 19 Feb 2016 11:38:37 +0200 Subject: [PATCH] Moved Around Torrent-Type Checking - From local/data->URL>magnet to magnet->URL->local/data. I feel stupid. --- headphones/deluge.py | 66 +++++++++++++++++++++++--------------------- 1 file changed, 34 insertions(+), 32 deletions(-) diff --git a/headphones/deluge.py b/headphones/deluge.py index d51ba678..9f999924 100644 --- a/headphones/deluge.py +++ b/headphones/deluge.py @@ -53,33 +53,12 @@ def addTorrent(link, data=None): try: result = {} retid = False - if not (link.startswith('http://') or link.startswith('https://')): - if link.endswith('.torrent') or data: - if data: - logger.debug('Deluge: Getting .torrent data') - torrentfile = data - else: - logger.debug('Deluge: Getting .torrent file') - with open(link, 'rb') as f: - torrentfile = f.read() - # Extract torrent name from .torrent - try: - logger.debug('Deluge: Getting torrent name length') - name_length = int(re.findall('name([0-9]*)\:.*?\:', torrentfile)[0]) - logger.debug('Deluge: Getting torrent name') - name = re.findall('name[0-9]*\:(.*?)\:', torrentfile)[0][:name_length] - except Exception as e: - logger.debug('Deluge: Could not get torrent name, getting file name') - # get last part of link/path (name only) - name = link.split('\\')[-1].split('/')[-1] - # remove '.torrent' suffix - if name[-len('.torrent'):] == '.torrent': - name = name[:-len('.torrent')] - logger.debug('Deluge: Sending Deluge torrent with name %s and content [%s...]' % (name, torrentfile[:40])) - result = {'type': 'torrent', - 'name': name, - 'content': torrentfile} - retid = _add_torrent_file(result) + + if link.startswith('magnet:'): + logger.debug('Deluge: Got a magnet link: %s' % link) + result = {'type': 'magnet', + 'url': link} + retid = _add_torrent_magnet(result) elif link.startswith('http://') or link.startswith('https://'): logger.debug('Deluge: Got a URL: %s' % link) @@ -122,11 +101,34 @@ def addTorrent(link, data=None): 'content': torrentfile} retid = _add_torrent_file(result) - elif link.startswith('magnet:'): - logger.debug('Deluge: Got a magnet link: %s' % link) - result = {'type': 'magnet', - 'url': link} - retid = _add_torrent_magnet(result) + elif not (link.startswith('http://') or link.startswith('https://')): + #elif link.endswith('.torrent') or data: + if data: + logger.debug('Deluge: Getting .torrent data') + torrentfile = data + else: + logger.debug('Deluge: Getting .torrent file') + with open(link, 'rb') as f: + torrentfile = f.read() + # Extract torrent name from .torrent + try: + logger.debug('Deluge: Getting torrent name length') + name_length = int(re.findall('name([0-9]*)\:.*?\:', torrentfile)[0]) + logger.debug('Deluge: Getting torrent name') + name = re.findall('name[0-9]*\:(.*?)\:', torrentfile)[0][:name_length] + except Exception as e: + logger.debug('Deluge: Could not get torrent name, getting file name') + # get last part of link/path (name only) + name = link.split('\\')[-1].split('/')[-1] + # remove '.torrent' suffix + if name[-len('.torrent'):] == '.torrent': + name = name[:-len('.torrent')] + logger.debug('Deluge: Sending Deluge torrent with name %s and content [%s...]' % (name, torrentfile[:40])) + result = {'type': 'torrent', + 'name': name, + 'content': torrentfile} + retid = _add_torrent_file(result) + else: logger.error('Deluge: Unknown file type: %s' % link)