From f0ac96de9408f19098838e05b304729e717b0530 Mon Sep 17 00:00:00 2001 From: Shane Frischkorn Date: Sat, 20 Jul 2013 20:58:29 +1000 Subject: [PATCH] Added email messaging --- EmailSettings.cfg | 5 +++++ TVEncoder.py | 24 ++++++++++++++------- libemail.py | 28 +++++++++++++++++++++++++ libsettings.py | 50 +++++++++++++++++++++++++++++++++++++++++++- logs/encoding.log | 0 logs/needsaction.log | 0 tests/emailtest.py | 7 +++++++ 7 files changed, 105 insertions(+), 9 deletions(-) create mode 100644 EmailSettings.cfg create mode 100644 libemail.py create mode 100644 logs/encoding.log create mode 100644 logs/needsaction.log create mode 100644 tests/emailtest.py diff --git a/EmailSettings.cfg b/EmailSettings.cfg new file mode 100644 index 0000000..90c69ad --- /dev/null +++ b/EmailSettings.cfg @@ -0,0 +1,5 @@ +SMTPServer = "" +SMTPUser = "" +SMTPPassword = "" +From = "" +To = "" diff --git a/TVEncoder.py b/TVEncoder.py index dad2e57..abdad64 100644 --- a/TVEncoder.py +++ b/TVEncoder.py @@ -10,11 +10,14 @@ import getopt from libfilemanager import FileManager from libsettings import Settings import libhandbrake +import libemail from libtvdatasource import TVData from collections import namedtuple from termcolor import colored import logging +SETTINGS = "settings.cfg" +EMAIL_SETTINGS = "EmailSettings.cfg" def showhelp(): """ @@ -101,15 +104,9 @@ def main(argv): sys.exit(2) inputoptions = processarguments(opts) - settings = Settings("settings.cfg") + settings = Settings(SETTINGS) filemanager = FileManager(settings) - logging.basicConfig(level=logging.DEBUG) - generallogger = createlogger("general", settings.generallogfile(), - logging.DEBUG) - actionlogger = createlogger("action", settings.actionlogfile(), - logging.INFO) - if inputoptions.readonly: if inputoptions.doencode: #Generate the list of files that would be encoded @@ -123,6 +120,13 @@ def main(argv): else: if inputoptions.doencode: #Encode the files and move them to their final destination + + logging.basicConfig(level=logging.DEBUG) + generallogger = createlogger("general", settings.generallogfile(), + logging.DEBUG) + actionlogger = createlogger("action", settings.actionlogfile(), + logging.INFO) + showdata = filemanager.getencodingfiles(inputoptions.readonly) generallogger.info("There are {0} files to process." .format(len(showdata))) @@ -149,6 +153,10 @@ def main(argv): generallogger.info("Processing finished.") generallogger.info("===========================" "=============\n\n") + + libemail.SendEmail(EMAIL_SETTINGS, "Encoding Complete", + "Finished encoding {0} shows." + .format(len(showdata))) else: # Process files for encoding shows = filemanager.getfilestoprepare(inputoptions.numfiles) @@ -163,7 +171,7 @@ def createlogger(name, filename, level): """ logger = logging.getLogger(name) - handler = logging.FileHandler(filename) + handler = logging.FileHandler(filename, mode='w') formatter = logging.Formatter('%(asctime)s %(message)s') handler.setFormatter(formatter) handler.setLevel(level) diff --git a/libemail.py b/libemail.py new file mode 100644 index 0000000..6d3d383 --- /dev/null +++ b/libemail.py @@ -0,0 +1,28 @@ +# -*- coding: utf-8 -*- +""" +Created on Sat Jul 20 20:48:10 2013 + +@author: shanef +""" + +from libsettings import EmailSettings + +import smtplib +from email.mime.text import MIMEText + + +def SendEmail(settingsfilename, subject, body): + settings = EmailSettings(settingsfilename) + + msg = MIMEText(body, "plain") + msg["Subject"] = subject + msg["From"] = settings.getfromaddress() + msg["To"] = settings.gettoaddress() + + s = smtplib.SMTP(settings.getsmtpserver()) + s.ehlo() + s.starttls() + s.login(settings.getsmtpuser(), settings.getsmtppassword()) + s.sendmail(settings.getfromaddress(), [settings.gettoaddress()], + msg.as_string()) + s.quit() diff --git a/libsettings.py b/libsettings.py index 0a5c9fb..771ad80 100644 --- a/libsettings.py +++ b/libsettings.py @@ -231,7 +231,7 @@ class Settings: # just returns what is input def getshow(self, showname): """ - Get the InputDirectory setting for the show, showname. + Get the name of the show, showname. """ showsection = self.__getshowsubsection(showname) if showsection is None: @@ -252,3 +252,51 @@ class Settings: return self.__config["Shows"][show] return None + + +class EmailSettings: + """ + Accessor for the email configuration file + """ + + def __init__(self, settingsfile): + """ + Initialise settingsfile as a configobj + """ + + self.__config = ConfigObj(settingsfile) + + def getsmtpserver(self): + """ + Get the address of the smtp server + """ + + return self.__config["SMTPServer"] + + def getsmtpuser(self): + """ + Get the username for the smtp server + """ + + return self.__config["SMTPUser"] + + def getsmtppassword(self): + """ + Get the username for the smtp server + """ + + return self.__config["SMTPPassword"] + + def getfromaddress(self): + """ + Get the from address for emails + """ + + return self.__config["From"] + + def gettoaddress(self): + """ + Get the to address for emails + """ + + return self.__config["To"] \ No newline at end of file diff --git a/logs/encoding.log b/logs/encoding.log new file mode 100644 index 0000000..e69de29 diff --git a/logs/needsaction.log b/logs/needsaction.log new file mode 100644 index 0000000..e69de29 diff --git a/tests/emailtest.py b/tests/emailtest.py new file mode 100644 index 0000000..e35724c --- /dev/null +++ b/tests/emailtest.py @@ -0,0 +1,7 @@ +# -*- coding: utf-8 -*- +""" +Created on Fri Jul 19 23:31:16 2013 + +@author: shanef +""" +