Add a Google News RSS-based renderer. Minor improvements to all
[kiosk.git] / kiosk.py
index 379e196cbdd0563a950cf772692b441b94ee1054..0c8cce09f3a6b0bc92a636ada326cee71eb27cf7 100755 (executable)
--- a/kiosk.py
+++ b/kiosk.py
@@ -1,4 +1,4 @@
-#!/usr/local/bin/python
+#!/usr/local/bin/python3.7
 
 import sys
 import traceback
@@ -29,39 +29,47 @@ def thread_change_current():
             if page != last_page:
                 print('chooser[%s] - EMERGENCY PAGE %s LOAD NEEDED' % (
                     utils.timestamp(), page))
-                f = open(os.path.join(constants.pages_dir,
-                                      "current.shtml"), "w")
-                emit_wrapped(f, page)
-                f.close()
+                try:
+                    f = open(os.path.join(constants.pages_dir,
+                                          'current.shtml'), 'w')
+                    emit_wrapped(f, page)
+                    f.close()
+                except:
+                    print('chooser[%s] - page does not exist?!' % (
+                        utils.timestamp()))
+                    continue
+                last_page = page
+                swap_page_target = now + constants.refresh_period_sec
 
-                # Notify XMLHTTP clients that they need to refresh now.
+                # Also notify XMLHTTP clients that they need to refresh now.
                 path = os.path.join(constants.pages_dir,
-                                    "reload_immediately.html")
+                                    'reload_immediately.html')
                 f = open(path, 'w')
-                f.write("Reload, suckers!")
+                f.write('Reload, suckers!')
                 f.close()
+
+                # Fix this hack... maybe read the webserver logs and see if it
+                # actually was picked up?
                 time.sleep(0.750)
                 os.remove(path)
-                last_page = page
-                swap_page_target = now + constants.refresh_period_sec
 
         elif now >= swap_page_target:
             if (page == last_page):
-                print('chooser[%s] - nominal choice got the same page...' % (
-                    utils.timestamp()))
+                print(('chooser[%s] - nominal choice got the same page...' % (
+                    utils.timestamp())))
                 continue
             print('chooser[%s] - nominal choice of %s' % (utils.timestamp(), page))
             try:
                 f = open(os.path.join(constants.pages_dir,
-                                      "current.shtml"), "w")
+                                      'current.shtml'), 'w')
                 emit_wrapped(f, page)
                 f.close()
-                last_page = page
-                swap_page_target = now + constants.refresh_period_sec
             except:
                 print('chooser[%s] - page does not exist?!' % (utils.timestamp()))
                 continue
-        time.sleep(1.0)
+            last_page = page
+            swap_page_target = now + constants.refresh_period_sec
+        time.sleep(1)
 
 def pick_background_color():
     now = datetime.now()
@@ -242,17 +250,26 @@ def emit_wrapped(f, filename):
 
 def thread_invoke_renderers():
     while True:
+        print("renderer[%s]: invoking all renderers in catalog..." % (
+            utils.timestamp()))
         for r in renderer_catalog.get_renderers():
+            now = time.time()
             try:
                 r.render()
             except Exception as e:
                 traceback.print_exc()
-                print("renderer[%s] unknown exception, swallowing it." % (
-                    utils.timestamp()))
+                print("renderer[%s] unknown exception in %s, swallowing it." % (
+                    utils.timestamp(), r.get_name()))
             except Error as e:
                 traceback.print_exc()
-                print("renderer[%s] unknown error, swallowing it." % (
-                    utils.timestamp()))
+                print("renderer[%s] unknown error in %s, swallowing it." % (
+                    utils.timestamp(), r.get_name()))
+            delta = time.time() - now
+            if (delta > 1.0):
+                print("renderer[%s]: Warning: %s's rendering took %5.2fs." % (
+                    utils.timestamp(), r.get_name(), delta))
+        print("renderer[%s]: thread having a little break for %ds..." % (
+            utils.timestamp(), constants.render_period_sec))
         time.sleep(constants.render_period_sec)
 
 if __name__ == "__main__":
@@ -262,13 +279,15 @@ if __name__ == "__main__":
     while True:
         if (changer_thread == None or
             not changer_thread.is_alive()):
-            print("chooser[%s] - (Re?)initializing chooser thread..." % utils.timestamp())
+            print("MAIN[%s] - (Re?)initializing chooser thread..." % (
+                utils.timestamp()))
             changer_thread = Thread(target = thread_change_current, args=())
             changer_thread.start()
         if (renderer_thread == None or
             not renderer_thread.is_alive()):
-            print("renderer[%s] - (Re?)initializing render thread..." % utils.timestamp())
+            print("MAIN[%s] - (Re?)initializing render thread..." % (
+                utils.timestamp()))
             renderer_thread = Thread(target = thread_invoke_renderers, args=())
             renderer_thread.start()
-        time.sleep(10000)
+        time.sleep(60)
     print("Should never get here.")