≤≥≈∝∞∫∑∈∉⊆⊇⊂⊃∪∩∧∨¬∀∃π×⨯⊗→←↑↓↔ ϒϖϑℵ⟩⟨⋅⊗⊄≅∗∉∅⇓⇑⇐↵ℜℑ℘⊕⊥∼∴∪∨∧∠∝" . "∋∈∇∃∀⇔⇒∩¬∞√∑∏∂″′ªº³²¹¾½¼‰¶‡†¿§«»@" ); for ( $i = 0; $i < strlen( $text ); $i++ ) { $char = $text[$i]; if ( !in_array( $char, $dict_char ) ) { $text = str_replace( $char, ' ', $text ); } } return $text; } // Search for the source code of the next record to process if( isset( $_GET["id"] ) AND is_numeric( $_GET["id"] ) ) $req="`id`='" . $_GET["id"] . "'"; else $req="`step`='1' ORDER By `id` "; if( isset( $_GET["media"] ) AND is_numeric( $_GET["media"] ) ) $req="`media`='" . $_GET["media"] . "' ORDER By Rand() "; $result = mysqli_query( $mysqli, "SELECT `id`, `url`, `title`, `txt_clean` FROM `base_news` WHERE $req LIMIT 1" ); while ( $row = mysqli_fetch_assoc( $result ) ) { $id_source = $row["id"]; $next = $id_source + 1; $url = trim( $row["url"] ); $title = trim( html_entity_decode( $row["title"] ) ); $txt_clean = trim( $row["txt_clean"] ); } if( !isset( $id_source ) ) exit; mysqli_query( $mysqli, "UPDATE `base_news` SET `step`='2' WHERE `id`='$id_source' LIMIT 1" ); $htmlContent = ''; $file_path = "$path/sources/html_news/$id_source.txt"; if ( file_exists( $file_path ) ) $htmlContent = implode ( '', file ( $file_path ) ); if( substr_count( $url, '/replay' ) ) $htmlContent =''; // utf8 $current_encoding = mb_detect_encoding( $htmlContent, 'auto' ); try { $htmlContent = ($current_encoding == 'UTF-8') ? $htmlContent : mb_convert_encoding($htmlContent, 'UTF-8', $current_encoding); } catch (ValueError $e) { if( $redirect_enabled ) echo ""; else echo "Error: mb_convert_encoding()"; exit; } $doc = \phpQuery::newDocument( $htmlContent ); // Extracting the title if( trim( $title )=='' ){ $title = html_entity_decode( trim( pq('h1')->text() ) ); mysqli_query( $mysqli, "UPDATE `base_news` SET `title`='" . addslashes( $title ) . "' WHERE `id`='$id_source' LIMIT 1" ); } // Extracting the description if( trim( $txt_clean )=='' ){ $description = pq('meta[name="description"]')->attr('content'); if( trim( $description )=='' ){ $description = pq('meta[name="og:description"]')->attr('content'); } if( trim( $description )!='' ){ $txt_clean = html_entity_decode( trim( $description ) ); $txt_clean = preg_replace( '/\s{2,}/', ' ', $txt_clean ); } } // Extracting the text from source code $content =''; $html =''; // Extracting the text from
tag $color ='#008000'; if( !substr_count( $url, ".rtbf.be" ) ) { foreach ( pq( 'article p' ) as $p ) { $html .= " " . trim( pq( $p )->html() ) . " "; if( substr_count( $url, ".futura-sciences.com" ) ) pq($p)->find('a')->remove(); $content .= " " . trim( html_entity_decode( pq( $p )->text() ) ) . " "; } } // Extracting from

tags after

if( trim( $content )=='' ){ $start = pq('h1:first'); $nextElements = pq($start)->nextAll(); foreach ($nextElements as $element) { if (pq($element)->is('p')) { $start = trim(html_entity_decode(pq($element)->text())); } } foreach ( pq( 'p' ) as $p ) { $content .= " " . trim( html_entity_decode( pq( $p )->text() ) ) . " "; } if( trim( $start )!='' ) $content = strstr( $content, $start ); $color ='#000080'; } // Extracting from

