Mailing List Archive

services API question
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
Re: services API question [ In reply to ]
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@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

_______________________________________________
mythtv-users mailing list
mythtv-users@mythtv.org
http://lists.mythtv.org/mailman/listinfo/mythtv-users
http://wiki.mythtv.org/Mailing_List_etiquette
MythTV Forums: https://forum.mythtv.org
Re: services API question [ In reply to ]
Hoi Hika,

Thursday, February 23, 2023, 9:48:05 PM, you wrote:

> 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.

Further values are always string. You yourself have to convert them to
int or datetime. Datetime is always in utc.


Tot mails,
Hika mailto:hikavdh@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

_______________________________________________
mythtv-users mailing list
mythtv-users@mythtv.org
http://lists.mythtv.org/mailman/listinfo/mythtv-users
http://wiki.mythtv.org/Mailing_List_etiquette
MythTV Forums: https://forum.mythtv.org
Re: services API question [ In reply to ]
On Thursday 23 February 2023 02:14:02 PM (-06:00), George Bingham 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


Take a peek at this:


#!/usr/bin/python3



import json

from MythTV.services_api import send as api





def main():



backend = api.Send(host='localhost')



resp_dict=backend.send(endpoint="Channel/GetChannelInfoList",


rest="SourceID=0&StartIndex=1&Count=33&OnlyVisible=true&Details=false")


#print(json.dumps(resp_dict, sort_keys=True, indent=4,

# separators=(',', ': ')))



channelInfos = resp_dict['ChannelInfoList']['ChannelInfos']



print(json.dumps(channelInfos, sort_keys=True, indent=4,

separators=(',', ': ')))



for chan in range(0, int(resp_dict['ChannelInfoList']['Count'])):

print(channelInfos[chan]['ChanId'],

channelInfos[chan]['ChanNum'],

channelInfos[chan]['CallSign'])





if __name__ == '__main__':

main()




Note that it's ChanId, not ChanID. The json.dumps() method is helpful to
see what's
returned. Or, you can just send the message to a browser.


Things in [] are lists. Things in {} are dictionaries. I hardcoded the
host, your args.host is
probably fine.


--
Bill
Re: services API question [ In reply to ]
Thanks Hika & Bill,

Both your inputs are very helpful!

-- George


On Thu, Feb 23, 2023 at 4:07 PM Bill Meek <keemllib@gmail.com> wrote:

> On Thursday 23 February 2023 02:14:02 PM (-06:00), George Bingham 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
>
>
> Take a peek at this:
>
> #!/usr/bin/python3
>
>
>
> import json
>
> from MythTV.services_api import send as api
>
>
>
>
>
> def main():
>
>
>
> backend = api.Send(host='localhost')
>
>
>
> resp_dict=backend.send(endpoint="Channel/GetChannelInfoList",
>
>
> rest="SourceID=0&StartIndex=1&Count=33&OnlyVisible=true&Details=false")
>
>
> #print(json.dumps(resp_dict, sort_keys=True, indent=4,
>
> # separators=(',', ': ')))
>
>
>
> channelInfos = resp_dict['ChannelInfoList']['ChannelInfos']
>
>
>
> print(json.dumps(channelInfos, sort_keys=True, indent=4,
>
> separators=(',', ': ')))
>
>
>
> for chan in range(0, int(resp_dict['ChannelInfoList']['Count'])):
>
> print(channelInfos[chan]['ChanId'],
>
> channelInfos[chan]['ChanNum'],
>
> channelInfos[chan]['CallSign'])
>
>
>
>
>
> if __name__ == '__main__':
>
> main()
>
>
> Note that it's ChanId, not ChanID. The json.dumps() method is helpful to
> see what's
> returned. Or, you can just send the message to a browser.
>
> Things in [] are lists. Things in {} are dictionaries. I hardcoded the
> host, your args.host is
> probably fine.
>
> --
> Bill
> _______________________________________________
> mythtv-users mailing list
> mythtv-users@mythtv.org
> http://lists.mythtv.org/mailman/listinfo/mythtv-users
> http://wiki.mythtv.org/Mailing_List_etiquette
> MythTV Forums: https://forum.mythtv.org
>