[mythtv-users] services API question

Bill Meek keemllib at gmail.com
Thu Feb 23 22:06:31 UTC 2023


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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.mythtv.org/pipermail/mythtv-users/attachments/20230223/6c4b27d7/attachment.htm>


More information about the mythtv-users mailing list