tags if( trim( $content )=='' ){ foreach ( pq( 'p' ) as $p ) { $content .= " " . trim( html_entity_decode( pq( $p )->text() ) ) ; } $color ='#888'; } // Adding a space after punctuation and various deletions $content = str_replace(array("\r", "\n"), ' ', $content); $remove = [ '?', '!', ";", "!", "»", "]",]; foreach ( $remove as $phrase ) { $content = str_replace( $phrase, $phrase . ' ', $content ); } $content = str_replace( html_entity_decode(' '), " ", $content ); // Standardizing spaces $content = preg_replace( '/\s{2,}/', ' ', $content ); // one space, no more $pattern = '/À lire aussi.{1,200}?»/'; $content = preg_replace($pattern, ' ', $content); $pattern = '/Temps de Lecture.{1,20}? Fiche/'; $content = preg_replace($pattern, ' ', $content); $pattern = '/Temps de Lecture.{1,20}? min./'; $content = preg_replace($pattern, ' ', $content); // Media-specific cutting rules if( substr_count( $url, ".elle.fr" ) ){ $pattern = '/Note :.{13,45}? sur 5/'; $content = preg_replace($pattern, ' ', $content); $content = str_replace( strstr( $content, " sur 5 " ), '', $content ); } if( substr_count( $url, ".latribune.fr" ) ){ $content = str_replace( "Partager :", ' ', $content ); } if( substr_count( $url, ".techno-science.net" ) ){ $content = strip_tags( strstr( $htmlContent, "suivez-nous sur" ) ); $content = strstr( $content, ');' ); $content = html_entity_decode( str_replace( strstr( $content, "Référence" ), "", $content ) ); } if( substr_count( $url, ".lexpress.fr" ) ){ $content = strstr( $htmlContent, "article__text" ) ; $content = strstr( $content, ">" ); $content = substr( $content,1,99999 ); $content = html_entity_decode( strip_tags( str_replace( strstr( $content, "

