From fd55e84274f91f21a8d712c9741df7dbed0064a3 Mon Sep 17 00:00:00 2001 From: Noam Date: Fri, 12 Feb 2016 13:16:22 +0200 Subject: [PATCH] Try to Download and Parse Torrents if Receives URL Deluge module will attempt to download any http(s) links it receives --- headphones/deluge.py | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/headphones/deluge.py b/headphones/deluge.py index d960d73f..4be0fa91 100644 --- a/headphones/deluge.py +++ b/headphones/deluge.py @@ -75,6 +75,37 @@ def addTorrent(link, data=None): 'content': metainfo} retid = _add_torrent_file(result) + elif link.startswith('http://') or link.startswith('https://'): + user_agent = 'Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2243.2 Safari/537.36' + headers = { 'User-Agent': user_agent } + torrentfile = '' + r = requests.get(link, headers=headers) + if r.status_code == 200: + for chunk in r.iter_content(chunk_size=1024): + if chunk: # filter out keep-alive new chunks + torrent_file = torrentfile + chunk + else: + logger.debug('Deluge: Trying to GET ' + link + ' returned status ' + r.status_code) + return False + metainfo = str(base64.b64encode(torrentfile.decode('utf-8'))) + if 'announce' not in metainfo[:40]: + logger.debug('Deluge: Contents of ' + link + ' doesn\'t look like a torrent file') + return False + # Extract torrent name from .torrent + try: + name_length = int(re.findall('name([0-9]*)\:.*?\:', base64.b64encode(metainfo))[0]) + name = re.findall('name[0-9]*\:(.*?)\:', base64.b64encode(metainfo))[0][:name_length] + except: + # 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')] + result = {'type': 'torrent', + 'name': name, + 'content': metainfo} + retid = _add_torrent_file(result) + elif link.startswith('magnet:'): result = {'type': 'magnet', 'url': link}