/** * * @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; } }
protected function private_core() { /// ¿El usuario tiene permiso para eliminar en esta página? $this->allow_delete = $this->user->allow_delete_on(__CLASS__); $fsvar = new fs_var(); $this->analytics = $fsvar->simple_get('inme_analytics'); $this->modrewrite = $fsvar->simple_get('inme_modrewrite'); $this->buscar = ''; $this->noticia = FALSE; $this->relacionada = FALSE; $this->temas = array(); $noti0 = new inme_noticia_fuente(); if (isset($_GET['delete'])) { $delete = $noti0->get($_GET['delete']); if ($delete) { if ($delete->delete()) { $this->new_error_msg('Noticia eliminada correctamente.'); } } } if (isset($_REQUEST['id'])) { $this->noticia = $noti0->get($_REQUEST['id']); } if ($this->noticia) { if (isset($_POST['url'])) { $this->noticia->editada = TRUE; $this->noticia->url = $_POST['url']; $this->noticia->titulo = $_POST['titulo']; $this->noticia->resumen = substr($_POST['resumen'], 0, 300); $this->noticia->texto = $_POST['texto']; $this->noticia->id_relacionada = null; if ($_POST['id_relacionada'] != '') { $this->noticia->id_relacionada = intval($_POST['id_relacionada']); } $this->noticia->preview = $_POST['preview']; $this->noticia->clean_keywords(); $keys = explode(',', $_POST['keywords']); if ($keys) { $tema0 = new inme_tema(); foreach ($keys as $k) { if ($k != '') { $codtema = $this->sanitize_url($k, 50); $tema = $tema0->get($codtema); if (!$tema) { $tema = new inme_tema(); $tema->codtema = $codtema; $tema->titulo = $tema->texto = $k; } if ($tema->save()) { $this->noticia->set_keyword($codtema); } } } } $this->noticia->destacada = isset($_POST['destacada']); $this->noticia->nativa = isset($_POST['nativa']); $this->noticia->parodia = isset($_POST['parodia']); if ($this->noticia->save()) { $this->new_message('Datos modificados correctamente.'); $this->cache->delete('inme_portada'); } else { $this->new_error_msg('Error al guardar los datos.'); } } else { if (isset($_GET['bad_image'])) { $this->cambiar_imagen(); } } $this->page->title = $this->noticia->titulo; if (!is_null($this->noticia->id_relacionada)) { $this->relacionada = $noti0->get($this->noticia->id_relacionada); } $tema0 = new inme_tema(); foreach ($this->noticia->keywords() as $key) { $tema = $tema0->get($key); if ($tema) { if ($tema->activo) { $this->temas[] = $tema; /// si no hay una preview, usamos la de un tema if ($tema->imagen and !$this->noticia->preview) { $this->noticia->preview = $tema->imagen; $this->noticia->save(); } } } else { $this->new_error_msg('Tema ' . $key . ' no encontrado.'); } } } else { $this->new_error_msg('Noticia no encontrada.'); } }
/** * * @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 & $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) { /// 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; } }