Ecoutez', 'Courrier international', 'Vous avez trouvé une erreur?', 'Il vous reste', 'Partager', "Suivez-nous", 'Newsletter', 'Abonnez-vous', '1€ le premier mois', 'Votre France Bleu', 'Soyez le premier à commenter cet article', 'Pour rester informé(e)', 'Un site du groupe', "Cet article est réservé aux abonnés", "Recevez chaque vendredi l'essentiel", "Suivez toute l'actualité de ZDNet", "Suivez-nous sur les résaux sociaux", ". par ", "Le résumé de la semaine", "ACTUELLEMENT EN KIOSQUE", " L’actualité par la rédaction de", "Gratis onbeperkt", "Débloquez immédiatement cet article", "À voir également", "null null null ", 'Du lundi au vendredi, à 19h', "La rédaction de La Tribune", "Restez toujours informé: suivez-nous sur Google Actualités", "Du lundi au vendredi, votre rendez-vous", "Enregistrer mon nom, mon e-mail", "Mot de passe oublié", '(function', ]; foreach ( $removePhrasesEnd as $phrase ) { $content = str_replace( strstr( $content, $phrase ), "", $content); } // List of phrases to remove $removePhrases = [ "Inscrivez-vous pour recevoir les newsletters de la Rép' dans votre boîte mail", "TF1 INFO", "Sujet TF1 Info", "Sujet JT LCI", "TF1 Info ", "JT 20h WE ", "JT 20h Semaine ", "Source :", "Inscrivez-vous aux newsletters de la RTBF Tous les sujets de l'article", "Pour voir ce contenu, connectez-vous gratuitement", ">> LIRE AUSSI", "À LIRE AUSSI", "A lire aussi >> ", " → À LIRE.", "À voir également", "Image d'illustration -", "Le média de la vie locale ", "Les plus lus.", "Ce live est à présent terminé.", " . -", "[…]", "[.]", "(…)", "(.)", "©" ]; foreach ( $removePhrases as $phrase ) { $content = str_replace( $phrase, ".", $content ); } $content = preg_replace( '/\.{2,}/', '.', $content ); $content = preg_replace( '/\s{2,}/', ' ', $content ); $content = preg_replace( '/-{2,}/', '-', $content ); $content = preg_replace( '/__{2,}/', '_', $content ); // Display echo "($id_source) [$current_encoding] $url

" . "Title: $title

" . "Content: $content"; if( strlen( $content ) != strlen( $content_or ) ){ echo "
$content_or"; // original content } // Formatting the output content for the txt file in the $add variable $content = strip_tags($content); $add = html_entity_decode( $title ) . '. '; if( trim( $txt_clean ) != '' ) $add .= str_replace( "..", ".", html_entity_decode( $txt_clean ) . '. ' ); if( strlen( $content ) > 160 ) $add .= $content . '.'; $add = trim( clean_text( $add ) ); $remove = [ '. .', '..', "?.", "!.", ";.", "??", "? ?",]; foreach ( $remove as $phrase ) { $add = str_replace( $phrase, substr( $phrase,0,1 ). ' ', $add ); } $add = strip_tags( $add ); $add = str_replace( html_entity_decode( ' ' ), " ", $add ); $add = preg_replace( '/&#(x[0-9a-fA-F]+|\d+);/', ' ', $add ); $add = preg_replace( '/&#\d+;/', ' ', $add ); $add = preg_replace( '/[\x{1F534}\x{26A0}\x{1F3C9}\x{1F6A8}\x{1F6D2}\x{1FA82}\x{25B6}\x{2139}\x{2600}-\x{26FF}\x{1F300}-\x{1F5FF}\x{1F600}-\x{1F64F}\x{1F680}-\x{1F6FF}\x{1F700}-\x{1F77F}\x{1F780}-\x{1F7FF}\x{1F800}-\x{1F8FF}\x{1F900}-\x{1F9FF}\x{1FA00}-\x{1FA6F}\x{1FA70}-\x{1FAFF}]/u', ' ', $add ); $add = preg_replace( '/\s{2,}/', ' ', $add ); $replacements = array( "À" => "À", "Â" => "Â", "Ã" => "Ã", "Ä" => "Ä", "Â" => "Â", "Ã…" => "Å", "á" => "á", "â" => "â", "ã" => "ã", "ä" => "ä", "Ã¥" => "å", "Ã’" => "Ò", "Ó" => "Ó", "Ô" => "Ô", "Õ" => "Õ", "Ö" => "Ö", "Ø" => "Ø", "ò" => "ò", "ó" => "ó", "ô" => "ô", "õ" => "õ", "ö" => "ö", "ø" => "ø", "È" => "È", "É" => "É", "Ê" => "Ê", "Ë" => "Ë", "è" => "è", "é" => "é", "ê" => "ê", "ë" => "ë", "Ç" => "Ç", "ç" => "ç", "ÃŒ" => "Ì", "ÃŽ" => "Î", "ì" => "ì", "í" => "í", "î" => "î", "ï" => "ï", "Ù" => "Ù", "Ú" => "Ú", "Û" => "Û", "Ãœ" => "Ü", "ù" => "ù", "ú" => "ú", "û" => "û", "ü" => "ü", "ÿ" => "ÿ", "Ñ" => "Ñ", "ñ" => "ñ", "Á" => "Á", "Í" => "Í", "à " => "à", '’'=>'\'', "«" => "«", '»'=>'»'); $add = str_replace( array_keys( $replacements ), array_values( $replacements ), $add ); // Additional display for debugging and testing if( $debug ){ if( trim( $content )=='' ) { echo "
html:
$html " . htmlentities( $html ) . "
" . htmlentities( $doc->text() ) .'
' . 'Source lenght: ' . strlen( $htmlContent ); } $test_char =""; foreach ( str_split( $add ) as $char) { $test_char .= $char . "(" . bin2hex($char). ") "; } echo '
hexadecimal code for each character from $add var:
' . $test_char; } else { if( $id_source%10000 === 0 ) mysqli_query( $mysqli, "OPTIMIZE TABLE `base_news`;" ); } // Saving the title + description + text in a text file $key_title = md5( $title ); $nb_base = mysqli_return_number( $mysqli, "SELECT `id` FROM `base_news` WHERE `key_title`='$key_title' LIMIT 1" ); if( isset( $_GET["id"] ) OR isset( $_GET["media"] ) OR ( strlen( $content ) > 200 AND !$nb_base ) ){ mysqli_query( $mysqli, "UPDATE `base_news` SET `key_title` = '$key_title' WHERE `id`='$id_source' LIMIT 1" ); $bom = "\xEF\xBB\xBF"; file_put_contents( "$path/sources/txt_news/$id_source.txt", $bom . $add ); } $execution_time = microtime(true) - $time_start; echo"
execution time: $execution_time"; if( $id_source%100 === 0 ) sleep(1); // throttling navigation to prevent the browser from hanging. if( $redirect_enabled ) echo ""; ?>