This notebook outlines how to capture a list of available datasets using the Datahub API. The output here is quite simple, but the avaialble metadata keys are displayed and output can be tailored to include additional attributes as needed.
API documentation is available at http://docs.planetos.com. If you have questions or comments, join our Slack community to chat with our development team.
For general information on usage of IPython/Jupyter and Matplotlib, please refer to their corresponding documentation. https://ipython.org/ and http://matplotlib.org/
In [1]:
from urllib.request import urlopen, Request
from urllib.parse import urlencode
import simplejson as json
Important! You'll need to replace apikey
below with your actual Planet OS API key, which you'll find on the Planet OS account settings page.
In [26]:
apikey = open('APIKEY').readlines()[0].strip() #'<YOUR API KEY HERE>'
url = "http://api.planetos.com/v1/datasets?apikey=%s" % apikey
print(url)
request = Request(url)
response = urlopen(request)
dataset_ids = json.loads(response.read())
In [27]:
# inspect the available keys and metadata structure from a single dataset
api_endpoint = "http://api.planetos.com/v1/datasets/"
query_dict = {'apikey': apikey}
query = urlencode(query_dict)
datasets = []
for id in dataset_ids:
api_query = "%s%s?%s" % (api_endpoint, id, query)
request = Request(api_query)
response = urlopen(request)
response_json = json.loads(response.read())
datasets.append(response_json)
datasets[0].keys()
Out[27]:
In [ ]:
In [28]:
# Alphabetically sort by dataset title
alpha_datasets = sorted(datasets, key=lambda k: k['Title'])
print("%s Datasets Available on Datahub\nhttp://data.planetos.com/\n" % len(datasets))
for ds in alpha_datasets:
datahub_url = "http://data.planetos.com/datasets/%s" % ds['Key']
# output Title, publisher, and Datahub url
print("(%s) %s\n%s\n" % (ds['Publisher'], ds['Title'], datahub_url))
# output Title, publisher, Datahub url and full abstract
# print("%s (%s)\n%s\n--\n%s\n" % (ds['Publisher'], ds['Title'], datahub_url, ds['Abstract']))
In [ ]: