pycurltest.py 1.3 KB
Newer Older
1
2
3
4
import pycurl
import json
from StringIO import StringIO

5
6
7
8
9
10

#---- SETTINGS ----#
rain_limit = 1.0
cloud_limit = 50
#------------------#

11
12
13
14
15
16
17
18
19
buffer = StringIO()
c = pycurl.Curl()
c.setopt(c.URL, 'api.openweathermap.org/data/2.5/forecast?q=pisa,it&APPID=c5ebd88f4494c472f3032e3426b6f0c6')
c.setopt(c.WRITEDATA, buffer)
c.perform()
c.close()

body = buffer.getvalue()
parsed = json.loads(body)
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

cloud_perc_average = 0
rain_average = 0

print "Date\t\t\tDescription\tCloud %\tRain"

for i in range(len(parsed["list"])):
    print parsed["list"][i]["dt_txt"], "\t",  parsed["list"][i]["weather"][0]["description"], "\t",  parsed["list"][i]["clouds"]["all"],

    cloud_perc_average += parsed["list"][i]["clouds"]["all"]

    if len(parsed["list"][i]["rain"]) > 0:
        print "\t",  parsed["list"][i]["rain"]["3h"]
        rain_average += parsed["list"][i]["rain"]["3h"]
    else :
        print "\t0"

    if i % 8 == 7:
        cloud_perc_average /= 8
        rain_average /= 8
        if cloud_perc_average > cloud_limit:
            print "\x1b[37;1m",
        print "Average clouds in the last 24h: ", cloud_perc_average, "\x1b[0m"
        if rain_average > rain_limit:
            print "\x1b[34m",
        print "Average rain in the last 24h: ", rain_average, "\n", "\x1b[0m"

        rain_average = 0
        cloud_perc_average = 0

50
51