Exemplo n.º 1
0
 def __init__(self):
     ProbeParser.__init__(self)
     # the HTML parser info
     self.__html_parser = JCCMDotEsHTMLParser()
     
     # fill up the probe list. 
     #
     self.probe_list.append(
         Probe(u'Albacete', JCCM_ES_PROBE_ALBACETE_URL,
             JCCM_ES_PROBE_ALBACETE_LAT, JCCM_ES_PROBE_ALBACETE_LON))
     self.probe_list.append(
         Probe(u'Azuqueca', JCCM_ES_PROBE_AZUQUECA_URL,
             JCCM_ES_PROBE_AZUQUECA_LAT, JCCM_ES_PROBE_AZUQUECA_LON))
     self.probe_list.append(
         Probe(u'Guadalajara', JCCM_ES_PROBE_GUADALAJARA_URL,
             JCCM_ES_PROBE_GUADALAJARA_LAT, JCCM_ES_PROBE_GUADALAJARA_LON))
     self.probe_list.append(
         Probe(u'Toledo', JCCM_ES_PROBE_TOLEDO_URL,
             JCCM_ES_PROBE_TOLEDO_LAT, JCCM_ES_PROBE_TOLEDO_LON))
     self.probe_list.append(
         Probe(u'Ciudad Real', JCCM_ES_PROBE_CIUDADREAL_URL,
             JCCM_ES_PROBE_CIUDADREAL_LAT, JCCM_ES_PROBE_CIUDADREAL_LON))
     self.probe_list.append(
         Probe(u'Cuenca', JCCM_ES_PROBE_CUENCA_URL,
             JCCM_ES_PROBE_CUENCA_LAT, JCCM_ES_PROBE_CUENCA_LON))
     self.probe_list.append(
         Probe(u'Illescas', JCCM_ES_PROBE_ILLESCAS_URL,
             JCCM_ES_PROBE_ILLESCAS_LAT, JCCM_ES_PROBE_ILLESCAS_LON))
     self.probe_list.append(
         Probe(u'Talavera', JCCM_ES_PROBE_TALAVERA_URL,
             JCCM_ES_PROBE_TALAVERA_LAT, JCCM_ES_PROBE_TALAVERA_LON))
     self.probe_list.append(
         Probe(u'Puertollano - Calle Ancha', JCCM_ES_PROBE_PUERTOLLANO_CALLEANCHA_URL,
             JCCM_ES_PROBE_PUERTOLLANO_CALLEANCHA_LAT, JCCM_ES_PROBE_PUERTOLLANO_CALLEANCHA_LON))
     self.probe_list.append(
         Probe(u'Puertollano - Instituto', JCCM_ES_PROBE_PUERTOLLANO_INSTITUTO_URL,
             JCCM_ES_PROBE_PUERTOLLANO_INSTITUTO_LAT, JCCM_ES_PROBE_PUERTOLLANO_INSTITUTO_LON))
     self.probe_list.append(
         Probe(u'Puertollano - Campo de fútbol', JCCM_ES_PROBE_PUERTOLLANO_CAMPOFUTBOL_URL,
             JCCM_ES_PROBE_PUERTOLLANO_CAMPOFUTBOL_LAT, JCCM_ES_PROBE_PUERTOLLANO_CAMPOFUTBOL_LON))
     self.probe_list.append(
         Probe(u'Puertollano - Barriada 630', JCCM_ES_PROBE_PUERTOLLANO_B630_URL,
             JCCM_ES_PROBE_PUERTOLLANO_B630_LAT, JCCM_ES_PROBE_PUERTOLLANO_B630_LON))
Exemplo n.º 2
0
class JCCMDotEsLiveProbeParser(ProbeParser):
    """
        Parser to retrieve probe data from jccm.es
        
        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 = JCCMDotEsHTMLParser()
        
        # fill up the probe list. 
        #
        self.probe_list.append(
            Probe(u'Albacete', JCCM_ES_PROBE_ALBACETE_URL,
                JCCM_ES_PROBE_ALBACETE_LAT, JCCM_ES_PROBE_ALBACETE_LON))
        self.probe_list.append(
            Probe(u'Azuqueca', JCCM_ES_PROBE_AZUQUECA_URL,
                JCCM_ES_PROBE_AZUQUECA_LAT, JCCM_ES_PROBE_AZUQUECA_LON))
        self.probe_list.append(
            Probe(u'Guadalajara', JCCM_ES_PROBE_GUADALAJARA_URL,
                JCCM_ES_PROBE_GUADALAJARA_LAT, JCCM_ES_PROBE_GUADALAJARA_LON))
        self.probe_list.append(
            Probe(u'Toledo', JCCM_ES_PROBE_TOLEDO_URL,
                JCCM_ES_PROBE_TOLEDO_LAT, JCCM_ES_PROBE_TOLEDO_LON))
        self.probe_list.append(
            Probe(u'Ciudad Real', JCCM_ES_PROBE_CIUDADREAL_URL,
                JCCM_ES_PROBE_CIUDADREAL_LAT, JCCM_ES_PROBE_CIUDADREAL_LON))
        self.probe_list.append(
            Probe(u'Cuenca', JCCM_ES_PROBE_CUENCA_URL,
                JCCM_ES_PROBE_CUENCA_LAT, JCCM_ES_PROBE_CUENCA_LON))
        self.probe_list.append(
            Probe(u'Illescas', JCCM_ES_PROBE_ILLESCAS_URL,
                JCCM_ES_PROBE_ILLESCAS_LAT, JCCM_ES_PROBE_ILLESCAS_LON))
        self.probe_list.append(
            Probe(u'Talavera', JCCM_ES_PROBE_TALAVERA_URL,
                JCCM_ES_PROBE_TALAVERA_LAT, JCCM_ES_PROBE_TALAVERA_LON))
        self.probe_list.append(
            Probe(u'Puertollano - Calle Ancha', JCCM_ES_PROBE_PUERTOLLANO_CALLEANCHA_URL,
                JCCM_ES_PROBE_PUERTOLLANO_CALLEANCHA_LAT, JCCM_ES_PROBE_PUERTOLLANO_CALLEANCHA_LON))
        self.probe_list.append(
            Probe(u'Puertollano - Instituto', JCCM_ES_PROBE_PUERTOLLANO_INSTITUTO_URL,
                JCCM_ES_PROBE_PUERTOLLANO_INSTITUTO_LAT, JCCM_ES_PROBE_PUERTOLLANO_INSTITUTO_LON))
        self.probe_list.append(
            Probe(u'Puertollano - Campo de fútbol', JCCM_ES_PROBE_PUERTOLLANO_CAMPOFUTBOL_URL,
                JCCM_ES_PROBE_PUERTOLLANO_CAMPOFUTBOL_LAT, JCCM_ES_PROBE_PUERTOLLANO_CAMPOFUTBOL_LON))
        self.probe_list.append(
            Probe(u'Puertollano - Barriada 630', JCCM_ES_PROBE_PUERTOLLANO_B630_URL,
                JCCM_ES_PROBE_PUERTOLLANO_B630_LAT, JCCM_ES_PROBE_PUERTOLLANO_B630_LON))

    def update(self):
        for thisProbe in self.probe_list:
            print "parsing jccm.es - " + 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():
                # jccm.es is ISO-8859-1
                line = line.strip().decode(charset).encode("utf-8")

                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 ('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 ('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']
            
            # update probe's latest measure
            thisProbe.last_measure = thisMeasure