dave-p / TVH-API-docs

User-created documentation for the TVHeadend HTTP API
Other
64 stars 7 forks source link

API to disable / enable an autorec? #4

Closed mathieugouin closed 3 years ago

mathieugouin commented 3 years ago

Hello!

First thanks a lot for this wonderful doc of the tvheadend API.

I was curious if you know of a way via API to disable and enable an autorec?

I'm asking because I realized that once scheduled, tvheadend does update timer derived from autorecs if the xmltv is later updated. My idea was to disable, the re-enable all autorecs timer to force tvheadend to "update" them with the latest xmltv EPG.

Thanks in advance!

dave-p commented 3 years ago

Changing the enable flag on an autorec should be possible using the idnode API but I doubt that it would achieve what you are asking. As far as I am aware, an autorec simply creates timers for matching events. You could delete the autorec (which would delete all pending timers) then re-create it, but that would be rather drastic. Tvheadend does update timers from the on-air EPG, I'm surprised that it doesn't do the same from XMLTV.

mathieugouin commented 3 years ago

When I use the web UI for thvheadend (ex: http://IP_ADDRESS:9981/extjs.html), and I go to the autorec tab, I can perform the disable / enable from there. Actually, I have to disable, save, enable, save. By doing so, from what I understand, it forces tvheadend to re-create the matching EPG entries to the autorecs.

I'm surprised that the on-air EPG triggers update of the timer entries? I was never able to make it work with XMLTV :( Where I live, the on-air EPG is not usable (some channels don't populate it).

My setup is as follows (maybe you can see something):

From what I saw, as soon as a new EPG entries is added in the XMLTV (14 days in the future), a new timer entries is created and never updated if the EPG changes down the road.

Thanks again, I will try the idnode api as you mention and let you know how it works.

mathieugouin commented 3 years ago

Wow it works like a charm!

If it can help someone, here is my quick python script to do it:

#!/usr/bin/python3

import json
import requests
import urllib
import time

TVH_SERVER = 'http://192.168.1.23:9981'

def get_api_url(api):
    url = TVH_SERVER + '/api/' + api
    ts_response = requests.get(url)
    if ts_response.status_code != 200:
        print('Error code %d\n%s' % (ts_response.status_code, ts_response.content))
        return {}
    ts_json = json.loads(ts_response.text, strict=False)
    return ts_json

def get_autorecs():
    return get_api_url('dvr/autorec/grid')

def get_idnode(uuid):
    return get_api_url('idnode/load?uuid=' + uuid)

def set_autorec_enabled(uuid, enabled):
    js = {
        'uuid' : uuid,
        'enabled' : enabled
    }
    api = 'idnode/save?node=' + urllib.parse.quote(json.dumps(js))
    #print(api)
    return get_api_url(api)

def json_pp(js):
    print(json.dumps(js, indent=4, sort_keys=False))

def main():
    autorecs = get_autorecs()
    #json_pp(autorecs)

    enabled_uuid = []

    if autorecs and 'entries' in autorecs:
        for e in autorecs['entries']:
            if e['enabled']:
                #print("%s\t%s\t%s" % (e['uuid'], e['enabled'], e['name']))
                uuid = e['uuid']
                enabled_uuid.append(uuid)
                # disable
                set_autorec_enabled(uuid, False)

    time.sleep(3)

    for uuid in enabled_uuid:
        print("Enabling " + uuid)
        set_autorec_enabled(uuid, True)

if __name__ == "__main__":
    main()
dave-p commented 3 years ago

Excellent, glad you could make it work.