forked from guiguiabloc/api-domogeek
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClassWeather.py
75 lines (71 loc) · 2.83 KB
/
ClassWeather.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
#!/usr/bin/python
# -*- coding: utf-8 -*-
#
# Gruik coded by GuiguiAbloc
# http://blog.guiguiabloc.fr
# http://api.domogeek.fr
#
import urllib,urllib2
import json
class weather:
def todayopenweathermap(self, lat, lng, request):
url = "http://api.openweathermap.org/data/2.5/forecast/daily?lat=%s&lon=%s&units=metric&cnt=2" % (urllib.quote(str(lat)), urllib.quote(str(lng)))
try:
data = urllib.urlopen(url).read()
dataopenweathermap = json.loads(data)
temp = dataopenweathermap['list'][0]['temp']
pressure = dataopenweathermap['list'][0]['pressure']
humidity = dataopenweathermap['list'][0]['humidity']
weather = dataopenweathermap['list'][0]['weather']
windspeed = dataopenweathermap['list'][0]['speed']
if request == "temperature":
return temp
if request == "pressure" :
return pressure
if request == "humidity" :
return humidity
if request == "weather" :
return weather
if request == "windspeed":
return windspeed
if request == "all":
return dataopenweathermap['list'][0]
else:
return None
except:
return "no data"
def tomorrowopenweathermap(self, lat, lng, request):
url = "http://api.openweathermap.org/data/2.5/forecast/daily?lat=%s&lon=%s&units=metric&cnt=2" % (urllib.quote(str(lat)), urllib.quote(str(lng)))
try:
data = urllib.urlopen(url).read()
dataopenweathermap = json.loads(data)
temp = dataopenweathermap['list'][1]['temp']
pressure = dataopenweathermap['list'][1]['pressure']
humidity = dataopenweathermap['list'][1]['humidity']
weather = dataopenweathermap['list'][1]['weather']
windspeed = dataopenweathermap['list'][1]['speed']
if request == "temperature":
return temp
if request == "pressure" :
return pressure
if request == "humidity" :
return humidity
if request == "weather" :
return weather
if request == "windspeed":
return windspeed
if request == "all":
return dataopenweathermap['list'][1]
else:
return None
except:
return "no data"
def getrain(self, lat, lng, api_key, date):
url = "http://api.worldweatheronline.com/free/v1//weather.ashx?q=%s,%s&key=%s&format=json&date=%s&includeLocation=no" % (urllib.quote(str(lat)), urllib.quote(str(lng)), urllib.quote(api_key), urllib.quote(date))
try:
data = urllib.urlopen(url).read()
dataopenweathermap = json.loads(data)
rain = dataopenweathermap['data']['weather'][0]['precipMM']
return rain
except:
return "no data"