From 0c231b78653e0b775161a2c23c296bd155f37b92 Mon Sep 17 00:00:00 2001 From: Ceda EI Date: Wed, 4 Apr 2018 10:30:58 +0530 Subject: [PATCH] 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. --- update_chain.php | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/update_chain.php b/update_chain.php index 140a6c7..2ae3f0f 100644 --- a/update_chain.php +++ b/update_chain.php @@ -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(); ?>