Esempio n. 1
0
 /**
  * 
  * @param type $item
  * @param inme_fuente $fuente
  */
 private function nueva_noticia(&$item, &$fuente)
 {
     $url = NULL;
     /// intentamos obtener el enlace original de meneame
     $meneos = 0;
     foreach ($item->children('meneame', TRUE) as $element) {
         if ($element->getName() == 'url') {
             $url = (string) $element;
         } else {
             if ($element->getName() == 'votes') {
                 $meneos = intval((string) $element);
             }
         }
     }
     if (is_null($url)) {
         /// intentamos obtener el enlace original de feedburner
         foreach ($item->children('feedburner', TRUE) as $element) {
             if ($element->getName() == 'origLink') {
                 $url = (string) $element;
                 break;
             }
         }
         /// intentamos leer el/los links
         if (is_null($url) and $item->link) {
             foreach ($item->link as $l) {
                 if (mb_substr((string) $l, 0, 4) == 'http') {
                     $url = (string) $l;
                 } else {
                     if ($l->attributes()->rel == 'alternate' and $l->attributes()->type == 'text/html') {
                         $url = (string) $l->attributes()->href;
                     } else {
                         if ($l->attributes()->type == 'text/html') {
                             $url = (string) $l->attributes()->href;
                         }
                     }
                 }
             }
         }
     }
     /// reemplazamos los &
     $url = str_replace('&', '&', $url);
     if (is_null($url)) {
         $this->log[] = 'No se ha podido encontrar la url en ' . $item->asXML();
         return 0;
     }
     /// ¿Ya existe la noticia en la bd?
     $nueva = FALSE;
     $noticia = $this->noticia->get_by_url($url);
     if (!$noticia) {
         $nueva = TRUE;
         /// si no existe la creamos
         $noticia = new inme_noticia_fuente();
         $noticia->url = $url;
         $noticia->codfuente = $fuente->codfuente;
         if ($item->pubDate) {
             $noticia->fecha = date('d-m-Y H:i:s', min(array(strtotime((string) $item->pubDate), time())));
         } else {
             if ($item->published) {
                 $noticia->fecha = date('d-m-Y H:i:s', min(array(strtotime((string) $item->published), time())));
             }
         }
         $noticia->titulo = (string) $item->title;
         if ($item->description) {
             $description = (string) $item->description;
         } else {
             if ($item->content) {
                 $description = (string) $item->content;
             } else {
                 if ($item->summary) {
                     $description = (string) $item->summary;
                 } else {
                     $description = '';
                     /// intentamos leer el espacio de nombres atom
                     foreach ($item->children('atom', TRUE) as $element) {
                         if ($element->getName() == 'summary') {
                             $description = (string) $element;
                             break;
                         }
                     }
                     foreach ($item->children('content', TRUE) as $element) {
                         if ($element->getName() == 'encoded') {
                             $description = (string) $element;
                             break;
                         }
                     }
                 }
             }
         }
         if ($fuente->meneame()) {
             /// quitamos el latiguillo de las noticias de menéame
             $aux = '';
             for ($i = 0; $i < mb_strlen($description); $i++) {
                 if (mb_substr($description, $i, 4) == '</p>') {
                     break;
                 } else {
                     $aux .= mb_substr($description, $i, 1);
                 }
             }
             $description = $aux;
         }
         /// eliminamos el html de la descripción
         $description = strip_tags(html_entity_decode($description, ENT_QUOTES, 'UTF-8'));
         $noticia->texto = $description;
         $noticia->resumen = substr($noticia->texto, 0, 300);
         /// procesamos las keywords de categorías
         if ($item->category) {
             foreach ($item->category as $cat) {
                 if (strlen((string) $cat) > 1) {
                     $tema = $this->tema->get((string) $cat);
                     if (!$tema) {
                         $tema = new inme_tema();
                         $tema->codtema = $tema->texto = (string) $cat;
                         $tema->save();
                     }
                     $noticia->set_keyword((string) $cat);
                 }
             }
         }
         /// procesamos las keywords y las imágenes de media
         foreach ($item->children('media', TRUE) as $element) {
             if ($element->getName() == 'thumbnail') {
                 $noticia->preview = (string) $element;
             } else {
                 if ($element->getName() == 'keywords') {
                     $aux = explode(',', (string) $element);
                     if ($aux) {
                         foreach ($aux as $a) {
                             $noticia->set_keyword(trim($a));
                         }
                     }
                 }
             }
         }
     }
     if ($meneos > 0) {
         $noticia->meneos = $meneos;
     }
     if ($noticia->save()) {
         if ($nueva) {
             $this->log[] = 'Encontrada noticia: <a href="' . $noticia->url . '" target="_blank">' . $noticia->titulo . '</a>';
         }
     } else {
         $this->log[] = 'Error al procesar la noticia: ' . $noticia->url;
     }
 }
Esempio n. 2
0
 /**
  * 
  * @param type $item
  * @param inme_fuente $fuente
  */
 private function nueva_noticia(&$item, &$fuente)
 {
     $url = NULL;
     /// intentamos obtener el enlace original de meneame
     $meneos = 0;
     $meneame_link = FALSE;
     foreach ($item->children('meneame', TRUE) as $element) {
         if ($element->getName() == 'url') {
             $url = (string) $element;
             $meneame_link = (string) $item->link;
         } else {
             if ($element->getName() == 'votes' or $element->getName() == 'clicks' or $element->getName() == 'comments') {
                 $meneos = max(array($meneos, intval((string) $element)));
             }
         }
     }
     if (is_null($url)) {
         /// intentamos obtener el enlace original de feedburner
         foreach ($item->children('feedburner', TRUE) as $element) {
             if ($element->getName() == 'origLink') {
                 $url = (string) $element;
                 break;
             }
         }
         /// intentamos leer el/los links
         if (is_null($url) and $item->link) {
             foreach ($item->link as $l) {
                 if (mb_substr((string) $l, 0, 4) == 'http') {
                     $url = (string) $l;
                 } else {
                     if ($l->attributes()->rel == 'alternate' and $l->attributes()->type == 'text/html') {
                         $url = (string) $l->attributes()->href;
                     } else {
                         if ($l->attributes()->type == 'text/html') {
                             $url = (string) $l->attributes()->href;
                         }
                     }
                 }
             }
         }
     }
     /// reemplazamos los &amp;
     $url = str_replace('&amp;', '&', $url);
     if (is_null($url)) {
         $this->log[] = 'No se ha podido encontrar la url en ' . $item->asXML();
         return 0;
     }
     /// ¿Ya existe la noticia en la bd?
     $nueva = FALSE;
     $noticia = $this->noticia->get_by_url($url);
     if (!$noticia) {
         /// es posible que la noticia ya exista, pero con otra url
         $titulo = $this->true_text_break((string) $item->title, 140);
         $noticia2 = $this->noticia->get_by_titulo($titulo);
         if ($noticia2) {
             /**
              * si hay una noticia con el mismo título y de hace menos de una
              * semana, la consideramos un duplicado.
              */
             if (strtotime($noticia2->fecha) > time() - 604800) {
                 $noticia = $noticia2;
             }
         }
     }
     if ($noticia) {
         /// procesamos las keywords de categorías
         if ($item->category) {
             foreach ($item->category as $cat) {
                 if (strlen((string) $cat) > 1) {
                     $codtema = $this->sanitize_url((string) $cat, 50);
                     $tema = $this->tema->get($codtema);
                     if (!$tema) {
                         $tema = new inme_tema();
                         $tema->codtema = $codtema;
                         $tema->titulo = $tema->texto = (string) $cat;
                     }
                     if ($tema->activo) {
                         if (!in_array($codtema, $noticia->keywords())) {
                             $tema->articulos++;
                         }
                         if ($tema->save()) {
                             if (is_null($noticia->preview) or $noticia->preview == '') {
                                 $noticia->preview = $tema->imagen;
                             }
                             $noticia->set_keyword($codtema);
                         }
                     }
                 }
             }
         }
         /// si esta fuente afirma que la fechal de la noticia es anterior a la que tenemos, usamos estos datos
         $fecha2 = date('d-m-Y H:i:s');
         if ($item->pubDate) {
             $fecha2 = date('d-m-Y H:i:s', min(array(strtotime((string) $item->pubDate), time())));
         } else {
             if ($item->published) {
                 $fecha2 = date('d-m-Y H:i:s', min(array(strtotime((string) $item->published), time())));
             }
         }
         if (strtotime($fecha2) < $noticia->fecha) {
             $noticia->fecha = $fecha2;
             $noticia->codfuente = $fuente->codfuente;
         }
     } else {
         $nueva = TRUE;
         /// si no existe la creamos
         $noticia = new inme_noticia_fuente();
         $noticia->url = $url;
         $noticia->codfuente = $fuente->codfuente;
         $noticia->nativa = $fuente->nativa;
         $noticia->parodia = $fuente->parodia;
         if ($item->pubDate) {
             $noticia->fecha = date('d-m-Y H:i:s', min(array(strtotime((string) $item->pubDate), time())));
         } else {
             if ($item->published) {
                 $noticia->fecha = date('d-m-Y H:i:s', min(array(strtotime((string) $item->published), time())));
             }
         }
         $noticia->titulo = $this->true_text_break((string) $item->title, 140);
         if ($item->description) {
             $description = (string) $item->description;
         } else {
             if ($item->content) {
                 $description = (string) $item->content;
             } else {
                 if ($item->summary) {
                     $description = (string) $item->summary;
                 } else {
                     $description = '';
                     /// intentamos leer el espacio de nombres atom
                     foreach ($item->children('atom', TRUE) as $element) {
                         if ($element->getName() == 'summary') {
                             $description = (string) $element;
                             break;
                         }
                     }
                     foreach ($item->children('content', TRUE) as $element) {
                         if ($element->getName() == 'encoded') {
                             $description = (string) $element;
                             break;
                         }
                     }
                 }
             }
         }
         if ($fuente->meneame()) {
             /// quitamos el latiguillo de las noticias de menéame
             $aux = '';
             for ($i = 0; $i < mb_strlen($description); $i++) {
                 if (mb_substr($description, $i, 4) == '</p>') {
                     break;
                 } else {
                     $aux .= mb_substr($description, $i, 1);
                 }
             }
             $description = $aux;
         }
         /// eliminamos el html de la descripción
         $description = strip_tags(html_entity_decode($description, ENT_QUOTES, 'UTF-8'));
         $noticia->texto = trim($description);
         $noticia->resumen = $this->true_text_break($noticia->texto, 300);
         /// procesamos las keywords de categorías
         if ($item->category) {
             foreach ($item->category as $cat) {
                 if (strlen((string) $cat) > 1) {
                     $codtema = $this->sanitize_url((string) $cat, 50);
                     $tema = $this->tema->get($codtema);
                     if (!$tema) {
                         $tema = new inme_tema();
                         $tema->codtema = $codtema;
                         $tema->titulo = $tema->texto = (string) $cat;
                     }
                     if ($tema->activo) {
                         $tema->articulos++;
                         if ($tema->save()) {
                             if (is_null($noticia->preview) or $noticia->preview == '') {
                                 $noticia->preview = $tema->imagen;
                             }
                             $noticia->set_keyword($codtema);
                         }
                     }
                     /// ¿Parodia?
                     if (strpos((string) $cat, 'humor') !== FALSE) {
                         $noticia->parodia = TRUE;
                     }
                 }
             }
         }
         /// procesamos las keywords y las imágenes de media
         foreach ($item->children('media', TRUE) as $element) {
             if ($element->getName() == 'thumbnail' and !$noticia->editada) {
                 $noticia->preview = (string) $element;
             } else {
                 if ($element->getName() == 'keywords') {
                     $aux = explode(',', (string) $element);
                     if ($aux) {
                         foreach ($aux as $a) {
                             if (strlen((string) $a) > 1) {
                                 $codtema = $this->sanitize_url((string) $a, 50);
                                 $tema = $this->tema->get($codtema);
                                 if (!$tema) {
                                     $tema = new inme_tema();
                                     $tema->codtema = $codtema;
                                     $tema->titulo = $tema->texto = (string) $a;
                                 }
                                 if ($tema->activo) {
                                     $tema->articulos++;
                                     if ($tema->save()) {
                                         if (is_null($noticia->preview) or $noticia->preview == '') {
                                             $noticia->preview = $tema->imagen;
                                         }
                                         $noticia->set_keyword($codtema);
                                     }
                                 }
                             }
                         }
                     }
                 }
             }
         }
     }
     if ($meneos > $noticia->meneos) {
         $noticia->meneos = $meneos;
         $noticia->meneame_link = $meneame_link;
     }
     if ($noticia->save()) {
         if ($nueva) {
             $this->log[] = 'Encontrada noticia: <a href="' . $noticia->edit_url() . '" target="_blank">' . $noticia->titulo . '</a>';
         }
     } else {
         $this->log[] = 'Error al procesar la noticia: ' . $noticia->url;
     }
 }