majorcornwallace

Finally a Clean HTTP Get for Xively Data

Jan 18th, 2014
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.97 KB | None | 0 0
  1. import json
  2. import urllib
  3. import httplib
  4.  
  5. xivelyhost = 'api.xively.com'
  6. feedurl = '/v2/feeds/1703619548'
  7. feedid = '170361954'
  8. apikey = 'EsTjqWQlHyj69kBKBYbtoanldlKxAb7HalVxzW5ef2V5x296' # Read-Only Key
  9.  
  10. #
  11. # Example 1: Pulling JSON Feed from my read-only APIKey URL
  12. #
  13.  
  14. #params = urllib.urlencode({'datastream': 'CO,NO2,Dust,Temperature,Humidity'))
  15. headers = {"Content-type": "application/x-www-form-urlencoded", "Accept": "application/json", "X-ApiKey": apikey}
  16. http = httplib.HTTPConnection(xivelyhost)
  17. http.request("GET", feedurl , '', headers)
  18. response = http.getresponse()
  19. print(response.status, response.reason)
  20.  
  21. eggdata = json.load(response)
  22.  
  23. http.close()
  24.  
  25.  
  26. print("Example 1")
  27. eggdata
  28.  
  29. #
  30. # get to the sensible parts of the data, quick
  31. #
  32. datastream_keys = ('CO','Dust','NO2','Temperature','Humidity')
  33. eggdict = {datastream['id'] : datastream['current_value'] for datastream in eggdata['datastreams'] if (datastream['id'] in datastream_keys)}
  34.  
  35. print eggdict
Advertisement
Add Comment
Please, Sign In to add comment