Added email messaging

This commit is contained in:
2013-07-20 20:58:29 +10:00
parent 00de89d02c
commit f0ac96de94
7 changed files with 105 additions and 9 deletions

5
EmailSettings.cfg Normal file
View File

@@ -0,0 +1,5 @@
SMTPServer = ""
SMTPUser = ""
SMTPPassword = ""
From = ""
To = ""

View File

@@ -10,11 +10,14 @@ import getopt
from libfilemanager import FileManager from libfilemanager import FileManager
from libsettings import Settings from libsettings import Settings
import libhandbrake import libhandbrake
import libemail
from libtvdatasource import TVData from libtvdatasource import TVData
from collections import namedtuple from collections import namedtuple
from termcolor import colored from termcolor import colored
import logging import logging
SETTINGS = "settings.cfg"
EMAIL_SETTINGS = "EmailSettings.cfg"
def showhelp(): def showhelp():
""" """
@@ -101,15 +104,9 @@ def main(argv):
sys.exit(2) sys.exit(2)
inputoptions = processarguments(opts) inputoptions = processarguments(opts)
settings = Settings("settings.cfg") settings = Settings(SETTINGS)
filemanager = FileManager(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.readonly:
if inputoptions.doencode: if inputoptions.doencode:
#Generate the list of files that would be encoded #Generate the list of files that would be encoded
@@ -123,6 +120,13 @@ def main(argv):
else: else:
if inputoptions.doencode: if inputoptions.doencode:
#Encode the files and move them to their final destination #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) showdata = filemanager.getencodingfiles(inputoptions.readonly)
generallogger.info("There are {0} files to process." generallogger.info("There are {0} files to process."
.format(len(showdata))) .format(len(showdata)))
@@ -149,6 +153,10 @@ def main(argv):
generallogger.info("Processing finished.") generallogger.info("Processing finished.")
generallogger.info("===========================" generallogger.info("==========================="
"=============\n\n") "=============\n\n")
libemail.SendEmail(EMAIL_SETTINGS, "Encoding Complete",
"Finished encoding {0} shows."
.format(len(showdata)))
else: else:
# Process files for encoding # Process files for encoding
shows = filemanager.getfilestoprepare(inputoptions.numfiles) shows = filemanager.getfilestoprepare(inputoptions.numfiles)
@@ -163,7 +171,7 @@ def createlogger(name, filename, level):
""" """
logger = logging.getLogger(name) logger = logging.getLogger(name)
handler = logging.FileHandler(filename) handler = logging.FileHandler(filename, mode='w')
formatter = logging.Formatter('%(asctime)s %(message)s') formatter = logging.Formatter('%(asctime)s %(message)s')
handler.setFormatter(formatter) handler.setFormatter(formatter)
handler.setLevel(level) handler.setLevel(level)

28
libemail.py Normal file
View File

@@ -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()

View File

@@ -231,7 +231,7 @@ class Settings:
# just returns what is input # just returns what is input
def getshow(self, showname): def getshow(self, showname):
""" """
Get the InputDirectory setting for the show, showname. Get the name of the show, showname.
""" """
showsection = self.__getshowsubsection(showname) showsection = self.__getshowsubsection(showname)
if showsection is None: if showsection is None:
@@ -252,3 +252,51 @@ class Settings:
return self.__config["Shows"][show] return self.__config["Shows"][show]
return None 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"]

0
logs/encoding.log Normal file
View File

0
logs/needsaction.log Normal file
View File

7
tests/emailtest.py Normal file
View File

@@ -0,0 +1,7 @@
# -*- coding: utf-8 -*-
"""
Created on Fri Jul 19 23:31:16 2013
@author: shanef
"""