-
Notifications
You must be signed in to change notification settings - Fork 0
/
weather.py
executable file
·37 lines (31 loc) · 1.23 KB
/
weather.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
#!/usr/bin/python
__author__ = 'mattiasd'
import xml.etree.ElementTree as ET
import urllib
def getWeather():
getWeatherUrl = "http://www.yr.no/place/Sweden/Stockholm/Stockholm/forecast.xml"
r = urllib.urlopen(getWeatherUrl)
tree = ET.parse(r)
#print tree
root = tree.getroot().tag
#print root
for timedata in tree.findall('./forecast/tabular/'):
dayWeather = []
weatherTimeData = timedata.attrib['from'].split('T')
weatherTimeDataLast = timedata.attrib['to'].split('T')
dayWeather.append(weatherTimeData[1] + " - " + weatherTimeDataLast[1])
for weatherdata in timedata.getchildren():
if weatherdata.attrib.has_key('name'):
if weatherdata.tag == "symbol":
dayWeather.append ("General Forecast: " + weatherdata.attrib['name'])
else:
dayWeather.append (weatherdata.tag + ": " + weatherdata.attrib['name'])
elif weatherdata.tag == "temperature":
dayWeather.append (weatherdata.tag + ": " + weatherdata.attrib['value'] + "C")
return dayWeather
if __name__ == "__main__":
weather = getWeather()
i = 0
while i < len(weather):
print weather[i]
i = i + 1