commit d915a65900558f5e8ced5b07c493a50ed4fb606c
Author: David Sorber <david.sorber@gmail.com>
Date:   Thu Jan 24 13:41:57 2019 -0500

    Adding hbkcd_status script to get status of current encode. Also added
    feature to (optionally) send email once an encode is completed.

diff --git a/software/hbkcd/hbkcd/command_server.py b/software/hbkcd/hbkcd/command_server.py
index f0e694b..6cb3444 100755
--- a/software/hbkcd/hbkcd/command_server.py
+++ b/software/hbkcd/hbkcd/command_server.py
@@ -103,7 +103,7 @@ class CommandHandler(BaseHTTPRequestHandler):
                     
             except Exception as exp:
                 response_dict['success'] = False
-                error = '{:s}: {!r}'.format(type(exp).__name__, exp.args)                
+                error = '{:s}: {!r}'.format(type(exp).__name__, exp.args)
                 response_dict['message'] = error
                 logging.error(error)
         
diff --git a/software/hbkcd/hbkcd/config.py b/software/hbkcd/hbkcd/config.py
index f9aba12..56c22a2 100644
--- a/software/hbkcd/hbkcd/config.py
+++ b/software/hbkcd/hbkcd/config.py
@@ -33,3 +33,6 @@ FFPROBE_CMD = [FFPROBE_PATH, '-show_streams', '-show_entries',
 
 # Command server port
 COMMAND_SERVER_PORT = 8081
+
+# Enable encode completion emails
+ENABLE_ENCODE_COMPLETE_EMAILS = True
diff --git a/software/hbkcd/hbkcd/daemon.py b/software/hbkcd/hbkcd/daemon.py
index d10d7cf..fd1a45d 100755
--- a/software/hbkcd/hbkcd/daemon.py
+++ b/software/hbkcd/hbkcd/daemon.py
@@ -1,7 +1,9 @@
 #!/usr/bin/python3
 import datetime
+from email.mime.text import MIMEText
 import logging
 import os
+from smtplib import SMTP_SSL
 import subprocess
 import sys
 import threading
@@ -11,10 +13,33 @@ from hbkcd.config import *
 from hbkcd.command_server import StoppableHTTPServer, CommandHandler
 from hbkcd.queue import ENCODE_QUEUE
 
+# These should probably be protected... 
+RELAY_EMAIL = 'dsorber.phalanx@gmail.com'
+RELAY_PASSWD = "You'reNotMySupervisor"
+TO_EMAIL = 'baranovich@gmail.com'
+
 #
 # HbkCD - Handbrake Control Daemon
 #
 
+def send_email(subject, msg_text): 
+    """ Simple helper function to send an email
+    """
+    msg = MIMEText(msg_text, 'plain')
+    msg['Subject'] = subject
+    msg['To'] = TO_EMAIL
+    try:
+        conn = SMTP_SSL('smtp.gmail.com')
+        conn.set_debuglevel(True)
+        conn.login(RELAY_EMAIL, RELAY_PASSWD)
+        try:
+            conn.sendmail(RELAY_EMAIL, TO_EMAIL, msg.as_string())
+        finally:
+            conn.close()
+    except Exception as exp:
+        error = '{:s}: {!r}'.format(type(exp).__name__, exp.args)
+        logging.error(error)
+
 def __test_executable(exe_name, exe_path):
     """ Helper function to cut down on code duplication.
     """
@@ -138,13 +163,26 @@ def main():
             
             # Print success or failure message to the log depending on the
             # HandBrake process's return code
+            status = ''
             if hbk_proc.returncode == 0:
+                status = 'COMPLETED SUCCESSFULLY'
                 fmt_str = 'Encode of {:s} completed in {:s} (queued for: {:s})'
                 logging.info(fmt_str.format(item.path, elapsed_str, queued_str))
             else:
+                status = 'ERROR'
                 fmt_str = 'Error while encoding {:s} (queued for: {:s})'
                 logging.error(fmt_str.format(item.path, queued_str))
             
+            # If configured send encode complete email
+            if ENABLE_ENCODE_COMPLETE_EMAILS:
+                subject = 'hbkcd Encode Complete'
+                msg = '{:s}\n\nEncoded: {:s}\nDuration: {:s}\n'\
+                      'Queued time: {:s}\nEncode log: {:s}\n'
+                
+                send_email(subject, msg.format(status, item.path, elapsed_str, 
+                                               queued_str, log_path))
+                logging.debug('Sent encode complete email')
+            
             # Remove the item from the queue
             ENCODE_QUEUE.remove(0)
             
diff --git a/software/hbkcd/install.sh b/software/hbkcd/install.sh
index f0bcfa6..bec5177 100755
--- a/software/hbkcd/install.sh
+++ b/software/hbkcd/install.sh
@@ -96,6 +96,7 @@ printf "DONE\n"
 printf "\n\n"
 
 # Install hbkcd Python module
+# TODO: need to figure out a clean way to detect if this fails and halt
 ./setup.py install
 printf "\n\n"
 
@@ -104,6 +105,7 @@ printf "Installing hbkcd scripts..."
 install -m 755 scripts/hbkcd_add.sh /usr/local/bin/hbkcd_add
 install -m 755 scripts/hbkcd_queue.sh /usr/local/bin/hbkcd_queue
 install -m 755 scripts/hbkcd_remove.sh /usr/local/bin/hbkcd_remove
+install -m 755 scripts/hbkcd_status.sh /usr/local/bin/hbkcd_status
 printf "DONE\n"
 printf "\n\n"
 
