From 7240db25678e320acc8ba287943bf18ae5669ddd Mon Sep 17 00:00:00 2001 From: rembo10 Date: Tue, 8 Apr 2014 15:14:38 -0700 Subject: [PATCH] Took out automatic torrage links, now grab either the magnet or torrent file, depending on settings, also, allow headphones to open magnet links if the blackhole option is set --- data/interfaces/default/config.html | 5 ++ headphones/__init__.py | 5 +- headphones/searcher.py | 87 +++++++++++++++++++---------- headphones/webserve.py | 4 +- 4 files changed, 70 insertions(+), 31 deletions(-) diff --git a/data/interfaces/default/config.html b/data/interfaces/default/config.html index 88b0286f..820e729d 100644 --- a/data/interfaces/default/config.html +++ b/data/interfaces/default/config.html @@ -200,6 +200,11 @@ Folder your Download program watches for Torrents +
+ + + Allow Headphones to open magnet links +
diff --git a/headphones/__init__.py b/headphones/__init__.py index f03ea67b..ed0ed0d3 100644 --- a/headphones/__init__.py +++ b/headphones/__init__.py @@ -128,6 +128,7 @@ AUTOWANT_UPCOMING = False AUTOWANT_ALL = False KEEP_TORRENT_FILES = False PREFER_TORRENTS = None # 0: nzbs, 1: torrents, 2: no preference +OPEN_MAGNET_LINKS = False SEARCH_INTERVAL = 360 LIBRARYSCAN = False @@ -336,7 +337,7 @@ def initialize(): HTTP_PORT, HTTP_HOST, HTTP_USERNAME, HTTP_PASSWORD, HTTP_ROOT, HTTP_PROXY, LAUNCH_BROWSER, API_ENABLED, API_KEY, GIT_PATH, GIT_USER, GIT_BRANCH, DO_NOT_OVERRIDE_GIT_BRANCH, \ CURRENT_VERSION, LATEST_VERSION, CHECK_GITHUB, CHECK_GITHUB_ON_STARTUP, CHECK_GITHUB_INTERVAL, MUSIC_DIR, DESTINATION_DIR, \ LOSSLESS_DESTINATION_DIR, PREFERRED_QUALITY, PREFERRED_BITRATE, DETECT_BITRATE, ADD_ARTISTS, CORRECT_METADATA, MOVE_FILES, \ - RENAME_FILES, FOLDER_FORMAT, FILE_FORMAT, FILE_UNDERSCORES, CLEANUP_FILES, INCLUDE_EXTRAS, EXTRAS, AUTOWANT_UPCOMING, AUTOWANT_ALL, KEEP_TORRENT_FILES, PREFER_TORRENTS, \ + RENAME_FILES, FOLDER_FORMAT, FILE_FORMAT, FILE_UNDERSCORES, CLEANUP_FILES, INCLUDE_EXTRAS, EXTRAS, AUTOWANT_UPCOMING, AUTOWANT_ALL, KEEP_TORRENT_FILES, PREFER_TORRENTS, OPEN_MAGNET_LINKS, \ ADD_ALBUM_ART, ALBUM_ART_FORMAT, EMBED_ALBUM_ART, EMBED_LYRICS, REPLACE_EXISTING_FOLDERS, DOWNLOAD_DIR, BLACKHOLE, BLACKHOLE_DIR, USENET_RETENTION, SEARCH_INTERVAL, \ TORRENTBLACKHOLE_DIR, NUMBEROFSEEDERS, ISOHUNT, KAT, PIRATEBAY, PIRATEBAY_PROXY_URL, MININOVA, WAFFLES, WAFFLES_UID, WAFFLES_PASSKEY, \ RUTRACKER, RUTRACKER_USER, RUTRACKER_PASSWORD, WHATCD, WHATCD_USERNAME, WHATCD_PASSWORD, DOWNLOAD_TORRENT_DIR, \ @@ -455,6 +456,7 @@ def initialize(): AUTOWANT_ALL = bool(check_setting_int(CFG, 'General', 'autowant_all', 0)) KEEP_TORRENT_FILES = bool(check_setting_int(CFG, 'General', 'keep_torrent_files', 0)) PREFER_TORRENTS = check_setting_int(CFG, 'General', 'prefer_torrents', 0) + OPEN_MAGNET_LINKS = bool(check_setting_int(CFG, 'General', 'open_magnet_links', 0)) SEARCH_INTERVAL = check_setting_int(CFG, 'General', 'search_interval', 1440) LIBRARYSCAN = bool(check_setting_int(CFG, 'General', 'libraryscan', 1)) @@ -868,6 +870,7 @@ def config_write(): new_config['General']['autowant_all'] = int(AUTOWANT_ALL) new_config['General']['keep_torrent_files'] = int(KEEP_TORRENT_FILES) new_config['General']['prefer_torrents'] = PREFER_TORRENTS + new_config['General']['open_magnet_links'] = OPEN_MAGNET_LINKS new_config['General']['numberofseeders'] = NUMBEROFSEEDERS new_config['General']['torrentblackhole_dir'] = TORRENTBLACKHOLE_DIR diff --git a/headphones/searcher.py b/headphones/searcher.py index 07207d4e..b2dcc003 100644 --- a/headphones/searcher.py +++ b/headphones/searcher.py @@ -26,6 +26,7 @@ import os, re, time import string import shutil import requests +import subprocess import headphones from headphones.common import USER_AGENT @@ -606,36 +607,56 @@ def send_to_downloader(data, bestqual, album): # Blackhole if headphones.TORRENT_DOWNLOADER == 0: - if bestqual[2].startswith("magnet:"): - logger.error("Cannot save magnet files to blackhole. Please switch your torrent downloader to Transmission or uTorrent") - return - # Get torrent name from .torrent, this is usually used by the torrent client as the folder name torrent_name = helpers.replace_illegal_chars(folder_name) + '.torrent' download_path = os.path.join(headphones.TORRENTBLACKHOLE_DIR, torrent_name) + + if bestqual[2].startswith("magnet:"): + if headphones.OPEN_MAGNET_LINKS: + try: + if headphones.SYS_PLATFORM == 'win32': + os.startfile(besqual[2]) + elif headphones.SYS_PLATFORM == 'darwin': + subprocess.Popen(["open", bestqual[2]], stdout=subprocess.PIPE, stderr=subprocess.PIPE) + else: + subprocess.Popen(["xdg-open", bestqual[2]], stdout=subprocess.PIPE, stderr=subprocess.PIPE) - try: - if bestqual[3] == 'rutracker.org': - download_path = rutracker.get_torrent(bestqual[2], headphones.TORRENTBLACKHOLE_DIR) - if not download_path: + # Gonna just take a guess at this..... Is there a better way to find this out? + folder_name = bestqual[0] + except Exception, e: + logger.error("Error opening magnet link: %s" % str(e)) return else: - #Write the torrent file to a path derived from the TORRENTBLACKHOLE_DIR and file name. - prev = os.umask(headphones.UMASK) - with open(download_path, 'wb') as fp: - fp.write(data) - os.umask(prev) + logger.error("Cannot save magnet files to blackhole. Please switch your torrent downloader to Transmission or uTorrent or allow Headphones to try to open magnet links") + return - #Open the fresh torrent file again so we can extract the proper torrent name - #Used later in post-processing. - with open(download_path, 'rb') as fp: - torrent_info = bencode.bdecode(fp.read()) + else: + try: - torrent_folder_name = torrent_info['info'].get('name', '') - logger.info('Torrent folder name: %s' % torrent_folder_name) - except Exception, e: - logger.error('Couldn\'t get name from Torrent file: %s' % e) - return + if bestqual[3] == 'rutracker.org': + download_path = rutracker.get_torrent(bestqual[2], headphones.TORRENTBLACKHOLE_DIR) + if not download_path: + return + else: + #Write the torrent file to a path derived from the TORRENTBLACKHOLE_DIR and file name. + with open(download_path, 'wb') as fp: + fp.write(data) + + try: + os.chmod(download_path, int(headphones.FILE_PERMISSIONS, 8)) + except: + logger.error("Could not change permissions for file: %s", download_path) + + #Open the fresh torrent file again so we can extract the proper torrent name + #Used later in post-processing. + with open(download_path, 'rb') as fp: + torrent_info = bencode.bdecode(fp.read()) + + folder_name = torrent_info['info'].get('name', '') + logger.info('Torrent folder name: %s' % folder_name) + except Exception, e: + logger.error('Couldn\'t get name from Torrent file: %s. Defaulting to torrent title' % e) + folder_name = bestqual[0] elif headphones.TORRENT_DOWNLOADER == 1: logger.info("Sending torrent to Transmission") @@ -1142,13 +1163,17 @@ def searchTorrent(album, new=False, losslessOnly=False): rightformat = True title = ''.join(item.find("a", {"class" : "detLink"})) seeds = int(''.join(item.find("td", {"align" : "right"}))) - url = item.findAll("a")[3]['href'] + url = None if headphones.TORRENT_DOWNLOADER == 0: - tor_hash = re.findall("urn:btih:(.*?)&", url) - if len(tor_hash) > 0: - url = "http://torrage.com/torrent/"+str(tor_hash[0]).upper()+".torrent" - else: - url = None + try: + url = item.find("a", {"title":"Download this torrent"})['href'] + except TypeError: + if headphones.OPEN_MAGNET_LINKS: + url = item.findAll("a")[3]['href'] + else: + logger.info('"%s" only has a magnet link, skipping' % title) + else: + url = item.findAll("a")[3]['href'] formatted_size = re.search('Size (.*),', unicode(item)).group(1).replace(u'\xa0', ' ') size = helpers.piratesize(formatted_size) if size < maxsize and minimumseeders < seeds and url != None: @@ -1297,6 +1322,9 @@ def preprocess(resultlist): # get outta here if rutracker or piratebay if result[3] == 'rutracker.org': return True, result + # Get out of here if it's a magnet link + if result[2].startswith("magnet"): + return True, result # Download the torrent file headers = {} @@ -1305,7 +1333,8 @@ def preprocess(resultlist): headers['Referer'] = 'http://kat.ph/' elif result[3] == 'What.cd': headers['User-Agent'] = 'Headphones' - + elif result[3] == "The Pirate Bay": + headers['User-Agent'] = 'Mozilla/5.0 (Linux; Android 4.1.1; Nexus 7 Build/JRO03D) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.166 Safari/535.19' return request.request_content(url=result[2], headers=headers), result else: diff --git a/headphones/webserve.py b/headphones/webserve.py index 75707f49..ab923cd2 100644 --- a/headphones/webserve.py +++ b/headphones/webserve.py @@ -995,6 +995,7 @@ class WebInterface(object): "prefer_torrents_0" : radio(headphones.PREFER_TORRENTS, 0), "prefer_torrents_1" : radio(headphones.PREFER_TORRENTS, 1), "prefer_torrents_2" : radio(headphones.PREFER_TORRENTS, 2), + "open_magnet_links" : checked(headphones.OPEN_MAGNET_LINKS), "log_dir" : headphones.LOG_DIR, "cache_dir" : headphones.CACHE_DIR, "interface_list" : interface_list, @@ -1096,7 +1097,7 @@ class WebInterface(object): numberofseeders=None, use_piratebay=0, piratebay_proxy_url=None, use_isohunt=0, use_kat=0, use_mininova=0, waffles=0, waffles_uid=None, waffles_passkey=None, whatcd=0, whatcd_username=None, whatcd_password=None, rutracker=0, rutracker_user=None, rutracker_password=None, rename_files=0, correct_metadata=0, cleanup_files=0, add_album_art=0, album_art_format=None, embed_album_art=0, embed_lyrics=0, replace_existing_folders=False, destination_dir=None, lossless_destination_dir=None, folder_format=None, file_format=None, file_underscores=0, include_extras=0, single=0, ep=0, compilation=0, soundtrack=0, live=0, - remix=0, spokenword=0, audiobook=0, other=0, autowant_upcoming=False, autowant_all=False, keep_torrent_files=False, prefer_torrents=0, interface=None, log_dir=None, cache_dir=None, music_encoder=0, encoder=None, xldprofile=None, + remix=0, spokenword=0, audiobook=0, other=0, autowant_upcoming=False, autowant_all=False, keep_torrent_files=False, prefer_torrents=0, open_magnet_links=0, interface=None, log_dir=None, cache_dir=None, music_encoder=0, encoder=None, xldprofile=None, bitrate=None, samplingfrequency=None, encoderfolder=None, advancedencoder=None, encoderoutputformat=None, encodervbrcbr=None, encoderquality=None, encoderlossless=0, delete_lossless_files=0, growl_enabled=0, growl_onsnatch=0, growl_host=None, growl_password=None, prowl_enabled=0, prowl_onsnatch=0, prowl_keys=None, prowl_priority=0, xbmc_enabled=0, xbmc_host=None, xbmc_username=None, xbmc_password=None, xbmc_update=0, xbmc_notify=0, nma_enabled=False, nma_apikey=None, nma_priority=0, nma_onsnatch=0, pushalot_enabled=False, pushalot_apikey=None, pushalot_onsnatch=0, synoindex_enabled=False, lms_enabled=0, lms_host=None, @@ -1199,6 +1200,7 @@ class WebInterface(object): headphones.AUTOWANT_ALL = autowant_all headphones.KEEP_TORRENT_FILES = keep_torrent_files headphones.PREFER_TORRENTS = int(prefer_torrents) + headphones.OPEN_MAGNET_LINKS = open_magnet_links headphones.INTERFACE = interface headphones.LOG_DIR = log_dir headphones.CACHE_DIR = cache_dir