diff --git a/headphones/__init__.py b/headphones/__init__.py index 928dfaae..eb8a5dd0 100644 --- a/headphones/__init__.py +++ b/headphones/__init__.py @@ -92,6 +92,8 @@ NZBSORG = False NZBSORG_UID = None NZBSORG_HASH = None +LASTFM_USERNAME = None + def CheckSection(sec): """ Check if INI section exists, if not create it """ try: @@ -149,7 +151,7 @@ def initialize(): ADD_ALBUM_ART, EMBED_ALBUM_ART, DOWNLOAD_DIR, BLACKHOLE, BLACKHOLE_DIR, USENET_RETENTION, NZB_SEARCH_INTERVAL, \ LIBRARYSCAN_INTERVAL, SAB_HOST, SAB_USERNAME, SAB_PASSWORD, SAB_APIKEY, SAB_CATEGORY, \ NZBMATRIX, NZBMATRIX_USERNAME, NZBMATRIX_APIKEY, NEWZNAB, NEWZNAB_HOST, NEWZNAB_APIKEY, \ - NZBSORG, NZBSORG_UID, NZBSORG_HASH + NZBSORG, NZBSORG_UID, NZBSORG_HASH, LASTFM_USERNAME if __INITIALIZED__: return False @@ -217,6 +219,8 @@ def initialize(): NZBSORG = bool(check_setting_int(CFG, 'NZBsorg', 'nzbsorg', 0)) NZBSORG_UID = check_setting_str(CFG, 'NZBsorg', 'nzbsorg_uid', '') NZBSORG_HASH = check_setting_str(CFG, 'NZBsorg', 'nzbsorg_hash', '') + + LASTFM_USERNAME = check_setting_str(CFG, 'General', 'lastfm_username', '') if not LOG_DIR: LOG_DIR = os.path.join(DATA_DIR, 'logs') @@ -376,6 +380,8 @@ def config_write(): new_config['NZBsorg']['nzbsorg_uid'] = NZBSORG_UID new_config['NZBsorg']['nzbsorg_hash'] = NZBSORG_HASH + new_config['General']['lastfm_username'] = LASTFM_USERNAME + new_config.write() diff --git a/headphones/importer.py b/headphones/importer.py index 41bbab38..5e89114c 100644 --- a/headphones/importer.py +++ b/headphones/importer.py @@ -132,7 +132,7 @@ def artistlist_to_mbids(artistlist): newValueDict = {"HaveTracks": havetracks} myDB.upsert("artists", newValueDict, controlValueDict) - # Update the cloud: + # Update the similar artist tag cloud: logger.info('Updating artist information from Last.fm') try: lastfm.getSimilar() diff --git a/headphones/lastfm.py b/headphones/lastfm.py index 3a03bcec..a3e9c8ec 100644 --- a/headphones/lastfm.py +++ b/headphones/lastfm.py @@ -3,6 +3,7 @@ from xml.dom import minidom from collections import defaultdict import random +import headphones from headphones import db api_key = '395e6ec6bb557382fc41fde867bce66f' @@ -32,9 +33,12 @@ def getSimilar(): for node in mbidnode: artist_mbid = node.data - if not any(artist_mbid in x for x in results): - artistlist.append((artist_name, artist_mbid)) - + try: + if not any(artist_mbid in x for x in results): + artistlist.append((artist_name, artist_mbid)) + except: + continue + count = defaultdict(int) for artist, mbid in artistlist: @@ -50,4 +54,41 @@ def getSimilar(): for tuple in top_list: artist_name, artist_mbid = tuple[0] count = tuple[1] - myDB.action('INSERT INTO lastfmcloud VALUES( ?, ?, ?)', [artist_name, artist_mbid, count]) \ No newline at end of file + myDB.action('INSERT INTO lastfmcloud VALUES( ?, ?, ?)', [artist_name, artist_mbid, count]) + +def getArtists(): + + myDB = db.DBConnection() + results = myDB.select('SELECT ArtistID from artists') + + if not headphones.LASTFM_USERNAME: + return + + else: + username = headphones.LASTFM_USERNAME + + url = 'http://ws.audioscrobbler.com/2.0/?method=library.getartists&limit=10000&api_key=%s&user=%s' % (api_key, username) + data = urllib.urlopen(url).read() + d = minidom.parseString(data) + artists = d.getElementsByTagName("artist") + + artistlist = [] + + for artist in artists: + mbidnode = artist.getElementsByTagName("mbid")[0].childNodes + + for node in mbidnode: + artist_mbid = node.data + + try: + if not any(artist_mbid in x for x in results): + artistlist.append(artist_mbid) + except: + continue + + from headphones import importer + + for artistid in artistlist: + importer.addArtisttoDB(artistid) + + \ No newline at end of file diff --git a/headphones/postprocessor.py b/headphones/postprocessor.py index 1c456c62..80b5c686 100644 --- a/headphones/postprocessor.py +++ b/headphones/postprocessor.py @@ -165,6 +165,17 @@ def moveFiles(albumpath, release, tracks): destination_path = os.path.join(headphones.DESTINATION_DIR, folder) destination_path = destination_path.replace('?','_') + if os.path.exists(destination_path): + i = 1 + while True: + new_folder_name = destination_path + '[%i]' % i + if os.path.exists(new_folder_name): + i += 1 + else: + destination_path = new_folder_name + break + + logger.info('Moving files from %s to %s' % (folder, destination_path)) try: @@ -206,7 +217,7 @@ def correctMetadata(albumid, release, downloaded_track_list): return distance, items, info = out_tuples[0] - + logger.debug('Beets recommendation: %s' % rec) autotag.apply_metadata(items, info) for item in items: diff --git a/headphones/webserve.py b/headphones/webserve.py index eac0cfdd..86a05078 100644 --- a/headphones/webserve.py +++ b/headphones/webserve.py @@ -370,14 +370,14 @@ class WebInterface(object): upcoming.exposed = True def manage(self): - if headphones.PATH_TO_XML: - path = headphones.PATH_TO_XML + if headphones.LASTFM_USERNAME: + lastfm_user_text = headphones.LASTFM_USERNAME else: - path = 'Absolute path to iTunes XML or Top-Level Music Directory' + lastfm_user_text = 'Last.FM Username' if headphones.MUSIC_DIR: - path2 = headphones.MUSIC_DIR + music_dir_text = headphones.MUSIC_DIR else: - path2 = 'Enter a directory to scan' + music_dir_text = 'Enter a directory to scan' page = [templates._header] page.append(templates._logobar) page.append(templates._nav) @@ -395,21 +395,27 @@ class WebInterface(object):

-

Import or Sync Your iTunes Library/Music Folder


- This is here for legacy purposes (try the Music Scanner above!)

- If you'd rather import an iTunes .xml file, you can enter the full path here.

-
+

Import Your Last.FM Artists


+ + (this.value==this.defaultValue) this.value='';" name="username" size="18" />

''' % (path2, path)) + Check for Headphones Updates


''' % (music_dir_text, lastfm_user_text)) page.append(templates._footer % headphones.CURRENT_VERSION) return page manage.exposed = True + def importLastFM(self, username): + headphones.LASTFM_USERNAME = username + headphones.config_write() + threading.Thread(target=lastfm.getArtists).start() + time.sleep(10) + raise cherrypy.HTTPRedirect("home") + importLastFM.exposed = True + def importItunes(self, path): headphones.PATH_TO_XML = path headphones.config_write()