From b0d1a8c33b24bb66e72c110b2a323204a50dfe8d Mon Sep 17 00:00:00 2001 From: Aleksandr Yakovlev Date: Mon, 30 Sep 2024 08:16:23 +0600 Subject: [PATCH] use a YAML list instead of timestamp --- .gitignore | 1 + bot.php | 35 +++++++++++++++++++++-------------- 2 files changed, 22 insertions(+), 14 deletions(-) diff --git a/.gitignore b/.gitignore index 04e5c32..df1d32b 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ vendor config.yml .lastrun +.seen.yml diff --git a/bot.php b/bot.php index a8bc05a..a2b4142 100644 --- a/bot.php +++ b/bot.php @@ -5,9 +5,9 @@ use Revolution\Mastodon\MastodonClient; use GuzzleHttp\Client; $config = Yaml::parse(file_get_contents('config.yml')); -$lastrun = 0; -if (file_exists('.lastrun') && !$config['DRY_RUN']) { - $lastrun = file_get_contents('.lastrun'); +$seen = []; +if (file_exists('.seen.yml')) { + $seen = Yaml::parse(file_get_contents('.seen.yml')); } function get_text($url) { @@ -76,14 +76,21 @@ $service->elementMap = [ $articles = $service->parse($string)[0]['value']; unset($string); $pandoc = new \Pandoc\Pandoc(); +try { $telegram = new Longman\TelegramBot\Telegram( $config['TELEGRAM_API_KEY'], $config['TELEGRAM_BOT_NAME'] ); +} catch (\Exception $e) { + if (!$config['DRY_RUN']) { + echo 'Wrong Telegram API key.'; + return; + }; +} $mastodon = new MastodonClient(new Client); foreach ($articles as $article) { - if (strtotime($article['pubDate']) <= $lastrun) { + if (in_array($article['link'], $seen)) { continue; } $title = $article['title']; @@ -104,19 +111,18 @@ foreach ($articles as $article) { } */ $limit = 500 - strlen($link_mastodon) - strlen($title) - 20; + $title = trim($title); $description = "$title\n\n".ellipse($description, $limit); - $description_mastodon = $description."\n$link_mastodon"; - $description_telegram = $description_mastodon; + $description = $description."\n$link_mastodon"; if (!$config['DRY_RUN']) { if ($config['TELEGRAM'] === true) { try { $result = \Longman\TelegramBot\Request::sendMessage([ 'chat_id' => $config['TELEGRAM_CHAT_ID'], - 'text' => $description_telegram, + 'text' => $description, 'parse_mode' => 'Markdown' ]); - unset($tdescription); if (!$config['DRY_RUN']) { file_put_contents('.lastrun', time()); } @@ -136,18 +142,19 @@ foreach ($articles as $article) { unlink('./'.basename($image)); $mdescription .= $attachment->url; }*/ - $mastodon->createStatus($description_mastodon, [ + $mastodon->createStatus($description, [ 'language' => 'ru' ]); - if (!$config['DRY_RUN']) { - file_put_contents('.lastrun', time()); - } + $seen[] = $article['link']; } - if (!empty($config['INDEXNOW'])) { + if (!$config['DRY_RUN'] && !empty($config['INDEXNOW'])) { $client = new \GuzzleHttp\Client(); $client->request('GET', 'https://www.bing.com/indexnow?url='.$link.'&key='.$config['INDEXNOW']); } } else { - echo $description."\n"; + echo $description."\n---\n"; } } +if (!$config['DRY_RUN']) { + file_put_contents('.seen.yml', Yaml::dump($seen)); +}