[mythtv-users] services API question

Hika van den Hoven hikavdh at gmail.com
Thu Feb 23 20:48:05 UTC 2023


Hoi George,

Thursday, February 23, 2023, 9:14:02 PM, you wrote:

> Hello List folk,


> This question may not be appropriate for this forum, so please
> redirect me if there's a better way to get this answered....


> I'm playing with some python code to get guide data similar to what
> mythweb does with the guide. Although I'm only interested in a subset of the full guide data.


> Anyway, I am using the Channel Service 'GetChannelInfoList' and am
> trying to loop my way through it to see the chanId, chanNum, CallSign.


> The data structures that most of these service api requests result
> in is confusing to me. How do I know what's a dictionary and what's
> a list? When it's a dictionary, how do I know what the keys are?
> Here's an example of a loop where I'm trying to traverse the
> results... This isn't part of any actual project yet, I'm just
> trying to understand how to examine and use the results of these queries...


>    
> resp_dict=backend.send(endpoint="Channel/GetChannelInfoList?SourceID=0&StartIndex=1&Count=33&OnlyVisible=true&Details=false")
>     print ("Response from channel Info query: ", resp_dict)
>     channelInfos=resp_dict['ChannelInfoList']
>     c=0
>     for chan in channelInfos:
>         print(channelInfos[c]['ChanID'],
> channelInfos[c]['ChanNum'], channelInfos[c]['CallSign'])
>         c+=1


> The print command in the for loop is generating an error, "KeyError: 0"
> But I used to have the 'chan' from the for loop there and it was
> giving me an error about indices must be integers, not strings, so
> I'm not sure if it's a list or a dict or what the heck it is...


> How is one supposed to determine the structures of data that are
> returned by the service api calls???


> Thanks for any insight!!

> -- George

Here a simple function to print the output to screen or file for
further analysis:

def print_output(data, spcs='', file = None):
    def print_value(value, spcs='', key = None):
        key = '' if key == None else '"%s" = ' % key
        if isinstance(value, dict):
            output('%s%s<dict>:%s' % (spcs, key, lineend))
            for k, v in value.items():
                print_value(v, '%s    ' % spcs, k)

        elif isinstance(value, list):
            output('%s%s<list>:%s' % (spcs, key, lineend))
            for v in value:
                print_value(v, '%s    ' % spcs)

        else:
            output('%s%s(%s)%s' % (spcs, key, value, lineend))

    output = print if file == None else file.write
    lineend = '' if file == None else '\n'
    print_value(data)

The file object in the calling should be a file opend for writing.


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