[mythtv-users] using mythtv as clockradio

Hika van den Hoven hikavdh at gmail.com
Thu Jan 21 00:24:06 UTC 2016


It is not by far ideal but. It at present it always sends a select
after starting Live TV etc. So if the Guide is not enabled it is very
inefficient!

start_live_tv.py

#!/usr/bin/env python2
# -*- coding: utf-8 -*-

import argparse, socket, urllib2, sys, re, traceback, time
from xml.etree import cElementTree as ET

def read_commandline():
    parser = argparse.ArgumentParser(description='Start LiveTV on the (optionaly) given channel.')

    parser.add_argument('-f', '--host', type=str, required=False,
        metavar='<hostname>', default=socket.gethostname(), dest = 'host',
        help='frontend dns hostname/ip-address, defaults to the local hostname')

    parser.add_argument('-p', '--portnumber', type=int, required=False,
        metavar='<portnr>', default=6547, dest = 'port',
        help='the frontend api portnumber, default is 6547')

    parser.add_argument('-c', '--channel', type=int, required=False,
        metavar='<channelnr>', default=None, dest = 'channel',
        help='the tv/radio channel to tune to')

    #~ parser.add_argument('-v', '--verbose', action='store_true', dest = 'verbose',
        #~ required=False, help='print the result to the screen')

    try:
        return parser.parse_args()

    except:
        return

def is_idle(host, port):
    url = 'http://%s:%s/Frontend/GetStatus' % (host, port)

    try:
        #~ print urllib2.urlopen(url).read().decode('utf-8', 'ignore')
        response = ET.parse(urllib2.urlopen(url))
        for val in response.findall('./State/String'):
            if val.findtext('Key') == 'state':
                return (val.findtext('Value') == 'idle')

    except:
        sys.stderr.write('An unexpected error has occured:\n')
        sys.stderr.write(traceback.format_exc())

def send_action(host, port, api_call):
    url = 'http://%s:%s/Frontend/SendAction?Action=%s' % (host, port, re.sub(' ', '%20', api_call))

    try:
        response = ET.parse(urllib2.urlopen(url))
        return response.findtext('.')

    except:
        sys.stderr.write('An unexpected error has occured:\n')
        sys.stderr.write(traceback.format_exc())

def main():
    try:
        args = read_commandline()
        if args == None:
            return(0)

        if not is_idle(args.host, args.port):
            sys.stderr.write('The Frontend %s is busy or otherwise unavailable\n' % args.host)
            return(2)


        if send_action(args.host, args.port, 'Live TV') != 'true':
            sys.stderr.write('The Frontend %s returned False on starting Live TV:\n' % args.host)

        time.sleep(2)
        send_action(args.host, args.port, 'SELECT')
        time.sleep(2)
        if args.channel != None:
            ch = str(args.channel)
            for chn in ch:
                send_action(args.host, args.port, chn)
                send_action(args.host, args.port, 'SELECT')

    except:
        sys.stderr.write('An unexpected error has occured:\n')
        sys.stderr.write(traceback.format_exc())

        return(99)

    # and return success
    return(0)
# end main()

# allow this to be a module
if __name__ == '__main__':
    sys.exit(main())



More information about the mythtv-users mailing list