Last active
March 18, 2016 23:52
-
-
Save Twoure/68e086af04532dd8e40d to your computer and use it in GitHub Desktop.
Example info for hatton33 question Developing Channel using JSON. Source https://forums.plex.tv/discussion/211265/developing-channel-using-json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python | |
| """ | |
| Example info for @hatton33 question 'Developing Channel using JSON' | |
| https://forums.plex.tv/discussion/211265/developing-channel-using-json | |
| """ | |
| import requests | |
| import json | |
| URL = 'http://api.import.io/store/connector/c59fa8cc-1d7a-4823-9d2d-cdc555c1218b/_query?input=webpage/url:http%3A%2F%2Fwww.getyourfixtures.com%2Fall%2Ffixtures%2Ftoday%2Ffootball%2F2-EnglishPremierLeague&&_apikey=75e89fac15d74ce5a75b7af4c3f39a6cfdaaf56ebb7e158a18658686c8894c8e4866df29b5cc8ee566fff1e0317f7daf2069a4c028b546ccbdd19f5e4c01de0f713169d604f3e363046152a3e3b031c7' | |
| page = requests.get(URL).text | |
| d = json.loads(page) | |
| # the above is the same as saying the below in PMS | |
| #d = JSON.ObjectFromURL(URL) | |
| # keys() gives a list of valid keys for d | |
| print 'List of valid keys for d\n%s\n' %d.keys() | |
| # in PMS use Log() in place of print to view info when debugging | |
| #Log(d.keys()) | |
| # or label the info useful | |
| #Log('Valid Json Keys for %s\n%s' %(URL, d.keys())) | |
| # checking the keys of the first item in the d['results'] list | |
| # wanted to see what keys were valid for the results list | |
| print 'Valid Json Keys from first item in results list from %s\n%s\n' %(URL, d['results'][0].keys()) | |
| # following the example code from original question | |
| for f in d['results']: | |
| # well not all keys are the same in ever results list | |
| #print f.keys() # used this to see the initial issue | |
| # Found out that not all results have OFFICIAL_LINK so will return an error and fail to run | |
| # instead test for the key first befor calling it | |
| if 'OFFICIAL_LINK' in f.keys(): | |
| link = f['OFFICIAL_LINK'] | |
| print 'Official Link = %s' %link | |
| elif 'COMPETITION_LINK' in f.keys(): | |
| link = f['COMPETITION_LINK'] | |
| print 'Competition Link = %s' %link | |
| else: | |
| print 'Official Link = NOT Available' | |
| # HOME_VALUE seems to be within ever f so no need to test it | |
| title = f['HOME_VALUE'] | |
| print 'Link Title = %s\n' %title |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment