def analize(self,url):
        try: 
            html = urlopen(url)
        except HTTPError as e:
            print(e)
        except URLError:
            print("Servidor no encontrado")
        else:        
            content = html.read().decode('utf-8', 'ignore')
            res = BeautifulSoup(content,"html.parser")
            info = res.find("div",{"id":"publicacionInfo"})
            if (info is None):
                print("NO hay datos de este boletín")
                return False

            numero = int(info.span.getText().split()[1]) 
            # obtener el año. 
            info = res.find("span",{"id":"DOGData"})
            temp = info.getText().split()
            year = int(temp[len(temp)-1])
            mes = self.meses.index(temp[len(temp)-3]) + 1
            dia = int(temp[len(temp)-5])
            fecha = date(year,mes,dia)

            if  (self.checkBulletinExists(self.bulletin,fecha) == True):
                print ("Ya he encontrado datos")
                return True
                
            self.beginAnalysis(fecha)
            #self.setAnalysisState("DOGA",fecha,"INICIADO")
            sections = res.findAll("div",{"id":re.compile('secciones*')})
            for section in sections:
                lines = section.findAll("li")
                for line in lines:
                    if (line.a is not None):
                        noticia = Noticia()
                        noticia.bulletin = self.bulletin
                        noticia.bulletin_year = year
                        noticia.bulletin_no = numero
                        noticia.bulletin_date = fecha
                        noticia.seccion   = ""
                        noticia.organismo = line.findPrevious('p',{'class':'dog-toc-organismo'}).getText()
                        noticia.organo    = ""  #li.span.getText()
                        noticia.servicio  = ""        
                        noticia.newname = line.a.getText()
                        noticia.url = "https://www.xunta.gal" + line.a['href'] 
                        self.normalizar(noticia)
                        pseccion =  line.findPrevious('p',{'class':'dog-toc-nivel-2'})
                        if (pseccion is not None):                                
                            if (pseccion.getText() == 'c) Outros anuncios'):
                                noticia.seccion = 'SECCIÓN NON OFICIAL'
                        if (self.isNotificable(noticia.newname)):
                            noticia.notify = 1
                        noticia.fav = 0
                        noticia.readed = 0
                        noticia.created_at = datetime.now()
                        noticia.updated_at = datetime.now()
                        noticia.imprimir()
                        noticia.save()
Exemple #2
0
    def analize(self, url):
        try:
            html = urlopen(url)
        except HTTPError as e:
            print(e)
        except URLError as u:
            print("Servidor depo no encontrado " + url)
            print(u)
        else:
            content = html.read().decode('utf-8', 'ignore')
            res = BeautifulSoup(content, "html.parser")

            s_info = res.find("div", {"id": "infoBoletin"})
            if (s_info is None):
                print("No se ha encontrado boletín")
                return False

            info = s_info.find("a").getText()
            cachos = info.split()
            numero = int(cachos[4])
            dia = int(cachos[7])
            mes = self.meses.index(cachos[9]) + 1
            ano = int(cachos[11])
            fecha = date(ano, mes, dia)

            if (self.checkNumber(ano, numero, "BOPCO")):
                return True  # Ya existen.

            self.beginAnalysis(fecha)
            self.setAnalysisState("BOPCO", fecha, "INICIADO")

            anuncios = res.findAll("div", {"class": "bloqueAnuncio"})
            for anuncio in anuncios:
                noticia = Noticia()
                noticia.bulletin = "BOPCO"
                noticia.bulletin_year = ano
                noticia.bulletin_no = numero
                noticia.bulletin_date = fecha

                if (anuncio.find("h2")):
                    noticia.organismo = anuncio.find("h2").getText()
                    noticia.organo = anuncio.find("h3").getText()
                    servi = anuncio.find("h4")
                    if (servi is not None):
                        noticia.servicio = anuncio.find("h4").getText()
                else:
                    tmp = anuncio.find("h3")
                    if (tmp is not None):
                        noticia.organismo = anuncio.find("h3").getText()
                    else:
                        noticia.organismo = "OTROS"

                    if (anuncio.find("h4")):
                        noticia.organo = anuncio.find("h4").getText()

                    noticia.servicio = ""
                noticia.seccion = anuncio.findPrevious(
                    'h1', {
                        "class": "administracion"
                    }).getText()

                resumen = anuncio.find("p", {"class": "resumenSumario"})
                noticia.url = "https://bop.dacoruna.gal/bopportal/publicado/" + str(
                    ano) + "/" + str(mes) + "/" + str(
                        dia) + "/" + resumen.select("a")[0]['href']
                noticia.newname = resumen.select("a")[0].getText().strip(
                ) + " - " + resumen.select("a")[1].getText().strip()
                # la notificación hay que comprobarla
                if (self.isNotificable(noticia.newname)):
                    noticia.notify = 1
                noticia.fav = 0
                noticia.readed = 0
                noticia.created_at = datetime.now()
                noticia.updated_at = datetime.now()
                noticia.imprimir()
                noticia.save()
            self.setAnalysisState("BOPCO", fecha, "FINALIZADO")