backend.py 2.37 KB
Newer Older
1 2
# -*- coding: utf-8 -*-

3 4 5 6 7 8 9 10 11 12 13 14 15 16
# Copyright(C) 2010  Romain Bignon
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, version 3 of the License.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 18 19 20


import urllib2
from xml.dom import minidom
21
import os
22 23

# TODO store datetime objects instead of strings
24
# from datetime import datetime
25

26
from weboob.capabilities.weather import ICapWeather, CityNotFound, Current, Forecast
Christophe Benz's avatar
Christophe Benz committed
27
from weboob.tools.backend import BaseBackend
28

Christophe Benz's avatar
Christophe Benz committed
29 30 31 32

__all__ = ['YWeatherBackend']


33
class YWeatherBackend(BaseBackend, ICapWeather):
34 35 36
    NAME = 'yweather'
    MAINTAINER = 'Romain Bignon'
    EMAIL = 'romain@peerfuse.org'
37
    VERSION = '0.1'
38 39
    DESCRIPTION = 'Yahoo Weather'
    LICENSE = 'GPLv3'
40
    ICON = os.path.join(os.path.dirname(__file__), 'data/logo.png')
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

    URL = 'http://weather.yahooapis.com/forecastrss?w=%s&u=%s'

    def iter_city_search(self, pattern):
        raise NotImplementedError()

    def _get_dom(self, city_id):
        handler = urllib2.urlopen(self.URL % (city_id, 'c'))
        dom = minidom.parse(handler)
        handler.close()
        if not dom.getElementsByTagName('yweather:condition'):
            raise CityNotFound()

        return dom

    def get_current(self, city_id):
        dom = self._get_dom(city_id)
        current = dom.getElementsByTagName('yweather:condition')[0]
        return Current(current.getAttribute('date'), int(current.getAttribute('temp')), current.getAttribute('text'), 'C')

    def iter_forecast(self, city_id):
        dom = self._get_dom(city_id)
        for forecast in dom.getElementsByTagName('yweather:forecast'):
            yield Forecast(forecast.getAttribute('date'),
                           int(forecast.getAttribute('low')),
                           int(forecast.getAttribute('high')),
                           forecast.getAttribute('text'),
Christophe Benz's avatar
Christophe Benz committed
68 69
                           'C',
                           )