support repositories to manage backends (closes #747)

This commit is contained in:
Romain Bignon 2012-01-03 12:10:21 +01:00
commit 14a7a1d362
410 changed files with 1079 additions and 297 deletions

View file

@ -0,0 +1,23 @@
# -*- coding: utf-8 -*-
# Copyright(C) 2010-2011 Romain Bignon
#
# This file is part of weboob.
#
# weboob is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# weboob is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with weboob. If not, see <http://www.gnu.org/licenses/>.
from .backend import MeteofranceBackend
__all__ = ['MeteofranceBackend']

View file

@ -0,0 +1,49 @@
# -*- coding: utf-8 -*-
# Copyright(C) 2010-2011 Cedric Defortis
#
# This file is part of weboob.
#
# weboob is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# weboob is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with weboob. If not, see <http://www.gnu.org/licenses/>.
from weboob.capabilities.weather import ICapWeather
from weboob.tools.backend import BaseBackend
from .browser import MeteofranceBrowser
__all__ = ['MeteofranceBackend']
class MeteofranceBackend(BaseBackend, ICapWeather):
NAME = 'meteofrance'
MAINTAINER = 'Cedric Defortis'
EMAIL = 'cedric@aiur.fr'
VERSION = '0.a'
DESCRIPTION = 'Get forecasts from the MeteoFrance website'
LICENSE = 'AGPLv3+'
BROWSER = MeteofranceBrowser
def create_default_browser(self):
return self.create_browser()
def get_current(self, city_id):
return self.browser.get_current(city_id)
def iter_forecast(self, city_id):
return self.browser.iter_forecast(city_id)
def iter_city_search(self, pattern):
return self.browser.iter_city_search(pattern)

View file

@ -0,0 +1,71 @@
# -*- coding: utf-8 -*-
# Copyright(C) 2010-2011 Cedric Defortis
#
# This file is part of weboob.
#
# weboob is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# weboob is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with weboob. If not, see <http://www.gnu.org/licenses/>.
import urllib
from weboob.tools.browser import BaseBrowser
from .pages.meteo import WeatherPage, CityPage
__all__ = ['MeteofranceBrowser']
class MeteofranceBrowser(BaseBrowser):
DOMAIN = 'france.meteofrance.com'
PROTOCOL = 'http'
ENCODING = 'utf-8'
USER_AGENT = BaseBrowser.USER_AGENTS['wget']
WEATHER_URL = '{0}://{1}/france/meteo?PREVISIONS_PORTLET.path=previsionsville/{{cityid}}'.format(PROTOCOL, DOMAIN)
CITY_SEARCH_URL = '{0}://{1}/france/accueil/resultat?RECHERCHE_RESULTAT_PORTLET.path=rechercheresultat&' \
'query={{city_pattern}}&type=PREV_FRANCE&satellite=france'.format(PROTOCOL, DOMAIN)
PAGES = {
WEATHER_URL.format(cityid=".*"): WeatherPage,
CITY_SEARCH_URL.format(city_pattern=".*"): CityPage,
'http://france.meteofrance.com/france/accueil/resultat.*': CityPage,
'http://france.meteofrance.com/france/meteo.*': WeatherPage,
}
def __init__(self, *args, **kwargs):
BaseBrowser.__init__(self, *args, **kwargs)
def iter_city_search(self, pattern):
searchurl = self.CITY_SEARCH_URL.format(city_pattern=urllib.quote_plus(pattern.encode('utf-8')))
self.location(searchurl)
if self.is_on_page(CityPage):
# Case 1: there are multiple results for the pattern:
return self.page.iter_city_search()
else:
# Case 2: there is only one result, and the website send directly
# the browser on the forecast page:
return [self.page.get_city()]
def iter_forecast(self, city_id):
self.location(self.WEATHER_URL.format(cityid=city_id))
assert self.is_on_page(WeatherPage)
return self.page.iter_forecast()
def get_current(self, city_id):
self.location(self.WEATHER_URL.format(cityid=city_id))
assert self.is_on_page(WeatherPage)
return self.page.get_current()

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.5 KiB

View file

@ -0,0 +1,18 @@
# -*- coding: utf-8 -*-
# Copyright(C) 2010-2011 Romain Bignon
#
# This file is part of weboob.
#
# weboob is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# weboob is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with weboob. If not, see <http://www.gnu.org/licenses/>.

View file

@ -0,0 +1,88 @@
# -*- coding: utf-8 -*-
# Copyright(C) 2010-2011 Cedric Defortis
#
# This file is part of weboob.
#
# weboob is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# weboob is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with weboob. If not, see <http://www.gnu.org/licenses/>.
from weboob.tools.browser import BasePage
from weboob.capabilities.weather import Forecast, Current, City
import datetime
__all__ = ['WeatherPage', 'CityPage']
class WeatherPage(BasePage):
def get_temp_without_unit(self, temp_str):
# It seems that the mechanize module give us some old style
# ISO character
return int(temp_str.replace(u"\xb0C", "").strip())
def iter_forecast(self):
for div in self.document.getiterator('li'):
if div.attrib.get('class', '').startswith('jour'):
mdate = div.xpath('./dl/dt')[0].text
t_low = self.get_temp_without_unit(div.xpath('.//dd[@class="minmax"]/strong')[0].text)
t_high = self.get_temp_without_unit(div.xpath('.//dd[@class="minmax"]/strong')[1].text)
mtxt = div.xpath('.//dd')[0].text
yield Forecast(mdate, t_low, t_high, mtxt, 'C')
elif div.attrib.get('class', '').startswith('lijourle'):
for em in div.getiterator('em'):
templist = em.text_content().split("/")
t_low = self.get_temp_without_unit(templist[0])
t_high = self.get_temp_without_unit(templist[1])
break
for strong in div.getiterator("strong"):
mdate = strong.text_content()
break
for img in div.getiterator("img"):
mtxt = img.attrib["title"]
break
yield Forecast(mdate, t_low, t_high, mtxt, "C")
def get_current(self):
div = self.document.getroot().xpath('//div[@class="bloc_details"]/ul/li/dl')[0]
mdate = datetime.datetime.now()
temp = self.get_temp_without_unit(div.xpath('./dd[@class="minmax"]')[0].text)
mtxt = div.find('dd').find('img').attrib['title']
return Current(mdate, temp, mtxt, 'C')
def get_city(self):
"""
Return the city from the forecastpage.
"""
for div in self.document.getiterator('div'):
if div.attrib.has_key("class") and div.attrib.get("class") == "choix":
for strong in div.getiterator("strong"):
city_name=strong.text +" "+ strong.tail.replace("(","").replace(")","")
city_id=self.url.split("/")[-1]
return City(city_id, city_name)
class CityPage(BasePage):
def iter_city_search(self):
for div in self.document.getiterator('div'):
if div.attrib.get('id') == "column1":
for li in div.getiterator('li'):
city_name = li.text_content()
for children in li.getchildren():
city_id = children.attrib.get("href").split("/")[-1]
mcity = City( city_id, city_name)
yield mcity

View file

@ -0,0 +1,35 @@
# -*- coding: utf-8 -*-
# Copyright(C) 2010-2011 Romain Bignon
#
# This file is part of weboob.
#
# weboob is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# weboob is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with weboob. If not, see <http://www.gnu.org/licenses/>.
from weboob.tools.test import BackendTest
class MeteoFranceTest(BackendTest):
BACKEND = 'meteofrance'
def test_meteofrance(self):
l = list(self.backend.iter_city_search('paris'))
self.assertTrue(len(l) > 0)
city = l[0]
current = self.backend.get_current(city.id)
self.assertTrue(current.temp > -20 and current.temp < 50)
forecasts = list(self.backend.iter_forecast(city.id))
self.assertTrue(len(forecasts) > 0)