fix search of cities
This commit is contained in:
parent
9cba3c63a0
commit
5e74da14f8
1 changed files with 25 additions and 46 deletions
|
|
@ -22,13 +22,14 @@ from __future__ import with_statement
|
||||||
|
|
||||||
import urllib2
|
import urllib2
|
||||||
from xml.dom import minidom
|
from xml.dom import minidom
|
||||||
|
from dateutil.parser import parse as parse_dt
|
||||||
|
|
||||||
# TODO store datetime objects instead of strings
|
# TODO store datetime objects instead of strings
|
||||||
# from datetime import datetime
|
# from datetime import datetime
|
||||||
|
|
||||||
from weboob.capabilities.weather import ICapWeather, CityNotFound, Current, Forecast, City
|
from weboob.capabilities.weather import ICapWeather, CityNotFound, Current, Forecast, City
|
||||||
from weboob.tools.backend import BaseBackend
|
from weboob.tools.backend import BaseBackend
|
||||||
from weboob.tools.browser import BaseBrowser
|
from weboob.tools.browser import StandardBrowser
|
||||||
|
|
||||||
|
|
||||||
__all__ = ['YahooBackend']
|
__all__ = ['YahooBackend']
|
||||||
|
|
@ -41,50 +42,27 @@ class YahooBackend(BaseBackend, ICapWeather):
|
||||||
VERSION = '0.d'
|
VERSION = '0.d'
|
||||||
DESCRIPTION = 'Yahoo!'
|
DESCRIPTION = 'Yahoo!'
|
||||||
LICENSE = 'AGPLv3+'
|
LICENSE = 'AGPLv3+'
|
||||||
BROWSER = BaseBrowser
|
BROWSER = StandardBrowser
|
||||||
WEATHER_URL = 'http://weather.yahooapis.com/forecastrss?w=%s&u=%s'
|
WEATHER_URL = 'http://weather.yahooapis.com/forecastrss?w=%s&u=%s'
|
||||||
SEARCH_URL = 'http://fr.meteo.yahoo.com/search/weather?p=%s'
|
|
||||||
|
|
||||||
def create_default_browser(self):
|
def create_default_browser(self):
|
||||||
return self.create_browser()
|
return self.create_browser(parser='json')
|
||||||
|
|
||||||
def iter_city_search(self, pattern):
|
def iter_city_search(self, pattern):
|
||||||
# minidom doesn't seem to work with that page
|
args = {'q': 'select line1, line2, line3, line4, city, uzip, statecode, countrycode, latitude, longitude, '
|
||||||
|
'country, woeid, quality, house, street, state from locdrop.placefinder '
|
||||||
|
'where text="%s" and locale="fr-FR" and gflags="f"' % pattern.encode('utf-8'),
|
||||||
|
'format': 'json',
|
||||||
|
}
|
||||||
|
doc = self.browser.location(self.browser.buildurl('http://locdrop.query.yahoo.com/v1/public/yql', **args))
|
||||||
|
|
||||||
#handler = urllib2.urlopen((self.SEARCH_URL % pattern).replace(' ','+'))
|
cities = doc['query']['results']['Result']
|
||||||
#dom = minidom.parse(handler)
|
if not isinstance(cities, (tuple,list)):
|
||||||
#handler.close()
|
cities = [cities]
|
||||||
#results = dom.getElementById('search-results')
|
|
||||||
#for no in results.childNodes:
|
|
||||||
# print no.nodeValue
|
|
||||||
|
|
||||||
# so i use a basic but efficient parsing
|
for result in cities:
|
||||||
with self.browser:
|
c = City(result['woeid'], u'%s, %s, %s' % (result['city'], result['state'], result['country']))
|
||||||
content = self.browser.readurl((self.SEARCH_URL % pattern.encode('utf-8')).replace(' ','+'))
|
yield c
|
||||||
|
|
||||||
page=''
|
|
||||||
for line in content.split('\n'):
|
|
||||||
if "<title>" in line and "Prévisions et Temps" in line:
|
|
||||||
page="direct"
|
|
||||||
elif "<title>" in line and "Résultats de la recherche" in line:
|
|
||||||
page="resultats"
|
|
||||||
|
|
||||||
if page == "resultats":
|
|
||||||
if '/redirwoei/' in line:
|
|
||||||
cities = line.split('/redirwoei/')
|
|
||||||
for c in cities:
|
|
||||||
if "strong" in c:
|
|
||||||
cid = c.split("'")[0]
|
|
||||||
cname = c.split("'")[1].replace("><strong>","").replace("</strong>","").split("</a>")[0]
|
|
||||||
yield City(cid, cname.decode('utf-8'))
|
|
||||||
elif page == "direct":
|
|
||||||
if 'div id="yw-breadcrumb"' in line:
|
|
||||||
l = line.split('</a>')
|
|
||||||
region = l[2].split('>')[-1]
|
|
||||||
country = l[1].split('>')[-1]
|
|
||||||
city = l[3].split('</li>')[1].replace('<li>','')
|
|
||||||
cid = line.split("/?unit")[0].split('-')[-1]
|
|
||||||
yield City(cid, (city+", "+region+", "+country).decode('utf-8'))
|
|
||||||
|
|
||||||
def _get_weather_dom(self, city_id):
|
def _get_weather_dom(self, city_id):
|
||||||
handler = urllib2.urlopen(self.WEATHER_URL % (city_id, 'c'))
|
handler = urllib2.urlopen(self.WEATHER_URL % (city_id, 'c'))
|
||||||
|
|
@ -98,14 +76,15 @@ class YahooBackend(BaseBackend, ICapWeather):
|
||||||
def get_current(self, city_id):
|
def get_current(self, city_id):
|
||||||
dom = self._get_weather_dom(city_id)
|
dom = self._get_weather_dom(city_id)
|
||||||
current = dom.getElementsByTagName('yweather:condition')[0]
|
current = dom.getElementsByTagName('yweather:condition')[0]
|
||||||
return Current(current.getAttribute('date'), int(current.getAttribute('temp')), current.getAttribute('text'), 'C')
|
return Current(parse_dt(current.getAttribute('date')),
|
||||||
|
float(current.getAttribute('temp')), unicode(current.getAttribute('text')), u'C')
|
||||||
|
|
||||||
def iter_forecast(self, city_id):
|
def iter_forecast(self, city_id):
|
||||||
dom = self._get_weather_dom(city_id)
|
dom = self._get_weather_dom(city_id)
|
||||||
for forecast in dom.getElementsByTagName('yweather:forecast'):
|
for forecast in dom.getElementsByTagName('yweather:forecast'):
|
||||||
yield Forecast(forecast.getAttribute('date'),
|
yield Forecast(parse_dt(forecast.getAttribute('date')),
|
||||||
int(forecast.getAttribute('low')),
|
float(forecast.getAttribute('low')),
|
||||||
int(forecast.getAttribute('high')),
|
float(forecast.getAttribute('high')),
|
||||||
forecast.getAttribute('text'),
|
unicode(forecast.getAttribute('text')),
|
||||||
'C',
|
u'C',
|
||||||
)
|
)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue