def __init__(self): ProbeParser.__init__(self) # the HTML parser info self.__html_parser = MadridDotOrgHTMLParser() # fill up the probe list # # madrid.org 02 Corredor del henares self.probe_list.append( Probe(u'Alcalá de Henares', MADRID_DOT_ORG_PROBE_ALCALA_URL, MADRID_DOT_ORG_PROBE_ALCALA_LAT, MADRID_DOT_ORG_PROBE_ALCALA_LON)) self.probe_list.append( Probe(u'Alcobendas', MADRID_DOT_ORG_PROBE_ALCOBENDAS_URL, MADRID_DOT_ORG_PROBE_ALCOBENDAS_LAT, MADRID_DOT_ORG_PROBE_ALCOBENDAS_LON)) self.probe_list.append( Probe(u'Torrejón de Ardoz', MADRID_DOT_ORG_PROBE_TORREJON_URL, MADRID_DOT_ORG_PROBE_TORREJON_LAT, MADRID_DOT_ORG_PROBE_TORREJON_LON)) self.probe_list.append( Probe(u'Coslada', MADRID_DOT_ORG_PROBE_COSLADA_URL, MADRID_DOT_ORG_PROBE_COSLADA_LAT, MADRID_DOT_ORG_PROBE_COSLADA_LON)) self.probe_list.append( Probe(u'Arganda del Rey', MADRID_DOT_ORG_PROBE_ARGANDA_URL, MADRID_DOT_ORG_PROBE_ARGANDA_LAT, MADRID_DOT_ORG_PROBE_ARGANDA_LON)) self.probe_list.append( Probe(u'Rivas Vaciamadrid', MADRID_DOT_ORG_PROBE_RIVAS_URL, MADRID_DOT_ORG_PROBE_RIVAS_LAT, MADRID_DOT_ORG_PROBE_RIVAS_LON)) self.probe_list.append( Probe(u'Algete', MADRID_DOT_ORG_PROBE_ALGETE_URL, MADRID_DOT_ORG_PROBE_ALGETE_LAT, MADRID_DOT_ORG_PROBE_ALGETE_LON)) # madrid.org 03 Urbana sur self.probe_list.append( Probe(u'Getafe', MADRID_DOT_ORG_PROBE_GETAFE_URL, MADRID_DOT_ORG_PROBE_GETAFE_LAT, MADRID_DOT_ORG_PROBE_GETAFE_LON)) self.probe_list.append( Probe(u'Leganés', MADRID_DOT_ORG_PROBE_LEGANES_URL, MADRID_DOT_ORG_PROBE_LEGANES_LAT, MADRID_DOT_ORG_PROBE_LEGANES_LON)) self.probe_list.append( Probe(u'Fuenlabrada', MADRID_DOT_ORG_PROBE_FUENLABRADA_URL, MADRID_DOT_ORG_PROBE_FUENLABRADA_LAT, MADRID_DOT_ORG_PROBE_FUENLABRADA_LON)) self.probe_list.append( Probe(u'Móstoles', MADRID_DOT_ORG_PROBE_MOSTOLES_URL, MADRID_DOT_ORG_PROBE_MOSTOLES_LAT, MADRID_DOT_ORG_PROBE_MOSTOLES_LON)) self.probe_list.append( Probe(u'Alcorcón', MADRID_DOT_ORG_PROBE_ALCORCON_URL, MADRID_DOT_ORG_PROBE_ALCORCON_LAT, MADRID_DOT_ORG_PROBE_ALCORCON_LON)) self.probe_list.append( Probe(u'Aranjuez', MADRID_DOT_ORG_PROBE_ARANJUEZ_URL, MADRID_DOT_ORG_PROBE_ARANJUEZ_LAT, MADRID_DOT_ORG_PROBE_ARANJUEZ_LON)) self.probe_list.append( Probe(u'Valdemoro', MADRID_DOT_ORG_PROBE_VALDEMORO_URL, MADRID_DOT_ORG_PROBE_VALDEMORO_LAT, MADRID_DOT_ORG_PROBE_VALDEMORO_LON)) # madrid.org 04 Urbana noroeste self.probe_list.append( Probe(u'Colmenar Viejo', MADRID_DOT_ORG_PROBE_COLMENAR_URL, MADRID_DOT_ORG_PROBE_COLMENAR_LAT, MADRID_DOT_ORG_PROBE_COLMENAR_LON)) self.probe_list.append( Probe(u'Majadahonda', MADRID_DOT_ORG_PROBE_MAJADAHONDA_URL, MADRID_DOT_ORG_PROBE_MAJADAHONDA_LAT, MADRID_DOT_ORG_PROBE_MAJADAHONDA_LON)) self.probe_list.append( Probe(u'Collado Villalba', MADRID_DOT_ORG_PROBE_COLLADO_URL, MADRID_DOT_ORG_PROBE_COLLADO_LAT, MADRID_DOT_ORG_PROBE_COLLADO_LON)) # madrid.org 05 Rural sierra norte self.probe_list.append( Probe(u'Guadalix de la Sierra', MADRID_DOT_ORG_PROBE_GUADALIX_URL, MADRID_DOT_ORG_PROBE_GUADALIX_LAT, MADRID_DOT_ORG_PROBE_GUADALIX_LON)) self.probe_list.append( Probe(u'El Atazar', MADRID_DOT_ORG_PROBE_ATAZAR_URL, MADRID_DOT_ORG_PROBE_ATAZAR_LAT, MADRID_DOT_ORG_PROBE_ATAZAR_LON)) # madrid.org 06 Cuenca del Alberche self.probe_list.append( Probe(u'San Martín de Valdeiglesias', MADRID_DOT_ORG_PROBE_SANMARTIN_URL, MADRID_DOT_ORG_PROBE_SANMARTIN_LAT, MADRID_DOT_ORG_PROBE_SANMARTIN_LON)) self.probe_list.append( Probe(u'Villa del Prado', MADRID_DOT_ORG_PROBE_ELPRADO_URL, MADRID_DOT_ORG_PROBE_ELPRADO_LAT, MADRID_DOT_ORG_PROBE_ELPRADO_LON)) # madrid.org 07 Cuenca del tajuna self.probe_list.append( Probe(u'Villarejo de Salvanés', MADRID_DOT_ORG_PROBE_VILLAREJO_URL, MADRID_DOT_ORG_PROBE_VILLAREJO_LAT, MADRID_DOT_ORG_PROBE_VILLAREJO_LON)) self.probe_list.append( Probe(u'Orusco de Tajuña', MADRID_DOT_ORG_PROBE_ORUSCO_URL, MADRID_DOT_ORG_PROBE_ORUSCO_LAT, MADRID_DOT_ORG_PROBE_ORUSCO_LON))
class MadridDotOrgLiveProbeParser(ProbeParser): """ Parser to retrieve probe data from madrid.org Each probe has a specific data URL which will be used to download the HTML content and parse the live probe feed """ def __init__(self): ProbeParser.__init__(self) # the HTML parser info self.__html_parser = MadridDotOrgHTMLParser() # fill up the probe list # # madrid.org 02 Corredor del henares self.probe_list.append( Probe(u'Alcalá de Henares', MADRID_DOT_ORG_PROBE_ALCALA_URL, MADRID_DOT_ORG_PROBE_ALCALA_LAT, MADRID_DOT_ORG_PROBE_ALCALA_LON)) self.probe_list.append( Probe(u'Alcobendas', MADRID_DOT_ORG_PROBE_ALCOBENDAS_URL, MADRID_DOT_ORG_PROBE_ALCOBENDAS_LAT, MADRID_DOT_ORG_PROBE_ALCOBENDAS_LON)) self.probe_list.append( Probe(u'Torrejón de Ardoz', MADRID_DOT_ORG_PROBE_TORREJON_URL, MADRID_DOT_ORG_PROBE_TORREJON_LAT, MADRID_DOT_ORG_PROBE_TORREJON_LON)) self.probe_list.append( Probe(u'Coslada', MADRID_DOT_ORG_PROBE_COSLADA_URL, MADRID_DOT_ORG_PROBE_COSLADA_LAT, MADRID_DOT_ORG_PROBE_COSLADA_LON)) self.probe_list.append( Probe(u'Arganda del Rey', MADRID_DOT_ORG_PROBE_ARGANDA_URL, MADRID_DOT_ORG_PROBE_ARGANDA_LAT, MADRID_DOT_ORG_PROBE_ARGANDA_LON)) self.probe_list.append( Probe(u'Rivas Vaciamadrid', MADRID_DOT_ORG_PROBE_RIVAS_URL, MADRID_DOT_ORG_PROBE_RIVAS_LAT, MADRID_DOT_ORG_PROBE_RIVAS_LON)) self.probe_list.append( Probe(u'Algete', MADRID_DOT_ORG_PROBE_ALGETE_URL, MADRID_DOT_ORG_PROBE_ALGETE_LAT, MADRID_DOT_ORG_PROBE_ALGETE_LON)) # madrid.org 03 Urbana sur self.probe_list.append( Probe(u'Getafe', MADRID_DOT_ORG_PROBE_GETAFE_URL, MADRID_DOT_ORG_PROBE_GETAFE_LAT, MADRID_DOT_ORG_PROBE_GETAFE_LON)) self.probe_list.append( Probe(u'Leganés', MADRID_DOT_ORG_PROBE_LEGANES_URL, MADRID_DOT_ORG_PROBE_LEGANES_LAT, MADRID_DOT_ORG_PROBE_LEGANES_LON)) self.probe_list.append( Probe(u'Fuenlabrada', MADRID_DOT_ORG_PROBE_FUENLABRADA_URL, MADRID_DOT_ORG_PROBE_FUENLABRADA_LAT, MADRID_DOT_ORG_PROBE_FUENLABRADA_LON)) self.probe_list.append( Probe(u'Móstoles', MADRID_DOT_ORG_PROBE_MOSTOLES_URL, MADRID_DOT_ORG_PROBE_MOSTOLES_LAT, MADRID_DOT_ORG_PROBE_MOSTOLES_LON)) self.probe_list.append( Probe(u'Alcorcón', MADRID_DOT_ORG_PROBE_ALCORCON_URL, MADRID_DOT_ORG_PROBE_ALCORCON_LAT, MADRID_DOT_ORG_PROBE_ALCORCON_LON)) self.probe_list.append( Probe(u'Aranjuez', MADRID_DOT_ORG_PROBE_ARANJUEZ_URL, MADRID_DOT_ORG_PROBE_ARANJUEZ_LAT, MADRID_DOT_ORG_PROBE_ARANJUEZ_LON)) self.probe_list.append( Probe(u'Valdemoro', MADRID_DOT_ORG_PROBE_VALDEMORO_URL, MADRID_DOT_ORG_PROBE_VALDEMORO_LAT, MADRID_DOT_ORG_PROBE_VALDEMORO_LON)) # madrid.org 04 Urbana noroeste self.probe_list.append( Probe(u'Colmenar Viejo', MADRID_DOT_ORG_PROBE_COLMENAR_URL, MADRID_DOT_ORG_PROBE_COLMENAR_LAT, MADRID_DOT_ORG_PROBE_COLMENAR_LON)) self.probe_list.append( Probe(u'Majadahonda', MADRID_DOT_ORG_PROBE_MAJADAHONDA_URL, MADRID_DOT_ORG_PROBE_MAJADAHONDA_LAT, MADRID_DOT_ORG_PROBE_MAJADAHONDA_LON)) self.probe_list.append( Probe(u'Collado Villalba', MADRID_DOT_ORG_PROBE_COLLADO_URL, MADRID_DOT_ORG_PROBE_COLLADO_LAT, MADRID_DOT_ORG_PROBE_COLLADO_LON)) # madrid.org 05 Rural sierra norte self.probe_list.append( Probe(u'Guadalix de la Sierra', MADRID_DOT_ORG_PROBE_GUADALIX_URL, MADRID_DOT_ORG_PROBE_GUADALIX_LAT, MADRID_DOT_ORG_PROBE_GUADALIX_LON)) self.probe_list.append( Probe(u'El Atazar', MADRID_DOT_ORG_PROBE_ATAZAR_URL, MADRID_DOT_ORG_PROBE_ATAZAR_LAT, MADRID_DOT_ORG_PROBE_ATAZAR_LON)) # madrid.org 06 Cuenca del Alberche self.probe_list.append( Probe(u'San Martín de Valdeiglesias', MADRID_DOT_ORG_PROBE_SANMARTIN_URL, MADRID_DOT_ORG_PROBE_SANMARTIN_LAT, MADRID_DOT_ORG_PROBE_SANMARTIN_LON)) self.probe_list.append( Probe(u'Villa del Prado', MADRID_DOT_ORG_PROBE_ELPRADO_URL, MADRID_DOT_ORG_PROBE_ELPRADO_LAT, MADRID_DOT_ORG_PROBE_ELPRADO_LON)) # madrid.org 07 Cuenca del tajuna self.probe_list.append( Probe(u'Villarejo de Salvanés', MADRID_DOT_ORG_PROBE_VILLAREJO_URL, MADRID_DOT_ORG_PROBE_VILLAREJO_LAT, MADRID_DOT_ORG_PROBE_VILLAREJO_LON)) self.probe_list.append( Probe(u'Orusco de Tajuña', MADRID_DOT_ORG_PROBE_ORUSCO_URL, MADRID_DOT_ORG_PROBE_ORUSCO_LAT, MADRID_DOT_ORG_PROBE_ORUSCO_LON)) def update(self): for thisProbe in self.probe_list: print "parsing madrid.org - " + thisProbe.name + "..." # ensure the parser is clean to start the parsing process self.__html_parser.reset() req = urllib2.Request(thisProbe.dataURL, data=None, headers=HTTP_HEADERS) htmlFile = urllib2.urlopen(req) # what character encoding set is this file???? charset = htmlFile.headers.getparam('charset') for line in htmlFile.readlines(): # gestiona.madrid.org is ISO-8859-1, but still we should be doing # things right line = line.strip().decode(charset).encode("utf-8") try: self.__html_parser.feed(line) except HTMLParseError, ex: print "Exception %s" % (ex.msg) htmlFile.close() # Are we parsing the correct station? # WARNING: # To get the unicode from the bytes, you decode. To get the bytes from unicode, you encode if (self.__html_parser.m_stationName.decode("utf-8") == thisProbe.name): thisMeasure = ProbeMeasure() thisMeasure.sample_time = self.__html_parser.m_sampleTime if ('CO' in self.__html_parser.m_pollutants): thisMeasure.co = self.__html_parser.m_pollutants['CO'] if ('NO' in self.__html_parser.m_pollutants): thisMeasure.no = self.__html_parser.m_pollutants['NO'] if ('NO2' in self.__html_parser.m_pollutants): thisMeasure.no2 = self.__html_parser.m_pollutants['NO2'] if ('SO2' in self.__html_parser.m_pollutants): thisMeasure.so2 = self.__html_parser.m_pollutants['SO2'] if ('PM2,5' in self.__html_parser.m_pollutants): thisMeasure.pm25 = self.__html_parser.m_pollutants['PM2,5'] if ('PM10' in self.__html_parser.m_pollutants): thisMeasure.pm10 = self.__html_parser.m_pollutants['PM10'] if ('O3' in self.__html_parser.m_pollutants): thisMeasure.o3 = self.__html_parser.m_pollutants['O3'] if ('TOL' in self.__html_parser.m_pollutants): thisMeasure.tol = self.__html_parser.m_pollutants['TOL'] if ('BEN' in self.__html_parser.m_pollutants): thisMeasure.ben = self.__html_parser.m_pollutants['BEN'] if ('XIL' in self.__html_parser.m_pollutants): thisMeasure.xyl = self.__html_parser.m_pollutants['XIL'] if ('Velocidad viento' in self.__html_parser.m_weatherParams): thisMeasure.wind_speed = self.__html_parser.m_weatherParams['Velocidad viento'] if ('Dirección viento' in self.__html_parser.m_weatherParams): thisMeasure.wind_dir = self.__html_parser.m_weatherParams['Dirección viento'] if ('Temperatura' in self.__html_parser.m_weatherParams): thisMeasure.temp = self.__html_parser.m_weatherParams['Temperatura'] if ('Humedad relativa' in self.__html_parser.m_weatherParams): thisMeasure.hum = self.__html_parser.m_weatherParams['Humedad relativa'] if ('Presión' in self.__html_parser.m_weatherParams): thisMeasure.pressure = self.__html_parser.m_weatherParams['Presión'] if ('Radiación solar' in self.__html_parser.m_weatherParams): thisMeasure.solar_rad = self.__html_parser.m_weatherParams['Radiación solar'] if ('Precipitación' in self.__html_parser.m_weatherParams): thisMeasure.precip = self.__html_parser.m_weatherParams['Precipitación'] # update probe's latest measure reference thisProbe.last_measure = thisMeasure else: print "Error parsing " + thisProbe.name + "\n" print " Station name doesn't match parsed info: " + \ self.__html_parser.m_stationName.decode("utf-8") + "\n"