File size: 5,273 Bytes
eaee53a |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 |
<?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>";
?>
|