[mythtv-users] Can commercial flagging be scheduled?

Hika van den Hoven hikavdh at gmail.com
Sun Jan 31 00:13:19 UTC 2016


Hoi Hika,

Saturday, January 30, 2016, 11:54:09 PM, you wrote:

> Hoi Hika,

> Saturday, January 30, 2016, 11:10:18 PM, you wrote:

>> Hoi Mark,

>> Saturday, January 30, 2016, 8:02:13 AM, you wrote:

>>>> On 30 Jan 2016, at 12:43 pm, DryHeat122 . <dryheat122 at gmail.com> wrote:
>>>> 
>>>> Thanks for the responses.  If I can schedule job queue start/end why would I need the script option?
>>>> 

>>> The number one reason in my mind would be because it would stop any
>>> jobs running, not just commflag. However not having metadata lookup
>>> run in real time may not be significant either.

And a more generic one to toggle any of the 7 jobs. Next to Python you
also need the requests python module:
You can either run it through cron or link it to a system action when
you start/stop playing certain recordings that need all you CPU power.
You can activate/deactivate multiple jobs in one command

toggle_jobflag.py

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

import argparse, socket, sys, traceback, requests

def read_commandline():
    parser = argparse.ArgumentParser(description='Toggle the allowance of the given job(s) to run on the given backend.',
                            formatter_class=argparse.RawTextHelpFormatter)

    parser.add_argument('-b', '--backend', type=str, required=True,
        metavar='<hostname>', default=None, dest = 'backend',
        help='backendend dns hostname/ip-address')

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

    parser.add_argument('-H', '--hostname', type=str, required=False,
        metavar='<hostname>', default=None, dest = 'hostname',
        help='The MythTV hostname if not equal to the dns-name')

    parser.add_argument('-d', '--deactivate', required=False, default=[],
        metavar='<job type>', dest = 'deactivate', action = 'append',
        help='Give a job type to deactivate. You can add the option\nmore then ones.')

    parser.add_argument('-a', '--activate', required=False, default=[],
        metavar='<job type>', dest = 'activate', action = 'append',
        help='Give a job type to activate. You can add the option\nmore then ones.\n' + \
                    'Possible job types are:\n' + \
                    '   alljobs (to (de) activate all jobs\n' + \
                    '   commflag\n   metadata\n   transcode\n' + \
                    '   userjob1\n   userjob2\n   userjob3\n   userjob4\n' + \
                    '   the name you gave to one of the userjobs\n')

    try:
        return parser.parse_args()

    except:
        return

def post_api(url, postdata=None):
    try:
        headers = {'User-Agent':'0.27 Python Services API Client',
                            'Accept': 'application/json'}
        url_request = requests.post(url, headers = headers, data = postdata)
        return url_request.json()

    except requests.ConnectionError:
        sys.stderr.write('The host is not available!\n')

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

def check_hostname(args):
    url = 'http://%s:%s/Myth/GetHosts' % (args.backend, args.back_port)
    host_list = post_api(url)['StringList']
    if host_list == None:
        return(False)

    if args.hostname == None:
        args.hostname =args.backend

    for h in host_list:
        if h.lower() == args.hostname.lower():
            args.hostname = h
            return(True)

    else:
        sys.stderr.write('The given Backend/Hostname: %s is not known!\n' % args.hostname)
        return(False)

def check_jobname(args, job):
    job_list = {'commflag': 'JobAllowCommFlag',
                    'metadata': 'JobAllowMetadata',
                    'transcode': 'JobAllowTranscode',
                    'userjob1': 'JobAllowUserJob1',
                    'userjob2': 'JobAllowUserJob2',
                    'userjob3': 'JobAllowUserJob3',
                    'userjob4': 'JobAllowUserJob4'}
    if job.lower() in job_list.keys():
        return job_list[job.lower()]

    job_desc = {'UserJobDesc1': 'JobAllowUserJob1',
                    'UserJobDesc2': 'JobAllowUserJob2',
                    'UserJobDesc3': 'JobAllowUserJob3',
                    'UserJobDesc4': 'JobAllowUserJob4'}

    for k, j in job_desc.items():
        if get_setting(args, k, True).lower() == job.lower():
            return j

    sys.stderr.write('Unknown job type: %s\n' % job)

def get_setting(args, key, no_hostname = False):
    url = 'http://%s:%s/Myth/GetSetting' % (args.backend, args.back_port)
    if no_hostname:
        postdata ={'Key': key}

    else:
        postdata ={'HostName': args.hostname,'Key': key}

    reply = post_api(url, postdata)
    if reply != None:
        return reply['SettingList']['Settings'][key]

def put_setting(args, key, value, no_hostname = False):
    url = 'http://%s:%s/Myth/PutSetting' % (args.backend, args.back_port)
    if no_hostname:
        postdata ={'Key': key, 'Value': value}

    else:
        postdata ={'HostName': args.hostname,'Key': key, 'Value': value}

    reply = post_api(url, postdata)
    if reply == None:
        return False

    return(reply['bool'] == 'true')

def main():
    try:
        # get the commandline parameters
        args = read_commandline()
        if args == None:
            return(0)

        if len(args.activate) == 0 and len(args.deactivate) == 0:
            sys.stderr.write('You should at least give one job type to (de)activate\n')
            return(1)

        if 'alljobs' in args.activate:
            args.activate = ['commflag', 'metadata', 'transcode', 'userjob1', 'userjob2', 'userjob3', 'userjob4']
            args.deactivate = []

        elif 'alljobs' in args.deactivate:
            args.deactivate = ['commflag', 'metadata', 'transcode', 'userjob1', 'userjob2', 'userjob3', 'userjob4']
            args.activate = []

        # Check if the given hostname is known
        if not check_hostname(args):
            return(2)

        # Check if the Flag is unset and adjust it
        for job in args.activate:
            job_name = check_jobname(args, job)
            if job_name == None:
                continue

            job_setting = get_setting(args, job_name)
            if job_setting != '1':
                if not put_setting(args, job_name, '1'):
                    sys.stderr.write('The command returned False!\n')

                else:
                    sys.stderr.write('Activated %s\n' % job)

            else:
                sys.stderr.write('No need to change %s!\n' % job)

        # Check if the Flag is set and adjust it
        for job in args.deactivate:
            job_name = check_jobname(args, job)
            if job_name == None:
                continue

            job_setting = get_setting(args, job_name)
            if job_setting != '0':
                if not put_setting(args, job_name, '0'):
                    sys.stderr.write('The command returned False!\n')

                else:
                    sys.stderr.write('Deactivated %s\n' % job)

            else:
                sys.stderr.write('No need to change %s!\n' % job)

    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())


Tot mails,
  Hika                            mailto:hikavdh at gmail.com

"Zonder hoop kun je niet leven
Zonder leven is er geen hoop
Het eeuwige dilemma
Zeker als je hoop moet vernietigen om te kunnen overleven!"

De lerende Mens



More information about the mythtv-users mailing list