-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpivotal-light.py
62 lines (51 loc) · 1.65 KB
/
pivotal-light.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#!/usr/bin/env python
import os
import json
import requests
from flask import Flask
import redis
app = Flask(__name__)
#read in redis details then use for logging into redis instance
VCAP_SERVICES = json.loads(os.environ['VCAP_SERVICES'])
CREDENTIALS = VCAP_SERVICES["rediscloud"][0]["credentials"]
r = redis.Redis(host=CREDENTIALS["hostname"], port=CREDENTIALS["port"], password=CREDENTIALS["password"])
#read from redis and populate varible to display
bright = r.get('bdata')
#API call to Wunderground to get wether details for Melbourne
url = "http://api.wunderground.com/api/459098590d3c97e4/conditions/q/AU/melbourne.json"
response = requests.get(url)
#Parse the JSON reponse
parsed = json.loads(response.content)
#Populate varibles with parsed JSON
weather = parsed["current_observation"]["weather"]
temp = parsed["current_observation"]["temp_c"]
hum = parsed["current_observation"]["relative_humidity"]
@app.route('/')
def mainmenu():
#HMTL for the brightness section
bright_html = """
<html>
<body>
<center><h1>The brightness at your location is:</br>
{}</br>
</center>
</body>
</html>
""".format(bright)
#HTML for the weather section
weather_html = """
<html>
<body>
</br>
<centre><h1>and the weather in Melbourne is: {}</br>
The temp is: {} degress celsius</br>
And the humidity is: {}</br>
</centre>
</body>
</html>
""".format(weather, temp, hum)
#combine both HTML sections and return the response
final_html = bright_html + weather_html
return final_html
if __name__ == "__main__":
app.run(debug=False,host='0.0.0.0', port=int(os.getenv('PORT', '5000')))