def __init__(self): ProbeParser.__init__(self) # the HTML parser info self.__html_parser = AirecantabriaHTMLParser() # fill up the probe list. # self.probe_list.append( Probe(u'Santander - Centro', AIRECANTABRIA_COM_PROBE_SANTANDER_CENTRO_URL, AIRECANTABRIA_COM_PROBE_SANTANDER_CENTRO_LAT, AIRECANTABRIA_COM_PROBE_SANTANDER_CENTRO_LON)) self.probe_list.append( Probe(u'Santander - Tetuán', AIRECANTABRIA_COM_PROBE_SANTANDER_TETUAN_URL, AIRECANTABRIA_COM_PROBE_SANTANDER_TETUAN_LAT, AIRECANTABRIA_COM_PROBE_SANTANDER_TETUAN_LON)) self.probe_list.append( Probe(u'Cros-Camargo', AIRECANTABRIA_COM_PROBE_CAMARGO_URL, AIRECANTABRIA_COM_PROBE_CAMARGO_LAT, AIRECANTABRIA_COM_PROBE_CAMARGO_LON)) self.probe_list.append( Probe(u'Guarnizo', AIRECANTABRIA_COM_PROBE_GUARNIZO_URL, AIRECANTABRIA_COM_PROBE_GUARNIZO_LAT, AIRECANTABRIA_COM_PROBE_GUARNIZO_LON)) self.probe_list.append( Probe(u'Torrelavega - Escuela de Minas', AIRECANTABRIA_COM_PROBE_TORRELAVEGA_MINAS_URL, AIRECANTABRIA_COM_PROBE_TORRELAVEGA_MINAS_LAT, AIRECANTABRIA_COM_PROBE_TORRELAVEGA_MINAS_LON)) self.probe_list.append( Probe(u'Torrelavega - Parque Zapatón', AIRECANTABRIA_COM_PROBE_TORRELAVEGA_ZAPATON_URL, AIRECANTABRIA_COM_PROBE_TORRELAVEGA_ZAPATON_LAT, AIRECANTABRIA_COM_PROBE_TORRELAVEGA_ZAPATON_LON)) self.probe_list.append( Probe(u'Torrelavega - Barreda', AIRECANTABRIA_COM_PROBE_TORRELAVEGA_BARREDA_URL, AIRECANTABRIA_COM_PROBE_TORRELAVEGA_BARREDA_LAT, AIRECANTABRIA_COM_PROBE_TORRELAVEGA_BARREDA_LON)) # self.probe_list.append( # Probe(u'Torrelavega - C.I.M.A.', AIRECANTABRIA_COM_PROBE_TORRELAVEGA_CIMA_URL, # AIRECANTABRIA_COM_PROBE_TORRELAVEGA_CIMA_LAT, AIRECANTABRIA_COM_PROBE_TORRELAVEGA_CIMA_LON)) self.probe_list.append( Probe(u'Los Corrales de Buelna', AIRECANTABRIA_COM_PROBE_BUELNA_URL, AIRECANTABRIA_COM_PROBE_BUELNA_LAT, AIRECANTABRIA_COM_PROBE_BUELNA_LON)) self.probe_list.append( Probe(u'Reinosa', AIRECANTABRIA_COM_PROBE_REINOSA_URL, AIRECANTABRIA_COM_PROBE_REINOSA_LAT, AIRECANTABRIA_COM_PROBE_REINOSA_LON)) self.probe_list.append( Probe(u'Los Tojos', AIRECANTABRIA_COM_PROBE_TOJOS_URL, AIRECANTABRIA_COM_PROBE_TOJOS_LAT, AIRECANTABRIA_COM_PROBE_TOJOS_LON)) self.probe_list.append( Probe(u'Castro Urdiales', AIRECANTABRIA_COM_PROBE_CASTRO_URL, AIRECANTABRIA_COM_PROBE_CASTRO_LAT, AIRECANTABRIA_COM_PROBE_CASTRO_LON))
class AirecantabriaLiveProbeParser(ProbeParser): """ Parser to retrieve probe data from airecantabria.com 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 = AirecantabriaHTMLParser() # fill up the probe list. # self.probe_list.append( Probe(u'Santander - Centro', AIRECANTABRIA_COM_PROBE_SANTANDER_CENTRO_URL, AIRECANTABRIA_COM_PROBE_SANTANDER_CENTRO_LAT, AIRECANTABRIA_COM_PROBE_SANTANDER_CENTRO_LON)) self.probe_list.append( Probe(u'Santander - Tetuán', AIRECANTABRIA_COM_PROBE_SANTANDER_TETUAN_URL, AIRECANTABRIA_COM_PROBE_SANTANDER_TETUAN_LAT, AIRECANTABRIA_COM_PROBE_SANTANDER_TETUAN_LON)) self.probe_list.append( Probe(u'Cros-Camargo', AIRECANTABRIA_COM_PROBE_CAMARGO_URL, AIRECANTABRIA_COM_PROBE_CAMARGO_LAT, AIRECANTABRIA_COM_PROBE_CAMARGO_LON)) self.probe_list.append( Probe(u'Guarnizo', AIRECANTABRIA_COM_PROBE_GUARNIZO_URL, AIRECANTABRIA_COM_PROBE_GUARNIZO_LAT, AIRECANTABRIA_COM_PROBE_GUARNIZO_LON)) self.probe_list.append( Probe(u'Torrelavega - Escuela de Minas', AIRECANTABRIA_COM_PROBE_TORRELAVEGA_MINAS_URL, AIRECANTABRIA_COM_PROBE_TORRELAVEGA_MINAS_LAT, AIRECANTABRIA_COM_PROBE_TORRELAVEGA_MINAS_LON)) self.probe_list.append( Probe(u'Torrelavega - Parque Zapatón', AIRECANTABRIA_COM_PROBE_TORRELAVEGA_ZAPATON_URL, AIRECANTABRIA_COM_PROBE_TORRELAVEGA_ZAPATON_LAT, AIRECANTABRIA_COM_PROBE_TORRELAVEGA_ZAPATON_LON)) self.probe_list.append( Probe(u'Torrelavega - Barreda', AIRECANTABRIA_COM_PROBE_TORRELAVEGA_BARREDA_URL, AIRECANTABRIA_COM_PROBE_TORRELAVEGA_BARREDA_LAT, AIRECANTABRIA_COM_PROBE_TORRELAVEGA_BARREDA_LON)) # self.probe_list.append( # Probe(u'Torrelavega - C.I.M.A.', AIRECANTABRIA_COM_PROBE_TORRELAVEGA_CIMA_URL, # AIRECANTABRIA_COM_PROBE_TORRELAVEGA_CIMA_LAT, AIRECANTABRIA_COM_PROBE_TORRELAVEGA_CIMA_LON)) self.probe_list.append( Probe(u'Los Corrales de Buelna', AIRECANTABRIA_COM_PROBE_BUELNA_URL, AIRECANTABRIA_COM_PROBE_BUELNA_LAT, AIRECANTABRIA_COM_PROBE_BUELNA_LON)) self.probe_list.append( Probe(u'Reinosa', AIRECANTABRIA_COM_PROBE_REINOSA_URL, AIRECANTABRIA_COM_PROBE_REINOSA_LAT, AIRECANTABRIA_COM_PROBE_REINOSA_LON)) self.probe_list.append( Probe(u'Los Tojos', AIRECANTABRIA_COM_PROBE_TOJOS_URL, AIRECANTABRIA_COM_PROBE_TOJOS_LAT, AIRECANTABRIA_COM_PROBE_TOJOS_LON)) self.probe_list.append( Probe(u'Castro Urdiales', AIRECANTABRIA_COM_PROBE_CASTRO_URL, AIRECANTABRIA_COM_PROBE_CASTRO_LAT, AIRECANTABRIA_COM_PROBE_CASTRO_LON)) def update(self): for thisProbe in self.probe_list: print "parsing airecantabria.com - " + thisProbe.name + "..." # ensure the aprser 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) # charset detection fails in airecantabria.com #charset = htmlFile.headers.getparam('charset') lineCount = 0 for line in htmlFile.readlines(): # airecantabria.com is UTF-8 lineCount = lineCount + 1 # parsing these URLs go bananas before this line if (lineCount > 199): line = line.strip() try: self.__html_parser.feed(line) except HTMLParseError, ex: print "Exception %s" % (ex.msg) htmlFile.close() 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 ('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 ('SH2' in self.__html_parser.m_pollutants): thisMeasure.sh2 = self.__html_parser.m_pollutants['SH2'] 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 (len(self.__html_parser.m_weatherParams) > 0): if ('VV' in self.__html_parser.m_weatherParams): thisMeasure.wind_speed = self.__html_parser.m_weatherParams['VV'] if ('DD' in self.__html_parser.m_weatherParams): thisMeasure.wind_dir = self.__html_parser.m_weatherParams['DD'] if ('TMP' in self.__html_parser.m_weatherParams): thisMeasure.temp = self.__html_parser.m_weatherParams['TMP'] if ('HR' in self.__html_parser.m_weatherParams): thisMeasure.hum = self.__html_parser.m_weatherParams['HR'] if ('PRB' in self.__html_parser.m_weatherParams): thisMeasure.pressure = self.__html_parser.m_weatherParams['PRB'] if ('RS' in self.__html_parser.m_weatherParams): thisMeasure.solar_rad = self.__html_parser.m_weatherParams['RS'] if ('LL' in self.__html_parser.m_weatherParams): thisMeasure.precip = self.__html_parser.m_weatherParams['LL'] # update probe's latest measure reference thisProbe.last_measure = thisMeasure