File size: 3,480 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
<?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>";

?>