Last active
December 19, 2015 03:18
-
-
Save azenla/5889049 to your computer and use it in GitHub Desktop.
Freebase API Example - Selects best result - Note that this is Python 2 only
This file contains 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
__author__ = 'Kenneth Endfinger' | |
import json | |
import urllib | |
api_key = 'AIzaSyCq5sypSd_I5n7g7aKYmitNtGSksuBfsKk' | |
query = 'Example' | |
print('Query: ' + query) | |
service_url = 'https://www.googleapis.com/freebase/v1/search' | |
params = { | |
'query': query, | |
'key': api_key | |
} | |
url = service_url + '?' + urllib.urlencode(params) | |
response = json.loads(urllib.urlopen(url).read()) | |
results = response['result'] | |
count = len(results) | |
if count is 0: | |
print('No Results Found.') | |
exit() | |
print('There are {} results. Selecting best result.'.format(count)) | |
largest = 0 | |
best = None | |
for result in response['result']: | |
if result['score'] > largest: | |
largest = result['score'] | |
best = result | |
print('Name: ' + best['name']) | |
print('Score: {}'.format(best['score'])) | |
print('MID: ' + best['mid']) | |
print('ID: ' + best['id']) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment