news_fr / extract_news /1_extract_rss.php
eckendoerffer's picture
Upload 10 files
eaee53a
raw
history blame
5.27 kB
<?php
/**
* RSS Article URL Extractor
*
* This script is designed to fetch and extract article URLs from the RSS feeds of various French media outlets.
* The sources of these RSS feeds are stored in the `base_media` table of the database.
* For each source, the script retrieves the RSS content, processes each entry, and stores the article URLs
* into the database for further processing or analysis.
*
* @author Guillaume Eckendoerffer
* @date 07-09-2023
*/
// Include the database functions
include( "functions_mysqli.php" );
// Initialize global variables
$last = date("U");
$path = $_SERVER['DOCUMENT_ROOT'];
/**
* Fetches the latest URL from the database.
*
* @param mysqli $mysqli The mysqli connection object.
* @return array Associative array containing the ID, URL, and the last update time.
*/
function fetchLatestURL( $mysqli ) {
$result = mysqli_query( $mysqli, "SELECT `id`, `url`, `last` FROM `base_media` ORDER By `last` ASC LIMIT 1" );
return mysqli_fetch_assoc( $result );
}
/**
* Updates the last fetched timestamp for the given source.
*
* @param mysqli $mysqli The mysqli connection object.
* @param int $id_source The ID of the source to update.
* @param int $last The new timestamp.
*/
function updateLastFetched( $mysqli, $id_source, $last ) {
mysqli_query( $mysqli, "UPDATE `base_media` SET `last`='$last' WHERE `id`='$id_source' LIMIT 1" );
}
/**
* Fetches the content from a given URL.
*
* @param string $url The URL to fetch.
* @return string The content of the fetched URL.
*/
function fetchURLContent( $url) {
$data = file_get_contents( trim( $url ) );
return $data;
}
/**
* Saves the given content to a file.
*
* @param string $path The path where the content should be saved.
* @param string $data The content to save.
*/
function saveToFile( $path, $data ) {
$file = fopen( $path, "w" );
fwrite( $file, $data );
fclose( $file );
}
/**
* Processes the fetched RSS content and updates the database accordingly.
*
* @param mysqli $mysqli The mysqli connection object.
* @param string $content The RSS content.
* @param int $id_source The ID of the source.
*/
function processRSSContent( $mysqli, $content, $id_source ) {
$a = new SimpleXMLElement( $content );
$nb = 0;
// First attempt: Process each item in the channel
foreach ( $a->channel->item as $entry ) {
$media = $entry->children( 'media', TRUE );
$attributes = $media->content->attributes();
$src = $attributes['url'];
$url = trim( $entry->link );
$url = str_replace(strstr( $url, "#"), "", $url );
$url = str_replace(strstr( $url, "?utm"), "", $url );
$url = str_replace(strstr( $url, "?xtor"), "", $url );
$key = md5( $url );
$txt = addslashes( strip_tags( html_entity_decode( $entry->description ) ) );
$title = addslashes( strip_tags( html_entity_decode( $entry->title ) ) );
$nb++;
$nb_base = mysqli_return_number( $mysqli, "SELECT `id` FROM `base_news` WHERE `key_media`='$key' OR `url` LIKE '$url%' LIMIT 1" );
if ( trim( $url ) != '' && trim( $title ) != '' && $nb_base == 0 ) {
mysqli_query( $mysqli, "INSERT INTO `base_news` (`id`, `key_media`, `media`, `url`, `link`, `step`) VALUES (NULL, '$key', '$id_source', '$url', '0', '0');" );
$id_inserted = $mysqli->insert_id;
mysqli_query( $mysqli, "UPDATE `base_news` SET `title`='$title' WHERE `id`='$id_inserted' LIMIT 1" );
mysqli_query( $mysqli, "UPDATE `base_news` SET `txt_clean`='$txt' WHERE `id`='$id_inserted' LIMIT 1" );
}
}
// Second attempt: If no entries were processed in the first attempt, process the top-level entries
if ( !$nb ) {
foreach ( $a as $entry ) {
$url = $entry->loc;
$url = str_replace( strstr( $url, "#" ), "", $url );
$url = str_replace(strstr( $url, "?utm"), "", $url );
$key = md5( $url );
$nb++;
$nb_base = mysqli_return_number( $mysqli, "SELECT `id` FROM `base_news` WHERE `key_media`='$key' OR `url` LIKE '$url%' LIMIT 1" );
if ( trim( $url ) != '' && $nb_base == 0 ) {
mysqli_query( $mysqli, "INSERT INTO `base_news` (`id`, `key_media`, `media`, `url`, `link`, `step`) VALUES (NULL, '$key', '$id_source', '$url', '0', '0');" );
}
}
}
$nb_base_news = mysqli_return_number( $mysqli, "SELECT `id` FROM `base_news` WHERE `media` = '$id_source'" );
mysqli_query( $mysqli, "UPDATE `base_media` SET `nb`='$nb_base_news' WHERE `id`='$id_source' LIMIT 1" );
}
// Main script execution
$record = fetchLatestURL( $mysqli );
$id_source = $record['id'];
$url = trim( $record['url'] );
$last_update = $record['last'];
echo "$id_source # $url";
// Exit if the last update is too recent < 1h
if ( $last_update + 3600 > $last ) {
exit;
}
updateLastFetched( $mysqli, $id_source, $last );
$data = fetchURLContent( $url );
if( trim( $data )!='' ){
saveToFile( $path . "/sources/rss/" . $id_source . ".txt", $data );
processRSSContent( $mysqli, $data, $id_source );
}
echo "<script type=\"text/javascript\">location.href='" . $_SERVER ['REQUEST_URI'] . "';</script>";
?>