Created
October 26, 2011 15:37
-
-
Save ldodds/1316716 to your computer and use it in GitHub Desktop.
Searching a dataset in Kasabi
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
import pytassium | |
import time | |
dataset = pytassium.Dataset('nasa','put-your-api-key-here') | |
# -------------------------- | |
# Use the lookup API | |
# -------------------------- | |
response, data = dataset.lookup('http://data.kasabi.com/dataset/nasa/person/eugeneandrewcernan') | |
if response.status in range(200,300): | |
# data now contains an rdflib.Graph | |
print data.serialize(format="turtle") | |
else: | |
print "Oh no! %d %s - %s" % (response.status, response.reason, body) | |
# -------------------------- | |
# Use the sparql API | |
# -------------------------- | |
response, data = dataset.select('select ?s where {?s a <http://xmlns.com/foaf/0.1/Person>} limit 10') | |
if response.status in range(200,300): | |
# data now contains a dictionary of results | |
print data | |
else: | |
print "Oh no! %d %s - %s" % (response.status, response.reason, body) | |
# -------------------------- | |
# Use the attribution API | |
# -------------------------- | |
response, data = dataset.attribution() | |
# assuming success, data now contains dictionary | |
print data['homepage'] | |
# -------------------------- | |
# Use the search API | |
# -------------------------- | |
# search for 5 results matching apollo | |
response, data = dataset.search("apollo", 5) | |
for result in data['results']: | |
print "%s (score: %s)" % (result['title'], result['score']) | |
# facet on a search for alan, with the name and type fields | |
fields = ['name', 'type'] | |
query = "alan" | |
response, data = dataset.facet(query, fields) | |
for facet in data['fields']: | |
print "Top %ss matching %s" % (facet['name'],query) | |
for term in facet['terms']: | |
print "%s (%s results)" % (term['value'], term['number']) | |
# -------------------------- | |
# Use the reconciliation API | |
# -------------------------- | |
# Reconcile one label | |
response, data = dataset.reconcile('Alan Shepard') | |
print "Best match is: %s" % data['result'][0]['id'] | |
# Reconcile a list of labels | |
labels = ['Neil Armstrong','alan shepard'] | |
response, data = dataset.reconcile(labels) | |
for i in range(0, len(labels)): | |
print "Best match for %s is: %s" % (labels[i], data['q%s'%i]['result'][0]['id']) | |
# Reconcile a label with specific parameters | |
response, data = dataset.reconcile('Apollo 11', limit=3, type='http://purl.org/net/schemas/space/Mission', type_strict ='any') | |
print "Best match is: %s" % data['result'][0]['id'] | |
# Reconcile with a specific query | |
query = { | |
"query" : "Apollo 11", | |
"limit" : 3, | |
"type" : "http://purl.org/net/schemas/space/Mission", | |
"type_strict" : "any", | |
} | |
response, data = dataset.reconcile(query) | |
print "Best match is: %s" % data['result'][0]['id'] | |
# -------------------------- | |
# Use the update API | |
# -------------------------- | |
dataset = pytassium.Dataset('my-writable-dataset','put-your-api-key-here') | |
# Store the contents of a turtle file | |
dataset.store_file('/tmp/mydata.ttl', media_type='text/turtle') | |
# Store data from a string | |
mytriples = "<http://example.com/foo> a <http://example.com/Cat> ." | |
dataset.store_data(mytriples, media_type='text/turtle') | |
# -------------------------- | |
# Use the jobs API | |
# -------------------------- | |
response, job_uri = dataset.schedule_reset() | |
print "Reset scheduled, URI is: %s" % job_uri | |
print "Waiting for reset to complete" | |
done = False | |
while not done: | |
response, data = dataset.job_status(job_uri) | |
if response.status in range(200,300): | |
if data['status'] == 'scheduled': | |
print "Reset has not started yet" | |
elif data['status'] == 'running': | |
print "Reset is in progress" | |
elif data['status'] == 'failed': | |
print "Reset has failed :(" | |
done = True | |
elif data['status'] == 'succeeded': | |
print "Reset has completed :)" | |
done = True | |
if not done: | |
time.sleep(5) |
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
require 'rubygems' | |
require 'kasabi' | |
require 'json' | |
#Demonstration api key | |
APIKEY = "FIXME" | |
#Create a client for the NASA Search API | |
client = Kasabi::Search::Client.new("http://api.kasabi.com/api/search-api-nasa", { :apikey => APIKEY } ) | |
#Search for "Apollo", limiting to 5 results | |
results = client.search("apollo", { :max => 5 }) | |
#dump the data to the console | |
#Results are an RSS 1.0 feed | |
puts JSON.pretty_generate(results) | |
#Search for Spacecraft resource that match a search for "Apollo" | |
results = client.search("apollo type:\"http\\://purl.org/net/schemas/space/Spacecraft\"", { :max => 1 }) | |
puts JSON.pretty_generate(results) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment