protected function private_core() { /// ¿El usuario tiene permiso para eliminar en esta página? $this->allow_delete = $this->user->allow_delete_on(__CLASS__); $tema0 = new inme_tema(); $this->tema = FALSE; if (isset($_REQUEST['cod'])) { $this->tema = $tema0->get($_REQUEST['cod']); } if ($this->tema) { if (isset($_POST['titulo'])) { $this->tema->titulo = $_POST['titulo']; $this->tema->texto = $_POST['texto']; $this->tema->busqueda = $_POST['busqueda']; $this->tema->activo = isset($_POST['activo']); $this->tema->clean_keywords(); $keys = explode(',', $_POST['keywords']); if ($keys) { foreach ($keys as $k) { if ($k != '') { $this->tema->set_keyword($this->sanitize_url($k, 50)); } } } $this->tema->imagen = NULL; if ($_POST['imagen'] != '') { $this->tema->imagen = $_POST['imagen']; $this->aplicar_cambios(); } if ($this->tema->save()) { $this->new_message('Datos guardadados correctamente.'); $this->cache->delete('inme_temas_populares'); } else { $this->new_error_msg('Error al guardar los datos.'); } } else { if (isset($_GET['bad_image'])) { $this->cambiar_imagen(); } } $noti = new inme_noticia_fuente(); $this->noticias = $noti->all_from_keyword($this->tema->codtema); } else { $this->new_error_msg('Tema no encontrado.'); } }
public function stats_temas() { $stats = array(); $te0 = new inme_tema(); $max = 9; foreach ($te0->populares() as $i => $tema) { if ($i < $max) { $sql = "SELECT DATE_FORMAT(fecha, '%Y-%m') as fecha2,SUM(popularidad) as num FROM inme_noticias_fuente" . " WHERE keywords LIKE '%[" . $tema->codtema . "]%' group by fecha2"; $data = $this->db->select($sql); if ($data) { foreach ($data as $d) { if (!isset($stats[$d['fecha2']])) { $stats[$d['fecha2']] = array('time' => strtotime($d['fecha2']), 'tema_0' => array('codtema' => '-', 'popularidad' => 0), 'tema_1' => array('codtema' => '-', 'popularidad' => 0), 'tema_2' => array('codtema' => '-', 'popularidad' => 0), 'tema_3' => array('codtema' => '-', 'popularidad' => 0), 'tema_4' => array('codtema' => '-', 'popularidad' => 0), 'tema_5' => array('codtema' => '-', 'popularidad' => 0), 'tema_6' => array('codtema' => '-', 'popularidad' => 0), 'tema_7' => array('codtema' => '-', 'popularidad' => 0)); } $stats[$d['fecha2']]['tema_' . $i]['codtema'] = $tema->codtema; $stats[$d['fecha2']]['tema_' . $i]['popularidad'] = intval($d['num']); } } } else { break; } } /// completamos datos foreach ($te0->populares() as $i => $tema) { if ($i < $max) { foreach ($stats as $j => $value) { $stats[$j]['tema_' . $i]['codtema'] = $tema->codtema; } } else { break; } } /// ordenamos uasort($stats, function ($a, $b) { if ($a['time'] == $b['time']) { return 0; } else { if ($a['time'] < $b['time']) { return -1; } else { return 1; } } }); return $stats; }
private function comprobar_temas() { $noti0 = new inme_noticia_fuente(); $tema0 = new inme_tema(); /** * Leemos noticias y sacamos las keywords. */ $keys = array(); /// noticias de portadas foreach ($noti0->all(0, 'publicada DESC') as $n) { foreach ($n->keywords() as $key) { if (!in_array($key, $keys)) { $keys[] = $key; } } } /// últimas noticias foreach ($noti0->all() as $n) { foreach ($n->keywords() as $key) { if (!in_array($key, $keys)) { $keys[] = $key; } } } shuffle($keys); /** * Ahora buscamos los temas de esas keywords. */ $temas = array(); foreach ($keys as $k) { $tema = $tema0->get($k); if ($tema) { $temas[] = $tema; } } /** * Completamos descripciones de los temas con ayuda de la wikipedia. */ $max = 10; foreach ($temas as $tema) { if ($max <= 0) { break; } else { if ($tema->activo and mb_strtolower($tema->texto, 'UTF8') == mb_strtolower($tema->titulo, 'UTF8')) { /// buscamos en la wikipedia $url = 'https://es.wikipedia.org/w/api.php?format=json&action=query&prop=extracts' . '&exintro=&explaintext=&redirects=1&titles=' . urlencode($tema->titulo); $html = file_get_contents($url); if ($html) { $json = json_decode($html); if (isset($json->query)) { if (isset($json->query->pages)) { foreach ($json->query->pages as $page) { if (!isset($page->extract)) { /// caca } else { if (mb_strlen($page->extract) > 100) { $tema->titulo = $page->title; $tema->texto = $page->extract; if ($tema->save()) { echo '- Wikipedia: ' . $tema->codtema . ' -'; } } } } } } } $max--; } } } /** * Agregamos imágenes a los temas con ayuda de bing. */ $max = 10; foreach ($temas as $tema) { if ($max <= 0) { break; } else { if ($tema->activo and is_null($tema->imagen)) { $tema->imagen = $this->get_image_from_bing($tema->titulo); if ($tema->imagen) { if ($tema->save()) { echo '- Bing: ' . $tema->codtema . ' '; /// buscamos noticias relacionadas foreach ($noti0->all_from_keyword($tema->codtema) as $n) { if (is_null($n->preview)) { $n->preview = $tema->imagen; $n->save(); echo '*'; } } echo ' -'; } } $max--; } } } /** * Realizamos una busqueda en las noticias para asignarle tema */ $sql = "SELECT * FROM inme_temas WHERE busqueda != '' ORDER BY popularidad DESC;"; $data = $this->db->select($sql); if ($data) { foreach ($data as $d) { $tema = new inme_tema($d); foreach ($tema->busquedas() as $buscar) { echo '- buscar: ' . $buscar . ' '; foreach ($noti0->search($buscar, 0, 'fecha DESC') as $no) { $no->set_keyword($tema->codtema); if (is_null($no->preview) and $tema->imagen) { $no->preview = $tema->imagen; echo '*'; } $no->save(); } echo ' -'; } } } $max = 10; $total = $tema0->count(); while ($total > 0 and $max > 0) { $tema0->cron_job(); $total -= FS_ITEM_LIMIT; $max--; echo 'T'; } }
protected function public_core() { $this->template = 'inme_public/editar_noticia'; $this->page_description = 'Detalle de la noticia.'; $this->buscar = ''; $fsvar = new fs_var(); $this->analytics = $fsvar->simple_get('inme_analytics'); $this->modrewrite = $fsvar->simple_get('inme_modrewrite'); if (isset($_GET['ok_cookies'])) { setcookie('ok_cookies', 'TRUE', time() + FS_COOKIES_EXPIRE, '/'); $fslog = new fs_log(); $fslog->tipo = 'cookies'; $fslog->detalle = 'Se han aceptado las cookies'; $fslog->ip = $_SERVER['REMOTE_ADDR']; $fslog->save(); } $this->noticia = FALSE; $this->relacionada = FALSE; $this->temas = array(); $noti0 = new inme_noticia_fuente(); if (isset($_REQUEST['id'])) { $this->noticia = $noti0->get($_REQUEST['id']); } else { if (isset($_REQUEST['permalink'])) { $this->noticia = $noti0->get_by_permalink($_REQUEST['permalink']); } } if ($this->noticia) { $this->page_title = $this->noticia->titulo; $this->page_description = $this->true_text_break($this->noticia->resumen, 140); 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.'); } }
public function procesar_portada() { $this->mostrar = 'portada'; if (isset($_GET['mostrar'])) { $this->mostrar = $_GET['mostrar']; } $this->buscar = ''; if (isset($_REQUEST['buscar'])) { $this->buscar = $_REQUEST['buscar']; } $this->codfuente = ''; if (isset($_GET['codfuente'])) { $this->codfuente = $_GET['codfuente']; } $this->keyword = ''; if (isset($_GET['keyword'])) { $this->keyword = $_GET['keyword']; } $this->offset = 0; if (isset($_GET['offset'])) { $this->offset = intval($_GET['offset']); } $this->preview = new inme_noticia_preview(); $noti = new inme_noticia_fuente(); if ($this->buscar != '') { $this->noticias = $noti->search($this->buscar, $this->offset); } else { if ($this->codfuente != '') { $this->noticias = $noti->all_from_fuente($this->codfuente, $this->offset); } else { if ($this->keyword != '') { $this->noticias = $noti->all_from_keyword($this->keyword, $this->offset); } else { if ($this->mostrar == 'portada') { if ($this->offset > 0) { $this->noticias = $noti->all($this->offset, 'publicada DESC'); } else { $this->noticias = $this->cache->get('inme_portada'); if (!$this->noticias) { $this->noticias = $noti->all($this->offset, 'publicada DESC'); $this->cache->set('inme_portada', $this->noticias, 300); } } } else { if ($this->mostrar == 'populares') { $this->noticias = $noti->all($this->offset, 'popularidad DESC'); } else { $this->noticias = $noti->all($this->offset); } } } } } $tema = new inme_tema(); $this->mostrar_tema = FALSE; if (isset($_GET['keyword'])) { $this->mostrar_tema = $tema->get($_GET['keyword']); } $this->temas_populares = $this->cache->get('inme_temas_populares'); if (!$this->temas_populares) { $this->temas_populares = $tema->populares(); $this->cache->set('inme_temas_populares', $this->temas_populares, 300); } }
/** * * @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; } }
/** * Busca imágentes/vídeos en la noticia. * @param inme_noticia_fuente $noti */ private function preview_noticia(&$noti) { if ($noti->editada) { /// si está editada, no hacemos nada } else { if (is_null($noti->preview)) { /// primero intentamos asignar la imagen de un tema $tema0 = new inme_tema(); foreach ($noti->keywords() as $key) { $tema = $tema0->get($key); if ($tema) { if ($tema->imagen and $tema->activo) { $noti->preview = $tema->imagen; $noti->save(); $this->log[] = 'Asignada imagen del tema ' . $tema->titulo . ': <a href="' . $noti->edit_url() . '" target="_blank">' . $noti->titulo . '</a>'; break; } } } /// ahora buscamos una previsualización $preview = new inme_noticia_preview(); $preview->load($noti->url, $noti->texto); if ($preview->type) { /** * nos interesan previews de youtube y vimeo, así como imágenes de imgur, * PERO si es una imagen normal, solamente la queremos si no tenemos nada. */ if (is_null($noti->preview) and ($preview->type == 'imgur' or $preview->type == 'image')) { $noti->preview = $preview->preview(); $noti->texto .= "\n<div class='thumbnail'>\n<img src='" . $preview->link . "' alt='" . $noti->titulo . "'/>\n</div>"; $noti->editada = TRUE; $noti->save(); } else { if ($preview->type == 'youtube') { $imagen = $preview->preview(); if ($imagen) { $noti->preview = $imagen; $noti->texto = '<div class="embed-responsive embed-responsive-16by9">' . '<iframe class="embed-responsive-item" src="//www.youtube-nocookie.com/embed/' . $preview->filename . '"></iframe>' . '</div><br/>' . $noti->texto; $noti->editada = TRUE; $noti->save(); } } else { if ($preview->type == 'vimeo') { $imagen = $preview->preview(); if ($imagen) { $noti->preview = $imagen; $noti->texto = '<div class="embed-responsive embed-responsive-16by9">' . '<iframe class="embed-responsive-item" src="//player.vimeo.com/video/' . $preview->filename . '"></iframe>' . '</div><br/>' . $noti->texto; $noti->editada = TRUE; $noti->save(); } } } } } else { if (is_null($noti->preview)) { /// exploramos la página para buscar imágenes $html = $preview->curl_download($noti->url); $txt_adicional = FALSE; $urls = array(); if (preg_match_all('@<meta property="og:image" content="([^"]+)@', $html, $urls)) { foreach ($urls[1] as $url) { $preview->load($url); if ($preview->type and stripos($url, 'logo') === FALSE and $noti->preview != $preview->link) { $noti->preview = $preview->preview(); $noti->save(); $this->log[] = 'Encontrada imagen: <a href="' . $preview->link . '" target="_blank">' . $preview->link . '</a>'; $txt_adicional = "\n<div class='thumbnail'>\n<img src='" . $preview->link . "' alt='" . $noti->titulo . "'/>\n</div>"; break; } } } if (!$preview->type) { /// buscamos vídeos de youtube o vimeo $urls = array(); if (preg_match_all('@((https?://)?([-\\w]+\\.[-\\w\\.]+)+\\w(:\\d+)?(/([-\\w/_\\.]*(\\?\\S+)?)?)*)@', $html, $urls)) { foreach ($urls[0] as $url) { foreach (array('youtube', 'youtu.be', 'vimeo') as $domain) { if (strpos($url, $domain) !== FALSE) { $preview->load($url); if (in_array($preview->type, array('youtube', 'vimeo'))) { $noti->preview = $preview->preview(); $noti->save(); $this->log[] = 'Encontrado vídeo: <a href="' . $preview->link . '" target="_blank">' . $preview->link . '</a>'; if ($preview->type == 'youtube') { $txt_adicional = '<div class="embed-responsive embed-responsive-16by9">' . '<iframe class="embed-responsive-item" src="//www.youtube-nocookie.com/embed/' . $preview->filename . '"></iframe>' . '</div>'; } else { if ($preview->type == 'vimeo') { $txt_adicional = '<div class="embed-responsive embed-responsive-16by9">' . '<iframe class="embed-responsive-item" src="//player.vimeo.com/video/' . $preview->filename . '"></iframe>' . '</div>'; } } break; } } } if ($preview->type) { break; } } } } if ($txt_adicional) { $noti->texto .= $txt_adicional; $noti->save(); } } } } } }