Skip to content

Instantly share code, notes, and snippets.

@dbalan
Last active January 12, 2016 09:01
Show Gist options
  • Save dbalan/25e7d709611f57721b15 to your computer and use it in GitHub Desktop.
Save dbalan/25e7d709611f57721b15 to your computer and use it in GitHub Desktop.
Gets local weather values from bangalore open weather project.
#!/usr/local/bin/python
# -*- coding: utf-8 -*-
# this uses bangalore open weather api to query the temparature near you.
# the api is documented here: http://www.yuktix.com/m/aws/media/yuktix-public-api.pdf
# This is a standalone script, eventhough I run this as a https://github.com/matryer/bitbar plugin.
API_ENDPOINT = "http://api1.yuktix.com:8080/sensordb/v1"
API_KEY = ""
SERIAL_NUMBER = "" # serial number of the weather station you are interested in.
CACHE_FILE="/tmp/weather_cache" # store recent data and display it while offline.
import json
import requests
import sys
import pickle
url = "{}/module/device/archive/latest".format(API_ENDPOINT)
data = {'map': {
"module": "AWS",
"serialNumber": SERIAL_NUMBER,
"limit": 1,}}
headers = {"Content-type": "application/json",
"Authorization": "Signature={}".format(API_KEY)}
try:
r = requests.post(url, data=json.dumps(data), headers=headers)
if r.status_code != 200:
print "failed: got status {}".format(r.status_code)
sys.exit(-1)
resp = r.json()["result"][0]
except:
with open(CACHE_FILE) as f:
resp = pickle.load(f)
pressure = float(resp["P"])/ 100
temp = float(resp["T"])
if temp < 17:
color="blue"
elif temp <20:
color="green"
elif temp < 22:
color="yellow"
else:
color="red"
with open(CACHE_FILE, "w") as f:
pickle.dump(resp, f)
print "Temp: {} C | color={}".format(temp, color)
print "---"
print "Hum: {} % | color=green".format(resp["RH"])
print "Pres: {} hPa".format(pressure)
print "Rain: {} mm".format(resp["Rain"])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment