use a YAML list instead of timestamp

This commit is contained in:
Aleksandr Yakovlev 2024-09-30 08:16:23 +06:00
parent f9f57ebf0c
commit b0d1a8c33b
No known key found for this signature in database
GPG key ID: 8269E24B4008E32A
2 changed files with 22 additions and 14 deletions

1
.gitignore vendored
View file

@ -1,3 +1,4 @@
vendor vendor
config.yml config.yml
.lastrun .lastrun
.seen.yml

35
bot.php
View file

@ -5,9 +5,9 @@ use Revolution\Mastodon\MastodonClient;
use GuzzleHttp\Client; use GuzzleHttp\Client;
$config = Yaml::parse(file_get_contents('config.yml')); $config = Yaml::parse(file_get_contents('config.yml'));
$lastrun = 0; $seen = [];
if (file_exists('.lastrun') && !$config['DRY_RUN']) { if (file_exists('.seen.yml')) {
$lastrun = file_get_contents('.lastrun'); $seen = Yaml::parse(file_get_contents('.seen.yml'));
} }
function get_text($url) { function get_text($url) {
@ -76,14 +76,21 @@ $service->elementMap = [
$articles = $service->parse($string)[0]['value']; $articles = $service->parse($string)[0]['value'];
unset($string); unset($string);
$pandoc = new \Pandoc\Pandoc(); $pandoc = new \Pandoc\Pandoc();
try {
$telegram = new Longman\TelegramBot\Telegram( $telegram = new Longman\TelegramBot\Telegram(
$config['TELEGRAM_API_KEY'], $config['TELEGRAM_API_KEY'],
$config['TELEGRAM_BOT_NAME'] $config['TELEGRAM_BOT_NAME']
); );
} catch (\Exception $e) {
if (!$config['DRY_RUN']) {
echo 'Wrong Telegram API key.';
return;
};
}
$mastodon = new MastodonClient(new Client); $mastodon = new MastodonClient(new Client);
foreach ($articles as $article) { foreach ($articles as $article) {
if (strtotime($article['pubDate']) <= $lastrun) { if (in_array($article['link'], $seen)) {
continue; continue;
} }
$title = $article['title']; $title = $article['title'];
@ -104,19 +111,18 @@ foreach ($articles as $article) {
} }
*/ */
$limit = 500 - strlen($link_mastodon) - strlen($title) - 20; $limit = 500 - strlen($link_mastodon) - strlen($title) - 20;
$title = trim($title);
$description = "$title\n\n".ellipse($description, $limit); $description = "$title\n\n".ellipse($description, $limit);
$description_mastodon = $description."\n$link_mastodon"; $description = $description."\n$link_mastodon";
$description_telegram = $description_mastodon;
if (!$config['DRY_RUN']) { if (!$config['DRY_RUN']) {
if ($config['TELEGRAM'] === true) { if ($config['TELEGRAM'] === true) {
try { try {
$result = \Longman\TelegramBot\Request::sendMessage([ $result = \Longman\TelegramBot\Request::sendMessage([
'chat_id' => $config['TELEGRAM_CHAT_ID'], 'chat_id' => $config['TELEGRAM_CHAT_ID'],
'text' => $description_telegram, 'text' => $description,
'parse_mode' => 'Markdown' 'parse_mode' => 'Markdown'
]); ]);
unset($tdescription);
if (!$config['DRY_RUN']) { if (!$config['DRY_RUN']) {
file_put_contents('.lastrun', time()); file_put_contents('.lastrun', time());
} }
@ -136,18 +142,19 @@ foreach ($articles as $article) {
unlink('./'.basename($image)); unlink('./'.basename($image));
$mdescription .= $attachment->url; $mdescription .= $attachment->url;
}*/ }*/
$mastodon->createStatus($description_mastodon, [ $mastodon->createStatus($description, [
'language' => 'ru' 'language' => 'ru'
]); ]);
if (!$config['DRY_RUN']) { $seen[] = $article['link'];
file_put_contents('.lastrun', time());
}
} }
if (!empty($config['INDEXNOW'])) { if (!$config['DRY_RUN'] && !empty($config['INDEXNOW'])) {
$client = new \GuzzleHttp\Client(); $client = new \GuzzleHttp\Client();
$client->request('GET', 'https://www.bing.com/indexnow?url='.$link.'&key='.$config['INDEXNOW']); $client->request('GET', 'https://www.bing.com/indexnow?url='.$link.'&key='.$config['INDEXNOW']);
} }
} else { } else {
echo $description."\n"; echo $description."\n---\n";
} }
} }
if (!$config['DRY_RUN']) {
file_put_contents('.seen.yml', Yaml::dump($seen));
}