Last active
March 29, 2016 14:56
-
-
Save otykhonruk/273bdf16ace09bb619af to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
from collections import namedtuple | |
from xml.etree import ElementTree | |
""" Client for http://gismeteo.ru """ | |
# Sample forecast section: | |
# | |
# <FORECAST day="29" month="03" year="2016" hour="03" tod="0" predict="0" weekday="3"> | |
# <PHENOMENA cloudiness="0" precipitation="10" rpower="0" spower="0"/> | |
# <PRESSURE max="745" min="743"/> | |
# <TEMPERATURE max="-2" min="0"/> | |
# <WIND min="1" max="3" direction="0"/> | |
# <RELWET max="94" min="92"/> | |
# <HEAT min="-2" max="0"/> | |
# </FORECAST> | |
MinMax = namedtuple('MinMax', 'min, max') | |
FIELDS = { | |
'PHENOMENA': namedtuple('Phenomena', 'cloudiness,precipitation,spower,rpower'), | |
'PRESSURE': MinMax, | |
'TEMPERATURE': MinMax, | |
'WIND': namedtuple('Wind', 'direction,min,max'), | |
'RELWET': MinMax, | |
'HEAT': MinMax, | |
} | |
names = [k.lower() for k in FIELDS.keys()] + ['day', 'month', 'year', 'hour', 'tod', 'predict', 'weekday'] | |
Forecast = namedtuple('Forecast', names) | |
def _int_values(d): | |
return {k: int(v) for k, v in d.items()} | |
def forecasts(locality_id): | |
url = 'http://informer.gismeteo.ru/xml/{}.xml'.format(locality_id) | |
res = requests.get(url, headers={'User-Agent': 'Mozilla'}) | |
root = ElementTree.fromstring(res.text) | |
for fc in root.findall('REPORT/TOWN/FORECAST'): | |
args = _int_values(fc.attrib) | |
for e in fc: | |
attr_data = FIELDS[e.tag](**_int_values(e.attrib)) | |
args[e.tag.lower()] = attr_data | |
yield Forecast(**args) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment