From 6e208fcad11df64fb8cc031e4c2ba5ed6fe84f84 Mon Sep 17 00:00:00 2001 From: KRKeegan <-NOSPAM-kevin@krkeegan.com> Date: Mon, 10 Mar 2008 18:55:49 -0700 Subject: [PATCH] Cache TiVo NPL for 60 seconds, Fix redirect page --- plugins/admin/admin.py | 62 ++++++++++++++++++++++++++--------- plugins/admin/templates/redirect.tmpl | 4 +-- 2 files changed, 48 insertions(+), 18 deletions(-) diff --git a/plugins/admin/admin.py b/plugins/admin/admin.py index d4c3683..7455b13 100644 --- a/plugins/admin/admin.py +++ b/plugins/admin/admin.py @@ -20,7 +20,8 @@ p.pop() p = os.path.sep.join(p) config_file_path = os.path.join(p, 'pyTivo.conf') -status = {} +status = {} #Global variable to control download threads +tivo_cache = {} #Cache of TiVo NPL class Admin(Plugin): CONTENT_TYPE = 'text/html' @@ -35,7 +36,11 @@ class Admin(Plugin): handler.end_headers() t = Template(file=os.path.join(SCRIPTDIR,'templates', 'redirect.tmpl')) t.container = cname - t.text = '

The pyTivo Server has been soft reset.


pyTivo has reloaded the pyTivo.conf file and all changed should now be in effect.' + t.time = '3' + t.url = '/TiVoConnect?Command=Admin&Container=' + cname + t.text = '

The pyTivo Server has been soft reset.


pyTivo has reloaded the pyTivo.conf'+\ + 'file and all changed should now be in effect.
The'+ \ + ' Admin page will reload in 3 seconds.' handler.wfile.write(t) def Admin(self, handler, query): @@ -124,7 +129,11 @@ class Admin(Plugin): handler.end_headers() t = Template(file=os.path.join(SCRIPTDIR,'templates', 'redirect.tmpl')) t.container = cname - t.text = '

Your Settings have been saved.


You settings have been saved to the pyTivo.conf file. However you will need to do a Soft Reset before these changes will take effect.' + t.time = '10' + t.url = '/TiVoConnect?Command=Admin&Container=' + cname + t.text = '

Your Settings have been saved.


You settings have been saved to the pyTivo.conf file.'+\ + 'However you will need to do a Soft Reset before these changes will take effect.'+\ + '
The Admin page will reload in 10 seconds.' handler.wfile.write(t) def NPL(self, handler, query): @@ -150,16 +159,30 @@ class Admin(Plugin): opener = urllib2.build_opener(auth_handler) urllib2.install_opener(opener) - try: - handle = urllib2.urlopen(r) - except IOError, e: - print "Possibly wrong Media Access Key, or IP address for your TiVo." - handler.send_response(404) - handler.end_headers() - return - thepage = handle.read() + if theurl in tivo_cache: #check to see if we have accessed this page before + if tivo_cache[theurl]['thepage'] == '' or (time.time() - tivo_cache[theurl]['thepage_time']) >= 60: #if page is empty or old then retreive it + try: + handle = urllib2.urlopen(r) + except IOError, e: + print "Possibly wrong Media Access Key, or IP address for your TiVo." + handler.send_response(404) + handler.end_headers() + return + tivo_cache[theurl]['thepage'] = handle.read() + tivo_cache[theurl]['thepage_time'] = time.time() + else: #not in cache + tivo_cache[theurl] = {} + try: + handle = urllib2.urlopen(r) + except IOError, e: + print "Possibly wrong Media Access Key, or IP address for your TiVo." + handler.send_response(404) + handler.end_headers() + return + tivo_cache[theurl]['thepage'] = handle.read() + tivo_cache[theurl]['thepage_time'] = time.time() - xmldoc = minidom.parseString(thepage) + xmldoc = minidom.parseString(tivo_cache[theurl]['thepage']) items = xmldoc.getElementsByTagName('Item') data = [] @@ -285,14 +308,20 @@ class Admin(Plugin): handler.end_headers() t = Template(file=os.path.join(SCRIPTDIR,'templates', 'redirect.tmpl')) t.container = cname - t.text = '

Transfer Initiated.


You selected transfer has been initiated.' + t.time = '3' + t.url = '/TiVoConnect?Command=NPL&Container=' + cname + t.text = '

Transfer Initiated.


You selected transfer has been initiated.'+\ + '
The ToGo page will reload in 3 seconds.' handler.wfile.write(t) else: handler.send_response(200) handler.end_headers() t = Template(file=os.path.join(SCRIPTDIR,'templates', 'redirect.tmpl')) t.container = cname - t.text = '

Missing Data.


You must set both "tivo_mak" and "togo_path" before using this function.' + t.time = '10' + t.url = '/TiVoConnect?Command=NPL&Container=' + cname + t.text = '

Missing Data.


You must set both "tivo_mak" and "togo_path" before using this function.'+\ + '
The ToGo page will reload in 10 seconds.' handler.wfile.write(t) def ToGoStop(self, handler, query): @@ -307,5 +336,8 @@ class Admin(Plugin): handler.end_headers() t = Template(file=os.path.join(SCRIPTDIR,'templates', 'redirect.tmpl')) t.container = cname - t.text = '

Transfer Stopped.


Your transfer has been stopped.' + t.time = '3' + t.url = '/TiVoConnect?Command=NPL&Container=' + cname + t.text = '

Transfer Stopped.


Your transfer has been stopped.'+\ + '
The ToGo page will reload in 3 seconds.' handler.wfile.write(t) diff --git a/plugins/admin/templates/redirect.tmpl b/plugins/admin/templates/redirect.tmpl index c66aa76..3f271db 100644 --- a/plugins/admin/templates/redirect.tmpl +++ b/plugins/admin/templates/redirect.tmpl @@ -1,10 +1,8 @@ - + $text -
-The Admin page will reload in 10 seconds. -- 2.11.4.GIT