Esempio n. 1
0
 /**
  * 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();
                     }
                 }
             }
         }
     }
 }