import json import urllib2 import httplib2 xivelyhost = 'api.xively.com' # this is the base URL for Xively's API feeds feedurl = '/v2/feeds/1703619548' # this is the part of the URL that includes your feed ID number # we tack this chunk together with the xivelyhost URL url = "http://api.xively.com/v2/feeds/1703619548" #feedid = '170361954' # we aren't going to use this directly... but later we just might apikey = 'EsTjqWQlHyj69kBKBYbtoanldlKxAb7HalVxzW5ef2V5x296' # Your Read-Only Key! # # Example 1: Pulling JSON Feed from my read-only APIKey URL # headers = {"Content-type": "application/x-www-form-urlencoded", "Accept": "application/json", "X-ApiKey": apikey} # In order to authenticate with Xively we have to preload some stuff in the HTTP packet -- namely headers # Our request is the same type of request a server expects from, say, a web form -- more on this soon http = httplib2.Http() response, content = http.request(url, 'GET', headers=headers) # Simple -- we get back whatever data the web server is going to give us! print(response.status, response.reason) # Part of the info we get back is response / status... you may know this as something like HTTP 404 ;-) # Really we just do squat with this information -- for now #print(content) eggdata = json.loads(content) # We use some magic to turn JSON text into a dictionary # You shouldn't leave this as total magic, though -- print out the variable 'response' and see what's in it! print("Example 1") #print(eggdata) #or datastream in eggdata['datastreams']: # print("{") # for key in datastream: # print key,datastream[key] # print("}") datastream_keys = ('CO','Dust','NO2','Temperature','Humidity') eggdict = {datastream['id'] : datastream['current_value'] for datastream in eggdata['datastreams'] if (datastream['id'] in datastream_keys)} print(eggdict)