/** | |
* News Article Source Fetcher | |
* | |
* This script is designed to extract the content of news articles from various French media sources. | |
* The URLs of these articles are retrieved from the `base_news` table, where articles marked with | |
* a `step` value of '0' are pending extraction. | |
* | |
* Once extracted, the content of each article is saved locally for further processing. This separation | |
* of content fetching and processing is intentional to optimize resource management. | |
* | |
* The script operates in batches, processing a defined number of entries (`NB_BY_STEP`) at a time. | |
* After extraction, the `step` value of the processed articles is updated to '1' to indicate completion. | |
* | |
* For performance monitoring, the script outputs the processed article IDs, URLs, and calculates the average | |
* processing time per article. | |
* | |
* @author Guillaume Eckendoerffer | |
* @date 07-09-2023 | |
*/ | |
// Include the database functions | |
include( "functions_mysqli.php" ); | |
// Initialize global variables | |
const NB_BY_STEP = 20; // nb per steep | |
$last = date( "U" ); | |
$path = $_SERVER['DOCUMENT_ROOT']; | |
// Capture the start time for performance monitoring | |
$time_start = microtime( true ); | |
// Fetch a batch of unprocessed news entries | |
$return =''; | |
$result = mysqli_query( $mysqli, "SELECT `id`, `url` FROM `base_news` WHERE `step`='0' ORDER BY RAND() LIMIT " . NB_BY_STEP ); | |
while ( $row = mysqli_fetch_assoc( $result ) ) { | |
$id_source = $row["id"]; | |
$url = trim( $row["url"] ); | |
$time_start_item = microtime( true ); | |
// Mark the news entry as being processed | |
mysqli_query( $mysqli, "UPDATE `base_news` SET `step`='1' WHERE `id`='$id_source' LIMIT 1" ); | |
// Fetch the content of the news URL | |
$data = file_get_contents( trim( $url ) ); | |
// Save the fetched data to a local file | |
$inF = fopen( "$path/sources/html_news/$id_source.txt", "w" ); | |
fwrite( $inF, $data ); | |
fclose( $inF ); | |
$time_item = number_format( ( microtime( true ) - $time_start_item ) , 3, ',', '' ) . 's'; | |
// Output the ID and URL for monitoring purposes | |
$return .= "($id_source) [$time_item] $url <br />"; | |
} | |
// Fetch the count of news entries that haven't been processed | |
$nb_base = mysqli_return_number( $mysqli, "SELECT `id` FROM `base_news` WHERE `step`='0'" ); | |
// Calculate and display the processing time | |
$time = number_format( ( ( microtime( true ) - $time_start ) / NB_BY_STEP ), 3, ',', '' ) .'s/item'; | |
echo "<head><title>Rem: $nb_base - $time</title></head> $return"; | |
// If no more entries are found, exit, else move to the next processing step | |
if ( !isset( $id_source ) ) exit; | |
echo "<script type=\"text/javascript\">location.href='" . $_SERVER ['REQUEST_URI'] . "';</script>"; | |