Add update_user_by_username function

This function reads the latest user data from t.me and checks whom it links to. It then updates the database accordingly.
This commit is contained in:
Ceda EI 2018-04-04 10:30:58 +05:30
parent 90f69802cb
commit 0c231b7865
1 changed files with 20 additions and 0 deletions

View File

@ -83,5 +83,25 @@ function chain_to_string($chain) {
return $string;
}
function update_user_by_username($username) {
global $conn;
$html = file_get_contents("https://t.me/" . $username);
$dom = new domDocument;
$dom->loadHTML($html);
$dom->preserveWhiteSpace = false;
$xpath = new \DOMXPath($dom);
foreach ($xpath->query("descendant-or-self::div[@class and contains(concat(' ', normalize-space(@class), ' '), ' tgme_page_description ')]/a") as $node){
$username = preg_replace('/^@/', '', $node->nodeValue);
$query = "SELECT user_id FROM users WHERE username = " . $username ;
$result = $conn->query($query);
if ($result->num_rows > 0) {
$row = $result->fetch_assoc();
$query = "UPDATE users SET follows = " . $row['user_id'] . " WHERE username = " . $username . ";" ;
$conn->query($query);
return;
}
}
}
$conn->close();
?>