Ec5a58db4acc016568e0293d283cbc2d

Is there a more efficient way to parse the Yahoo weather RSS feed ?

#!/usr/bin/python
# -*- coding: utf-8 -*-

from urllib import urlopen
from xml.dom.minidom import parse

WEATHER_URL = 'http://xml.weather.yahoo.com/forecastrss?p=%s&u=%s'
WEATHER_NS = 'http://xml.weather.yahoo.com/ns/rss/1.0'

ERROR_MSG = 'No weather feed'
weather_params = ('FRXX0076','c')

class YWeatherAPI:

    def __init__(self):
        self.options = ['location', 'astronomy', 'units', 'wind', 'atmosphere', 'condition', 'forecast']
    
    def query_api(self, params):
        try:
            return self.get_data(parse(urlopen(WEATHER_URL % params)))
        except:
            return ERROR_MSG
            
    def get_data(self, xml):
        data = {}
        for option in self.options:
            param_list = []
            for node in xml.getElementsByTagNameNS(WEATHER_NS, option):
                attrs = node.attributes.keys()
                options_dict = {}
                for attr in attrs :
                    value = node.getAttribute(attr)
                    options_dict[attr] = value
                param_list.append(options_dict)
            data[option]=param_list
        return data
        
y = YWeatherAPI()
print y.query_api(weather_params)

Refactorings

No refactoring yet !

E40ca6b36dbee9e693a16f32f1941acd

micheal d.

September 8, 2008, September 08, 2008 17:27, permalink

No rating. Login to rate!

Well, it depends wether you would like to have some speed increase or a serious speed increase. If first, use xml.sax, if second I advise using cElementTree. Then again for RSS I think sax would be perfect.

F635307583b9a832f1558add6a5153ed

Shmai

October 16, 2009, October 16, 2009 10:40, permalink

No rating. Login to rate!

This is classic asp script will allow you to monitor your current local weather via a Weather.com XML feed.
It parses the XML data and then outputs formatted HTML.

F635307583b9a832f1558add6a5153ed

shmai

October 28, 2009, October 28, 2009 12:47, permalink

No rating. Login to rate!

This is classic asp script will allow you to monitor your current local weather via a Weather.com XML feed.
It parses the XML data and then outputs formatted HTML.

Your refactoring





Format Copy from initial code

or Cancel