news_fr / extract_news /4_extract_news_url.php
eckendoerffer's picture
Upload 10 files
eaee53a
raw
history blame
3.48 kB
<?php
/**
* News Links Extractor
*
* Extracts and stores relevant links from local French online news articles.
*
* To use this script, you must have electrolinux/phpquery installed. Install it using Composer with
* the command: composer require electrolinux/phpquery.
*
* @author Guillaume Eckendoerffer
* @date 08-09-2023
*/
// composer require electrolinux/phpquery
require 'vendor/autoload.php';
// Include the database functions
include("functions_mysqli.php");
$path = $_SERVER ['DOCUMENT_ROOT'];
/**
* Get the base domain path from a given URL
*
* @param string $url The input URL
* @return string|false The base domain path or false on failure
*/
function getDomPath( $url ) {
$parsedUrl = parse_url( $url );
if ( !$parsedUrl || !isset( $parsedUrl['scheme'] ) || !isset( $parsedUrl['host'] ) ) {
return false;
}
return "{$parsedUrl['scheme']}://{$parsedUrl['host']}";
}
// Query the database for a news source with no link
$result = mysqli_query( $mysqli, "SELECT `id`, `url`, `media` FROM `base_news` WHERE `link`='0' AND `step` > 0 ORDER BY Rand() LIMIT 1" ); // AND `media` > 214
$row = mysqli_fetch_assoc( $result );
if (!$row) {
exit('No unprocessed news source found.');
}
$id_source = $row["id"];
$url_source = trim( $row["url"] );
$id_media = $row["media"];
$last = date( "U" );
$dom = getDomPath( $url_source );
echo "<span style='font-family: verdana;font-size: .9em;'>$id_source) $url_source => $dom <br /><br />";
// Mark the source as processed
mysqli_query($mysqli, "UPDATE `base_news` SET `link`='1' WHERE `id`='$id_source' LIMIT 1");
// Load the source content
$htmlContent = '';
$file_path = "$path/sources/html_news/$id_source.txt";
if ( file_exists( $file_path ) ){
$htmlContent = implode( '', file( $file_path ) );
} else {
echo "<script type=\"text/javascript\">location.href='" . $_SERVER['REQUEST_URI'] . "';</script>";
}
$doc = \phpQuery::newDocument($htmlContent);
foreach ( $doc['a'] as $link ) {
$url = trim( pq( $link )->attr( 'href' ) );
$url = str_replace( strstr( $url, "#" ), "", $url );
$url = str_replace( strstr( $url, "?utm"), "", $url );
$url = str_replace(strstr( $url, "?xtor"), "", $url );
if ( !substr_count( $url, "//" ) ) {
$url = ( substr( $url, 0, 1 ) != '/' ) ? $dom . '/' . $url : $dom . $url;
} elseif( !substr_count( $url, "http" ) ){
$url = 'https:' . $url;
}
$key = md5( $url );
$nb_base_news = mysqli_return_number($mysqli, "SELECT `id` FROM `base_news` WHERE `url` LIKE '$url%' OR `key_media`='$key' LIMIT 1");
if (substr($url, 0, strlen($dom)) != $dom) {
// Not the source domain
// echo "<span style='color:#000;'># $url </span><br />";
} elseif ($nb_base_news) {
// Already in the database
// echo "<span style='color:#FFA500;'># $url </span><br />";
} elseif (substr_count($url, "-") > 6 && substr_count($url, $dom) && !substr_count( $url, '.jpg' ) && !substr_count( $url, '.png' ) && !substr_count( $url, 'mailto' ) ) {
// Add the link to the database
echo "<span style='color:#008000;'># $url </span><br />";
$insertQuery = "INSERT INTO `base_news` (`id`, `key_media`, `media`, `url`, `step`) VALUES (NULL, '$key', '$id_media', '$url', '0');";
mysqli_query($mysqli, $insertQuery);
}
}
// Refresh the page
echo "<script type=\"text/javascript\">location.href='" . $_SERVER['REQUEST_URI'] . "';</script>";
?>