From a92d13652dc7c7f0f150a622e1ba6c6bd5c57b35 Mon Sep 17 00:00:00 2001 From: Ade Date: Fri, 6 Feb 2015 20:28:57 +1300 Subject: [PATCH] Email notifications #1045 --- data/interfaces/default/config.html | 53 +++++++++++++++++++++++++++++ headphones/config.py | 9 +++++ headphones/notifiers.py | 33 ++++++++++++++++++ headphones/postprocessor.py | 6 ++++ headphones/searcher.py | 7 +++- headphones/webserve.py | 13 +++++-- 6 files changed, 118 insertions(+), 3 deletions(-) diff --git a/data/interfaces/default/config.html b/data/interfaces/default/config.html index 3b72169d..28c5dd4c 100644 --- a/data/interfaces/default/config.html +++ b/data/interfaces/default/config.html @@ -1104,6 +1104,39 @@ +
+

Email

+
+ +
+
+
+ +
+
+ +
+
+ +
+
+ +
+
+ +
+
+ +
+
+ +
+
+ +
+
+
+
@@ -1866,6 +1899,26 @@ } }); + if ($("#email").is(":checked")) + { + $("#email_options").show(); + } + else + { + $("#email_options").hide(); + } + + $("#email").click(function(){ + if ($("#email").is(":checked")) + { + $("#email_options").slideDown(); + } + else + { + $("#email_options").slideUp(); + } + }); + if ($("#songkick").is(":checked")) { $("#songkickoptions").show(); diff --git a/headphones/config.py b/headphones/config.py index 4071edd7..741ba9cd 100644 --- a/headphones/config.py +++ b/headphones/config.py @@ -54,6 +54,15 @@ _CONFIG_DEFINITIONS = { 'DOWNLOAD_SCAN_INTERVAL': (int, 'General', 5), 'DOWNLOAD_TORRENT_DIR': (str, 'General', ''), 'DO_NOT_OVERRIDE_GIT_BRANCH': (int, 'General', 0), + 'EMAIL_ENABLED': (int, 'Email', 0), + 'EMAIL_FROM': (str, 'Email', ''), + 'EMAIL_TO': (str, 'Email', ''), + 'EMAIL_SMTP_SERVER': (str, 'Email', ''), + 'EMAIL_SMTP_USER': (str, 'Email', ''), + 'EMAIL_SMTP_PASSWORD': (str, 'Email', ''), + 'EMAIL_SMTP_PORT': (int, 'Email', 25), + 'EMAIL_TLS': (int, 'Email', 0), + 'EMAIL_ONSNATCH': (int, 'Email', 0), 'EMBED_ALBUM_ART': (int, 'General', 0), 'EMBED_LYRICS': (int, 'General', 0), 'ENABLE_HTTPS': (int, 'General', 0), diff --git a/headphones/notifiers.py b/headphones/notifiers.py index e8503b53..9631660d 100644 --- a/headphones/notifiers.py +++ b/headphones/notifiers.py @@ -34,6 +34,11 @@ import json import oauth2 as oauth import pythontwitter as twitter +from email.mime.text import MIMEText +import smtplib +import email.utils + + class GROWL(object): """ Growl notifications, for OS X. @@ -827,3 +832,31 @@ class SubSonicNotifier(object): # Invoke request request.request_response(self.host + "musicFolderSettings.view?scanNow", auth=(self.username, self.password)) + +class Email(object): + + def notify(self, subject, message): + + message = MIMEText(message, 'plain', "utf-8") + message['Subject'] = subject + message['From'] = email.utils.formataddr(('Headphones', headphones.CONFIG.EMAIL_FROM)) + message['To'] = headphones.CONFIG.EMAIL_TO + + try: + mailserver = smtplib.SMTP(headphones.CONFIG.EMAIL_SMTP_SERVER, headphones.CONFIG.EMAIL_SMTP_PORT) + + if (headphones.CONFIG.EMAIL_TLS): + mailserver.starttls() + + mailserver.ehlo() + + if headphones.CONFIG.EMAIL_SMTP_USER: + mailserver.login(headphones.CONFIG.EMAIL_SMTP_USER, headphones.CONFIG.EMAIL_SMTP_PASSWORD) + + mailserver.sendmail(headphones.CONFIG.EMAIL_FROM, headphones.CONFIG.EMAIL_TO, message.as_string()) + mailserver.quit() + return True + + except Exception, e: + logger.warn('Error sending Email: %s' % e) + return False \ No newline at end of file diff --git a/headphones/postprocessor.py b/headphones/postprocessor.py index 5bf06d99..b9f230ff 100755 --- a/headphones/postprocessor.py +++ b/headphones/postprocessor.py @@ -513,6 +513,12 @@ def doPostProcessing(albumid, albumpath, release, tracks, downloaded_track_list, mpc = notifiers.MPC() mpc.notify() + if headphones.CONFIG.EMAIL_ENABLED: + logger.info(u"Sending Email notification") + email = notifiers.Email() + subject = release['ArtistName'] + ' - ' + release['AlbumTitle'] + email.notify(subject, "Download and Postprocessing completed") + def embedAlbumArt(artwork, downloaded_track_list): logger.info('Embedding album art') diff --git a/headphones/searcher.py b/headphones/searcher.py index ea78d744..b92756e9 100644 --- a/headphones/searcher.py +++ b/headphones/searcher.py @@ -946,7 +946,12 @@ def send_to_downloader(data, bestqual, album): b2msg = 'From ' + provider + '

' + name boxcar = notifiers.BOXCAR() boxcar.notify('Headphones snatched: ' + title, b2msg, rgid) - + if headphones.CONFIG.EMAIL_ENABLED and headphones.CONFIG.EMAIL_ONSNATCH: + logger.info(u"Sending Email notification") + email = notifiers.Email() + subject = artist + ' - ' + albumname + message = 'Snatched from ' + provider + '. ' + title + email.notify(subject, message) def verifyresult(title, artistterm, term, lossless): diff --git a/headphones/webserve.py b/headphones/webserve.py index b3b67cac..04740593 100644 --- a/headphones/webserve.py +++ b/headphones/webserve.py @@ -1151,7 +1151,16 @@ class WebInterface(object): "cache_sizemb": headphones.CONFIG.CACHE_SIZEMB, "file_permissions": headphones.CONFIG.FILE_PERMISSIONS, "folder_permissions": headphones.CONFIG.FOLDER_PERMISSIONS, - "mpc_enabled": checked(headphones.CONFIG.MPC_ENABLED) + "mpc_enabled": checked(headphones.CONFIG.MPC_ENABLED), + "email_enabled": checked(headphones.CONFIG.EMAIL_ENABLED), + "email_from": headphones.CONFIG.EMAIL_FROM, + "email_to": headphones.CONFIG.EMAIL_TO, + "email_smtp_server": headphones.CONFIG.EMAIL_SMTP_SERVER, + "email_smtp_user": headphones.CONFIG.EMAIL_SMTP_USER, + "email_smtp_password": headphones.CONFIG.EMAIL_SMTP_PASSWORD, + "email_smtp_port": int(headphones.CONFIG.EMAIL_SMTP_PORT), + "email_tls": checked(headphones.CONFIG.EMAIL_TLS), + "email_onsnatch": checked(headphones.CONFIG.EMAIL_ONSNATCH) } # Need to convert EXTRAS to a dictionary we can pass to the config: @@ -1196,7 +1205,7 @@ class WebInterface(object): "nma_enabled", "nma_onsnatch", "pushalot_enabled", "pushalot_onsnatch", "synoindex_enabled", "pushover_enabled", "pushover_onsnatch", "pushbullet_enabled", "pushbullet_onsnatch", "subsonic_enabled", "twitter_enabled", "twitter_onsnatch", "osx_notify_enabled", "osx_notify_onsnatch", "boxcar_enabled", "boxcar_onsnatch", "songkick_enabled", "songkick_filter_enabled", - "mpc_enabled" + "mpc_enabled", "email_enabled", "email_tls", "email_onsnatch" ] for checked_config in checked_configs: if checked_config not in kwargs: