diff --git a/headphones/cache.py b/headphones/cache.py index 616dd9e9..4b3df58c 100644 --- a/headphones/cache.py +++ b/headphones/cache.py @@ -236,8 +236,8 @@ class Cache(object): image_url = data['artistthumb'][0]['url'] elif data.get('artistbackground'): image_url = data['artistbackground'][0]['url'] - elif data.get('hdmusiclogo'): - image_url = data['hdmusiclogo'][0]['url'] + # elif data.get('hdmusiclogo'): + # image_url = data['hdmusiclogo'][0]['url'] # fallback to 1st album cover if none of the above elif 'albums' in data: @@ -348,8 +348,8 @@ class Cache(object): image_url = data['artistthumb'][0]['url'] elif data.get('artistbackground'): image_url = data['artistbackground'][0]['url'] - elif data.get('hdmusiclogo'): - image_url = data['hdmusiclogo'][0]['url'] + # elif data.get('hdmusiclogo'): + # image_url = data['hdmusiclogo'][0]['url'] # fallback to 1st album cover if none of the above elif 'albums' in data: diff --git a/headphones/librarysync.py b/headphones/librarysync.py index a04d3535..50b7187b 100644 --- a/headphones/librarysync.py +++ b/headphones/librarysync.py @@ -364,6 +364,11 @@ def libraryScan(dir=None, append=False, ArtistID=None, ArtistName=None, for artist in unique_artists: + if not artist: + continue + + logger.info('Processing artist: %s' % artist) + # check if artist is already in the db artist_lookup = "\"" + artist.replace("\"", "\"\"") + "\"" @@ -399,20 +404,24 @@ def libraryScan(dir=None, append=False, ArtistID=None, ArtistName=None, # [artist])) # ) - havetracks = ( - len(myDB.select( - 'SELECT ArtistID From tracks WHERE ArtistID = ? AND Location IS NOT NULL', - [artistid])) + len(myDB.select( - 'SELECT ArtistName FROM have WHERE ArtistName LIKE ' + artist_lookup + ' AND Matched = "Failed"')) - ) + + try: + havetracks = ( + len(myDB.select( + 'SELECT ArtistID From tracks WHERE ArtistID = ? AND Location IS NOT NULL', + [artistid])) + len(myDB.select( + 'SELECT ArtistName FROM have WHERE ArtistName LIKE ' + artist_lookup + ' AND Matched = "Failed"')) + ) + except Exception as e: + logger.warn('Error updating counts for artist: %s: %s' % (artist, e)) # Note: some people complain about having "artist have tracks" > # of tracks total in artist official releases # (can fix by getting rid of second len statement) - myDB.action('UPDATE artists SET HaveTracks = ? WHERE ArtistID = ?', [havetracks, artistid]) - - # Update albums to downloaded if havetracks: + myDB.action('UPDATE artists SET HaveTracks = ? WHERE ArtistID = ?', [havetracks, artistid]) + + # Update albums to downloaded update_album_status(ArtistID=artistid) logger.info('Found %i new artists' % len(new_artist_list)) @@ -436,11 +445,16 @@ def libraryScan(dir=None, append=False, ArtistID=None, ArtistName=None, logger.info('Updating artist track counts') artist_lookup = "\"" + ArtistName.replace("\"", "\"\"") + "\"" - havetracks = len( - myDB.select('SELECT ArtistID FROM tracks WHERE ArtistID = ? AND Location IS NOT NULL', - [ArtistID])) + len(myDB.select( - 'SELECT ArtistName FROM have WHERE ArtistName LIKE ' + artist_lookup + ' AND Matched = "Failed"')) - myDB.action('UPDATE artists SET HaveTracks=? WHERE ArtistID=?', [havetracks, ArtistID]) + try: + havetracks = len( + myDB.select('SELECT ArtistID FROM tracks WHERE ArtistID = ? AND Location IS NOT NULL', + [ArtistID])) + len(myDB.select( + 'SELECT ArtistName FROM have WHERE ArtistName LIKE ' + artist_lookup + ' AND Matched = "Failed"')) + except Exception as e: + logger.warn('Error updating counts for artist: %s: %s' % (ArtistName, e)) + + if havetracks: + myDB.action('UPDATE artists SET HaveTracks=? WHERE ArtistID=?', [havetracks, ArtistID]) # Moved above to call for each artist # if not append: diff --git a/headphones/notifiers.py b/headphones/notifiers.py index 15e5e310..656885ac 100644 --- a/headphones/notifiers.py +++ b/headphones/notifiers.py @@ -1017,7 +1017,7 @@ class Email(object): class TELEGRAM(object): - def notify(self, message, status): + def notify(self, message, status, rgid=None, image=None): if not headphones.CONFIG.TELEGRAM_ENABLED: return @@ -1030,22 +1030,34 @@ class TELEGRAM(object): userid = headphones.CONFIG.TELEGRAM_USERID # Construct message - payload = {'chat_id': userid, 'text': status + ': ' + message} + message = '\n\n' + message + + # MusicBrainz link + if rgid: + message += '\n\n MusicBrainz' % rgid + + # Send image + if image: + image_file = {'photo': (image, open(image, "rb"))} + payload = {'chat_id': userid, 'parse_mode': "HTML", 'caption': status + message} + try: + response = requests.post(TELEGRAM_API % (token, "sendPhoto"), data=payload, files=image_file) + except Exception, e: + logger.info(u'Telegram notify failed: ' + str(e)) + # Sent text + else: + payload = {'chat_id': userid, 'parse_mode': "HTML", 'text': status + message} + try: + response = requests.post(TELEGRAM_API % (token, "sendMessage"), data=payload) + except Exception, e: + logger.info(u'Telegram notify failed: ' + str(e)) - # Send message to user using Telegram's Bot API - try: - response = requests.post(TELEGRAM_API % (token, "sendMessage"), - data=payload) - except Exception, e: - logger.info(u'Telegram notify failed: ' + str(e)) # Error logging sent_successfuly = True if not response.status_code == 200: - logger.info( - u'Could not send notification to TelegramBot ' - u'(token=%s). Response: [%s]', - (token, response.text)) + logger.info("Could not send notification to TelegramBot (token=%s). Response: [%s]", token, response.text) sent_successfuly = False logger.info(u"Telegram notifications sent.") diff --git a/headphones/postprocessor.py b/headphones/postprocessor.py index b2f7126b..5c7f4ebe 100755 --- a/headphones/postprocessor.py +++ b/headphones/postprocessor.py @@ -586,7 +586,7 @@ def doPostProcessing(albumid, albumpath, release, tracks, downloaded_track_list, if headphones.CONFIG.TELEGRAM_ENABLED: logger.info(u"Telegram request") telegram = notifiers.TELEGRAM() - telegram.notify(pushmessage, statusmessage) + telegram.notify(statusmessage, pushmessage) if headphones.CONFIG.TWITTER_ENABLED: logger.info(u"Sending Twitter notification") diff --git a/headphones/searcher.py b/headphones/searcher.py index 1e51dc58..d7536b22 100644 --- a/headphones/searcher.py +++ b/headphones/searcher.py @@ -1075,8 +1075,12 @@ def send_to_downloader(data, bestqual, album): slack.notify(name, "Download started") if headphones.CONFIG.TELEGRAM_ENABLED and headphones.CONFIG.TELEGRAM_ONSNATCH: logger.info(u"Sending Telegram notification") + from headphones import cache + c = cache.Cache() + album_art = c.get_artwork_from_cache(None, rgid) telegram = notifiers.TELEGRAM() - telegram.notify(name, "Download started") + message = 'Snatched from ' + provider + '. ' + name + telegram.notify(message, "Snatched: " + title, rgid, image=album_art) if headphones.CONFIG.TWITTER_ENABLED and headphones.CONFIG.TWITTER_ONSNATCH: logger.info(u"Sending Twitter notification") twitter = notifiers.TwitterNotifier()