# Test EPG client.  EPG server must be running.
#
# With no arguments, this will query the EPG server for all programs currently
# running.  With arguments, this will query all programs that match against 
# the keywords supplied as arguments.

import os, time, sys, textwrap
import kaa
from kaa.epg2 import *

def update_progress(cur, total):
    n = 0
    if total > 0:
        n = int((cur / float(total)) * 50)
    sys.stdout.write("|%51s| %d / %d\r" % (("="*n + ">").ljust(51), cur, total))
    sys.stdout.flush()
    if cur == total:
        print

guide = GuideClient("epg")
guide.signals["update_progress"].connect(update_progress)

# Initial import
if guide.get_num_programs() == 0:
    # update() is asynchronous so we enter kaa.main() and exit it
    # once the update is finished.
    guide.signals["updated"].connect(sys.exit)

    # xmltv backend: specify path to XML file:
    guide.update("xmltv", os.path.expanduser("~/.freevo/TV.xml"))

    # zap2it backend, specify username/passwd and optional start/stop time (GMT)
    # guide.update("zap2it", username="uname", passwd="passwd")
    kaa.main()

t0 = time.time()
if len(sys.argv) > 1:
    keywords = " ".join(sys.argv[1:])
    print "Results for '%s':" % keywords
    programs = guide.search(keywords = keywords)
    # Sort by start time
    programs.sort(lambda a, b: cmp(a.start, b.start))
else:
    print "All programs currently playing:"
    programs = guide.search(time = time.time())
    # Sort by channel
    programs.sort(lambda a, b: cmp(a.channel.channel, b.channel.channel))
t1 = time.time()

for program in programs:
    start_time = time.strftime("%a %H:%M", time.localtime(program.start))
    print "  %s (%s): %s" % (program.channel.channel, start_time, program.title)
    if program.desc:
        print "\t* " + "\n\t  ".join(textwrap.wrap(program.desc, 60))
print "- Queried %d programs; %s results; %.04f seconds" % \
      (guide.get_num_programs(), len(programs), t1-t0)

@@ -911,46 +911,19 @@
 v%22, 
-os.path.expanduser(%22~/.freevo/TV.xml%22)
+sys.argv%5B1%5D
 )%0A%0A 
@@ -1082,16 +1082,54 @@
 main()%0A%0A
+    print 'done'%0A    sys.exit(0)%0A    %0A
 t0 = tim

fix crash because the main loop is shut down