mirror of
https://github.com/serega404/VodokanalBot.git
synced 2025-04-21 14:30:46 +03:00
Added code
This commit is contained in:
parent
d4d5dfcec1
commit
766356b3bb
9
src/config_example.php
Normal file
9
src/config_example.php
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
<?php
|
||||||
|
return [
|
||||||
|
'url' => 'http://www.tgnvoda.ru/avarii.php',
|
||||||
|
// Telegram
|
||||||
|
'Send_silent' => 'false',
|
||||||
|
'Telegram_token' => 'token',
|
||||||
|
'Telegram_channel' => '@channel'
|
||||||
|
];
|
||||||
|
?>
|
21
src/libs/simplehtmldom/LICENSE
Normal file
21
src/libs/simplehtmldom/LICENSE
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
MIT License
|
||||||
|
|
||||||
|
Copyright (c) 2019 S.C. Chen, John Schlick, logmanoriginal
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
|
in the Software without restriction, including without limitation the rights
|
||||||
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
copies of the Software, and to permit persons to whom the Software is
|
||||||
|
furnished to do so, subject to the following conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be included in all
|
||||||
|
copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
SOFTWARE.
|
2353
src/libs/simplehtmldom/simple_html_dom.php
Normal file
2353
src/libs/simplehtmldom/simple_html_dom.php
Normal file
File diff suppressed because it is too large
Load Diff
78
src/vodokanal.php
Normal file
78
src/vodokanal.php
Normal file
@ -0,0 +1,78 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
// Connect library
|
||||||
|
include('libs/simplehtmldom/simple_html_dom.php');
|
||||||
|
|
||||||
|
// Load config
|
||||||
|
$config = include('config.php');
|
||||||
|
|
||||||
|
$db = null;
|
||||||
|
if (file_exists("db.json")) {
|
||||||
|
$db = json_decode(file_get_contents('./db.json', true), true);
|
||||||
|
echo print_r($db);
|
||||||
|
} else {
|
||||||
|
echo ("Database not exist\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
// get DOM from URL
|
||||||
|
$html = file_get_html($config['url']);
|
||||||
|
|
||||||
|
$all_elements;
|
||||||
|
|
||||||
|
$dateNow = date("d.m.Y");
|
||||||
|
echo "Date now: " . $dateNow . "\n";
|
||||||
|
|
||||||
|
foreach ($html->find('td[bgcolor="#ffffff"]') as $e) {
|
||||||
|
$date = "";
|
||||||
|
preg_match("/\\d{2}\\.\\d{2}\\.\\d{4}/", $e->plaintext, $date);
|
||||||
|
|
||||||
|
if (strcasecmp(reset($date), $dateNow) == 0) { // get current date posts
|
||||||
|
// remove date from content
|
||||||
|
$content = str_replace(reset($date), "", $e->plaintext);
|
||||||
|
$content = str_replace("\r\n \r\n", "", $content);
|
||||||
|
$content = str_replace("\n\n", "", $content);
|
||||||
|
// add post to array
|
||||||
|
$all_elements[] = $content;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
echo "The number of posts for this day: " . count($all_elements) . "\n";
|
||||||
|
|
||||||
|
if ($db != null) {
|
||||||
|
$diff = array_diff($all_elements, $db);
|
||||||
|
if ($diff != null) {
|
||||||
|
foreach ($diff as $e) {
|
||||||
|
echo "Message sended.\n";
|
||||||
|
send_telegram_message($e);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
echo "No changes.";
|
||||||
|
exit;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
foreach ($all_elements as $e) {
|
||||||
|
echo "Message sended.\n";
|
||||||
|
send_telegram_message($e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// save posts to database
|
||||||
|
$json = json_encode($all_elements, JSON_UNESCAPED_UNICODE);
|
||||||
|
file_put_contents("db.json", $json);
|
||||||
|
|
||||||
|
function send_telegram_message($message)
|
||||||
|
{
|
||||||
|
$message = str_replace(" ", "%20", $message); // fix for telegram
|
||||||
|
global $config;
|
||||||
|
$obj = json_decode(file_get_contents("https://api.telegram.org/bot" . $config['Telegram_token'] . "/sendMessage?chat_id=" . $config['Telegram_channel'] . "&disable_notification=" . $config['Send_silent'] . "&text=" . $message));
|
||||||
|
|
||||||
|
if ($http_response_header[0] != null && $http_response_header[0] == "HTTP/1.1 200 OK") {
|
||||||
|
try {
|
||||||
|
echo "Successfully sented message, mess id: " . $obj->{'result'}->{'message_id'} . "\n";
|
||||||
|
} catch (Exception $e) {
|
||||||
|
echo "Telegram: Не удалось записать id в файл\n";
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
echo "Telegram: Не удачный запрос: " . $http_response_header[0] . "\n";
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user