mirror of
https://github.com/rembo10/headphones.git
synced 2026-05-16 08:35:32 +01:00
Fix/Enhance Telegram
This commit is contained in:
@@ -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:
|
||||
|
||||
@@ -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:
|
||||
|
||||
@@ -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 <a href="http://musicbrainz.org/' \
|
||||
'release-group/%s">MusicBrainz</a>' % 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.")
|
||||
|
||||
@@ -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")
|
||||
|
||||
@@ -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()
|
||||
|
||||
Reference in New Issue
Block a user