|
<?php |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
include( "functions_mysqli.php" ); |
|
|
|
|
|
$last = date("U"); |
|
$path = $_SERVER['DOCUMENT_ROOT']; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 ); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function updateLastFetched( $mysqli, $id_source, $last ) { |
|
mysqli_query( $mysqli, "UPDATE `base_media` SET `last`='$last' WHERE `id`='$id_source' LIMIT 1" ); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function fetchURLContent( $url) { |
|
$data = file_get_contents( trim( $url ) ); |
|
return $data; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function saveToFile( $path, $data ) { |
|
$file = fopen( $path, "w" ); |
|
fwrite( $file, $data ); |
|
fclose( $file ); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function processRSSContent( $mysqli, $content, $id_source ) { |
|
$a = new SimpleXMLElement( $content ); |
|
$nb = 0; |
|
|
|
|
|
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" ); |
|
} |
|
} |
|
|
|
|
|
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" ); |
|
} |
|
|
|
|
|
|
|
$record = fetchLatestURL( $mysqli ); |
|
$id_source = $record['id']; |
|
$url = trim( $record['url'] ); |
|
$last_update = $record['last']; |
|
|
|
echo "$id_source # $url"; |
|
|
|
|
|
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>"; |
|
|
|
?> |
|
|
|
|