diff --git a/headphones/__init__.py b/headphones/__init__.py index ad7bd3dc..6d555f48 100644 --- a/headphones/__init__.py +++ b/headphones/__init__.py @@ -225,6 +225,8 @@ HPPASS = None CACHE_SIZEMB = 32 +UMASK = None + def CheckSection(sec): """ Check if INI section exists, if not create it """ try: @@ -291,7 +293,8 @@ def initialize(): PROWL_ENABLED, PROWL_PRIORITY, PROWL_KEYS, PROWL_ONSNATCH, PUSHOVER_ENABLED, PUSHOVER_PRIORITY, PUSHOVER_KEYS, PUSHOVER_ONSNATCH, MIRRORLIST, \ MIRROR, CUSTOMHOST, CUSTOMPORT, CUSTOMSLEEP, HPUSER, HPPASS, XBMC_ENABLED, XBMC_HOST, XBMC_USERNAME, XBMC_PASSWORD, XBMC_UPDATE, \ XBMC_NOTIFY, NMA_ENABLED, NMA_APIKEY, NMA_PRIORITY, NMA_ONSNATCH, SYNOINDEX_ENABLED, ALBUM_COMPLETION_PCT, PREFERRED_BITRATE_HIGH_BUFFER, \ - PREFERRED_BITRATE_LOW_BUFFER, PREFERRED_BITRATE_ALLOW_LOSSLESS, CACHE_SIZEMB + PREFERRED_BITRATE_LOW_BUFFER, PREFERRED_BITRATE_ALLOW_LOSSLESS, CACHE_SIZEMB, \ + UMASK if __INITIALIZED__: return False @@ -584,6 +587,10 @@ def initialize(): else: LATEST_VERSION = CURRENT_VERSION + # Store the original umask + UMASK = os.umask(0) + os.umask(UMASK) + __INITIALIZED__ = True return True diff --git a/headphones/mb.py b/headphones/mb.py index e693c322..f61ef991 100644 --- a/headphones/mb.py +++ b/headphones/mb.py @@ -318,7 +318,10 @@ def getRelease(releaseid, include_artist_info=True): if 'release-group' in results: release['rgid'] = unicode(results['release-group']['id']) release['rg_title'] = unicode(results['release-group']['title']) - release['rg_type'] = unicode(results['release-group']['type']) + try: + release['rg_type'] = unicode(results['release-group']['type']) + except KeyError: + release['rg_type'] = u'Unknown' else: logger.warn("Release " + releaseid + "had no ReleaseGroup associated") diff --git a/headphones/postprocessor.py b/headphones/postprocessor.py index c8800c7b..fd54f17f 100644 --- a/headphones/postprocessor.py +++ b/headphones/postprocessor.py @@ -52,11 +52,11 @@ def checkFolder(): sab_replace_spaces(sab_replace_dots(album['FolderName'])) ] - torrent_album_path = os.path.join(headphones.DOWNLOAD_TORRENT_DIR, album['FolderName']).encode(headphones.SYS_ENCODING) + torrent_album_path = os.path.join(headphones.DOWNLOAD_TORRENT_DIR, album['FolderName']).encode(headphones.SYS_ENCODING,'replace') for nzb_folder_name in nzb_album_possibilities: - nzb_album_path = os.path.join(headphones.DOWNLOAD_DIR, nzb_folder_name).encode(headphones.SYS_ENCODING) + nzb_album_path = os.path.join(headphones.DOWNLOAD_DIR, nzb_folder_name).encode(headphones.SYS_ENCODING, 'replace') if os.path.exists(nzb_album_path): logger.debug('Found %s in NZB download folder. Verifying....' % album['FolderName']) @@ -448,9 +448,11 @@ def addAlbumArt(artwork, albumpath, release): if album_art_name.startswith('.'): album_art_name = album_art_name.replace(0, '_') + prev = os.umask(headphones.UMASK) file = open(os.path.join(albumpath, album_art_name), 'wb') file.write(artwork) file.close() + os.umask(prev) def cleanupFiles(albumpath): logger.info('Cleaning up files') @@ -475,22 +477,24 @@ def moveFiles(albumpath, release, tracks): releasetype = release['Type'].replace('/', '_') if release['ArtistName'].startswith('The '): - sortname = release['ArtistName'][4:] + sortname = release['ArtistName'][4:] + ", The" else: sortname = release['ArtistName'] - if sortname.isdigit(): + if sortname[0].isdigit(): firstchar = '0-9' else: firstchar = sortname[0] values = { '$Artist': artist, + '$SortArtist': sortname, '$Album': album, '$Year': year, '$Type': releasetype, '$First': firstchar.upper(), '$artist': artist.lower(), + '$sortartist': sortname.lower(), '$album': album.lower(), '$year': year, '$type': releasetype.lower(), @@ -776,16 +780,28 @@ def renameFiles(albumpath, downloaded_track_list, release): else: title = f.title + if release['ArtistName'] == "Various Artists" and f.artist: + artistname = f.artist + else: + artistname = release['ArtistName'] + + if artistname.startswith('The '): + sortname = artistname[4:] + ", The" + else: + sortname = artistname + values = { '$Disc': discnumber, '$Track': tracknumber, '$Title': title, - '$Artist': release['ArtistName'], + '$Artist': artistname, + '$SortArtist': sortname, '$Album': release['AlbumTitle'], '$Year': year, '$disc': discnumber, '$track': tracknumber, '$title': title.lower(), - '$artist': release['ArtistName'].lower(), + '$artist': artistname.lower(), + '$sortartist': sortname.lower(), '$album': release['AlbumTitle'].lower(), '$year': year } diff --git a/headphones/searcher.py b/headphones/searcher.py index 9d0e4d01..7a939457 100644 --- a/headphones/searcher.py +++ b/headphones/searcher.py @@ -125,7 +125,7 @@ def searchforalbum(albumid=None, new=False, lossless=False): else: foundNZB = "none" - if (headphones.NZBMATRIX or headphones.NEWZNAB or headphones.NZBSORG or headphones.NEWZBIN) and (headphones.SAB_HOST or headphones.BLACKHOLE): + if (headphones.NZBMATRIX or headphones.NEWZNAB or headphones.NZBSORG or headphones.NEWZBIN or headphones.NZBX or headphones.NZBSRUS) and (headphones.SAB_HOST or headphones.BLACKHOLE): foundNZB = searchNZB(albumid, new, lossless) if (headphones.KAT or headphones.ISOHUNT or headphones.MININOVA or headphones.WAFFLES or headphones.RUTRACKER or headphones.WHATCD) and foundNZB == "none": @@ -348,18 +348,18 @@ def searchNZB(albumid=None, new=False, losslessOnly=False): if data: - d = feedparser.parse(data) + d = json.loads(data.replace('null','None')) - if not len(d.entries): + if d['matches'] <= 0: logger.info(u"No results found from NZBsRus for %s" % term) pass else: - for item in d.entries: + for item in d['results']: try: - url = item.link - title = item.title - size = int(item.links[1]['length']) + url = "http://www.nzbsrus.com/nzbdownload_rss.php/" + item['id'] + "/" + headphones.NZBSRUS_UID + "/" + item['key'] + title = item['name'] + size = int(item['size']) resultlist.append((title, size, url, provider)) logger.info('Found %s. Size: %s' % (title, helpers.bytes_to_mb(size))) @@ -553,9 +553,11 @@ def searchNZB(albumid=None, new=False, losslessOnly=False): nzb_name = nzb_folder_name + '.nzb' download_path = os.path.join(headphones.BLACKHOLE_DIR, nzb_name) try: + prev = os.umask(headphones.UMASK) f = open(download_path, 'w') f.write(data) f.close() + os.umask(prev) logger.info('File saved to: %s' % nzb_name) except Exception, e: logger.error('Couldn\'t write NZB file: %s' % e) @@ -743,6 +745,12 @@ def searchTorrent(albumid=None, new=False, losslessOnly=False): else: term = cleanartist + ' ' + cleanalbum + # Save user search term + if albums[4]: + usersearchterm = term + else: + usersearchterm = '' + semi_clean_artist_term = re.sub('[\.\-\/]', ' ', semi_cleanartist).encode('utf-8', 'replace') semi_clean_album_term = re.sub('[\.\-\/]', ' ', semi_cleanalbum).encode('utf-8', 'replace') # Replace bad characters in the term and unicode it @@ -844,11 +852,17 @@ def searchTorrent(albumid=None, new=False, losslessOnly=False): format = "MP3" maxsize = 300000000 - query_items = ['artist:"%s"' % artistterm, - 'album:"%s"' % albumterm, - 'format:(%s)' % format, - 'size:[0 TO %d]' % maxsize, - '-seeders:0'] # cut out dead torrents + if not usersearchterm: + query_items = ['artist:"%s"' % artistterm, + 'album:"%s"' % albumterm, + 'year:(%s)' % year] + else: + query_items = [usersearchterm] + + query_items.extend(['format:(%s)' % format, + 'size:[0 TO %d]' % maxsize, + '-seeders:0']) # cut out dead torrents + if bitrate: query_items.append('bitrate:"%s"' % bitrate) @@ -903,7 +917,7 @@ def searchTorrent(albumid=None, new=False, losslessOnly=False): # Ignore if release date not specified, results too unpredictable - if not year: + if not year and not usersearchterm: logger.info(u'Release date not specified, ignoring for rutracker.org') else: @@ -922,8 +936,12 @@ def searchTorrent(albumid=None, new=False, losslessOnly=False): bitrate = True # build search url based on above - - searchURL = rutracker.searchurl(artistterm, albumterm, year, format) + + if not usersearchterm: + searchURL = rutracker.searchurl(artistterm, albumterm, year, format) + else: + searchURL = rutracker.searchurl(usersearchterm, ' ', ' ', format) + logger.info(u'Parsing results from rutracker.org' % searchURL) # parse results and get best match @@ -1234,17 +1252,19 @@ def searchTorrent(albumid=None, new=False, losslessOnly=False): download_path = os.path.join(headphones.TORRENTBLACKHOLE_DIR, torrent_name) try: if bestqual[3] == 'rutracker.org': - download_path = rutracker.get_torrent(bestqual[2], headphones.TORRENTBLACKHOLE_DIR) - if not download_path: - break + download_path = rutracker.get_torrent(bestqual[2], headphones.TORRENTBLACKHOLE_DIR) + if not download_path: + break else: - #Write the torrent file to a path derived from the TORRENTBLACKHOLE_DIR and file name. - torrent_file = open(download_path, 'wb') - torrent_file.write(data) - torrent_file.close() - - #Open the fresh torrent file again so we can extract the proper torrent name - #Used later in post-processing. + #Write the torrent file to a path derived from the TORRENTBLACKHOLE_DIR and file name. + prev = os.umask(headphones.UMASK) + torrent_file = open(download_path, 'wb') + torrent_file.write(data) + torrent_file.close() + os.umask(prev) + + #Open the fresh torrent file again so we can extract the proper torrent name + #Used later in post-processing. torrent_file = open(download_path, 'rb') torrent_info = bencode.bdecode(torrent_file.read()) torrent_file.close()