From 3bb173c979d41a5b5e1560325c9c7aed83d69dc0 Mon Sep 17 00:00:00 2001 From: Ade Date: Fri, 12 Sep 2014 11:53:58 +1200 Subject: [PATCH] Piratebay user agent + More torrent removal stuff - Piratebay user agent - Torrent removal search interval config + more logging --- headphones/__init__.py | 8 ++++++-- headphones/mb.py | 4 ++-- headphones/postprocessor.py | 1 + headphones/searcher.py | 4 ++-- headphones/torrentfinished.py | 4 +++- headphones/transmission.py | 27 +++++++++++++++++---------- 6 files changed, 31 insertions(+), 17 deletions(-) diff --git a/headphones/__init__.py b/headphones/__init__.py index a635375e..80635061 100644 --- a/headphones/__init__.py +++ b/headphones/__init__.py @@ -138,6 +138,7 @@ LIBRARYSCAN_INTERVAL = 300 DOWNLOAD_SCAN_INTERVAL = 5 UPDATE_DB_INTERVAL = 24 MB_IGNORE_AGE = 365 +TORRENT_REMOVAL_INTERVAL = 720 SAB_HOST = None SAB_USERNAME = None @@ -356,7 +357,7 @@ def initialize(): 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, KAT, KAT_PROXY_URL, KAT_RATIO, PIRATEBAY, PIRATEBAY_PROXY_URL, PIRATEBAY_RATIO, MININOVA, MININOVA_RATIO, WAFFLES, WAFFLES_UID, WAFFLES_PASSKEY, WAFFLES_RATIO, \ RUTRACKER, RUTRACKER_USER, RUTRACKER_PASSWORD, RUTRACKER_RATIO, WHATCD, WHATCD_USERNAME, WHATCD_PASSWORD, WHATCD_RATIO, DOWNLOAD_TORRENT_DIR, \ - LIBRARYSCAN, LIBRARYSCAN_INTERVAL, DOWNLOAD_SCAN_INTERVAL, UPDATE_DB_INTERVAL, MB_IGNORE_AGE, SAB_HOST, SAB_USERNAME, SAB_PASSWORD, SAB_APIKEY, SAB_CATEGORY, \ + LIBRARYSCAN, LIBRARYSCAN_INTERVAL, DOWNLOAD_SCAN_INTERVAL, UPDATE_DB_INTERVAL, MB_IGNORE_AGE, TORRENT_REMOVAL_INTERVAL, SAB_HOST, SAB_USERNAME, SAB_PASSWORD, SAB_APIKEY, SAB_CATEGORY, \ NZBGET_USERNAME, NZBGET_PASSWORD, NZBGET_CATEGORY, NZBGET_PRIORITY, NZBGET_HOST, HEADPHONES_INDEXER, NZBMATRIX, TRANSMISSION_HOST, TRANSMISSION_USERNAME, TRANSMISSION_PASSWORD, \ UTORRENT_HOST, UTORRENT_USERNAME, UTORRENT_PASSWORD, UTORRENT_LABEL, NEWZNAB, NEWZNAB_HOST, NEWZNAB_APIKEY, NEWZNAB_ENABLED, EXTRA_NEWZNABS, \ NZBSORG, NZBSORG_UID, NZBSORG_HASH, OMGWTFNZBS, OMGWTFNZBS_UID, OMGWTFNZBS_APIKEY, \ @@ -464,6 +465,7 @@ def initialize(): DOWNLOAD_SCAN_INTERVAL = check_setting_int(CFG, 'General', 'download_scan_interval', 5) UPDATE_DB_INTERVAL = check_setting_int(CFG, 'General', 'update_db_interval', 24) MB_IGNORE_AGE = check_setting_int(CFG, 'General', 'mb_ignore_age', 365) + TORRENT_REMOVAL_INTERVAL = check_setting_int(CFG, 'General', 'torrent_removal_interval', 720) TORRENTBLACKHOLE_DIR = check_setting_str(CFG, 'General', 'torrentblackhole_dir', '') NUMBEROFSEEDERS = check_setting_str(CFG, 'General', 'numberofseeders', '10') @@ -938,6 +940,7 @@ def config_write(): new_config['General']['download_scan_interval'] = DOWNLOAD_SCAN_INTERVAL new_config['General']['update_db_interval'] = UPDATE_DB_INTERVAL new_config['General']['mb_ignore_age'] = MB_IGNORE_AGE + new_config['General']['torrent_removal_interval'] = TORRENT_REMOVAL_INTERVAL new_config['SABnzbd'] = {} new_config['SABnzbd']['sab_host'] = SAB_HOST @@ -1138,7 +1141,8 @@ def start(): SCHED.add_interval_job(postprocessor.checkFolder, minutes=DOWNLOAD_SCAN_INTERVAL) # Remove Torrent + data if Post Processed and finished Seeding - SCHED.add_interval_job(torrentfinished.checkTorrentFinished, hours=12) + if TORRENT_REMOVAL_INTERVAL > 0: + SCHED.add_interval_job(torrentfinished.checkTorrentFinished, minutes=TORRENT_REMOVAL_INTERVAL) SCHED.start() diff --git a/headphones/mb.py b/headphones/mb.py index 1619d0b1..8cf1c349 100644 --- a/headphones/mb.py +++ b/headphones/mb.py @@ -87,7 +87,7 @@ def findArtist(name, limit=1): try: artistResults = musicbrainzngs.search_artists(limit=limit, **criteria)['artist-list'] - except WebServiceError, e: + except musicbrainzngs.WebServiceError as e: logger.warn('Attempt to query MusicBrainz for %s failed (%s)' % (name, str(e))) time.sleep(5) @@ -141,7 +141,7 @@ def findRelease(name, limit=1, artist=None): try: releaseResults = musicbrainzngs.search_releases(query=name,limit=limit,artist=artist)['release-list'] - except WebServiceError, e: #need to update exceptions + except musicbrainzngs.WebServiceError as e: #need to update exceptions logger.warn('Attempt to query MusicBrainz for "%s" failed: %s' % (name, str(e))) time.sleep(5) diff --git a/headphones/postprocessor.py b/headphones/postprocessor.py index 946d9514..7fce5d30 100644 --- a/headphones/postprocessor.py +++ b/headphones/postprocessor.py @@ -424,6 +424,7 @@ def doPostProcessing(albumid, albumpath, release, tracks, downloaded_track_list, if seed_snatched: hash = seed_snatched['FolderName'] torrent_removed = False + logger.info(u'%s - %s. Checking if torrent has finished seeding and can be removed' % (release['ArtistName'], release['AlbumTitle'])) if headphones.TORRENT_DOWNLOADER == 1: torrent_removed = transmission.removeTorrent(hash, True) else: diff --git a/headphones/searcher.py b/headphones/searcher.py index 3efc3cad..a40342a6 100644 --- a/headphones/searcher.py +++ b/headphones/searcher.py @@ -1202,7 +1202,7 @@ def searchTorrent(album, new=False, losslessOnly=False, albumlength=None): # Requesting content logger.info('Parsing results from 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' } + headers = {'User-Agent' : 'Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2049.0 Safari/537.36'} params = { "iht": "2", "sort": "seeds" @@ -1342,7 +1342,7 @@ def preprocess(resultlist): 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' + headers['User-Agent'] = 'Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2049.0 Safari/537.36' return request.request_content(url=result[2], headers=headers), result else: diff --git a/headphones/torrentfinished.py b/headphones/torrentfinished.py index 0c468631..2c9c555b 100644 --- a/headphones/torrentfinished.py +++ b/headphones/torrentfinished.py @@ -15,13 +15,15 @@ import threading import headphones -from headphones import db, utorrent, transmission +from headphones import db, utorrent, transmission, logger postprocessor_lock = threading.Lock() # Remove Torrent + data if Post Processed and finished Seeding def checkTorrentFinished(): + logger.info("Checking if any torrents have finished seeding and can be removed") + with postprocessor_lock: myDB = db.DBConnection() diff --git a/headphones/transmission.py b/headphones/transmission.py index 06790b7f..046ccaa0 100644 --- a/headphones/transmission.py +++ b/headphones/transmission.py @@ -98,19 +98,26 @@ def removeTorrent(torrentid, remove_data = False): arguments = { 'ids': torrentid, 'fields': ['isFinished', 'name']} response = torrentAction(method, arguments) + if not response: + return False - finished = response['arguments']['torrents'][0]['isFinished'] - name = response['arguments']['torrents'][0]['name'] + try: + finished = response['arguments']['torrents'][0]['isFinished'] + name = response['arguments']['torrents'][0]['name'] - if finished: - logger.info('%s has finished seeding, removing torrent and data' % name) - method = 'torrent-remove' - if remove_data: - arguments = {'delete-local-data': True, 'ids': torrentid} + if finished: + logger.info('%s has finished seeding, removing torrent and data' % name) + method = 'torrent-remove' + if remove_data: + arguments = {'delete-local-data': True, 'ids': torrentid} + else: + arguments = {'ids': torrentid} + response = torrentAction(method, arguments) + return True else: - arguments = {'ids': torrentid} - response = torrentAction(method, arguments) - return True + logger.info('%s has not finished seeding yet, torrent will not be removed, will try again on next run' % name) + except: + return False return False