-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclimateclock.py
95 lines (64 loc) · 2.36 KB
/
climateclock.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import ui
import time
import webbrowser
import requests
import json
from datetime import datetime
from dateutil import parser
def buttonaction(sender):
#Extracting data from api to display
r = requests.get('https://api.climateclock.world/v1/clock', auth = ('user','pass'))
#using json formatter to parse every json element
data = r.json()
#extract deadline
carbonDeadline = data["data"]["modules"]["carbon_deadline_1"]["timestamp"]
carbonDeadline = parser.parse(carbonDeadline)
#extract green climate funds
fund = data["data"]["modules"]["green_climate_fund_1"]["initial"]
#extract indigenous land area
land = int(data["data"]["modules"]["indigenous_land_1"]["initial"] * 1000000)
#extract renewable percent
percent = data["data"]["modules"]["renewables_1"]["initial"]
dt = datetime.now()
dt = str(dt.date())
#extracting newsfeed for display
newsfeed_data = data["data"]["modules"]["newsfeed_1"]["newsfeed"]
news = ''
for idx in range(len(newsfeed_data)):
#print(newsfeed_data[idx]['headline_original'])
news = news + newsfeed_data[idx]['headline_original']
newsfeed_data = str(newsfeed_data)
tz_info = carbonDeadline.tzinfo
#for idx in range(len(newsfeed_data)):
# print(newsfeed_data[idx]['headline_original'])
# Now we can subtract two variables using the same time zone info
# For instance
# Lets obtain the Now() datetime but for the tz_info we got before
diff = carbonDeadline - datetime.now(tz_info)
#print(diff)
days = diff.days
hours = days * 24
minutes = hours * 60
seconds = minutes * 60
years = days // 365
# Calculating months
months = (days - years *365) // 30
# Calculating days
days = (days - years * 365 - months*30)-2
remain = str(years)+' Years '+str(months)+' Months '+str(days)+' Days'
remain2 = str(hours)+' Hours '+str(minutes)+' Minutes '+str(seconds)+' Seconds'
remain3 = str(fund)+' Billion USD'
remain4 = str(land)+' km sq'
remain5 = str(percent)+' %'
v["label5"].text = dt
v["textview1"].text = remain
v["textview2"].text = remain2
v["label6"].text = remain3
v["label7"].text = remain4
v["label8"].text = remain5
v["label10"].text = news
v = ui.load_view()
button = ui.Button(title='WELCOME TO CLIMATE CLOCK', font=('<System>', 24), flex='rwh', action = buttonaction)
button.frame = (300, 0, 450, 110)
v.add_subview(button)
v.present('sheet')