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
config.yml
.lastrun
.seen.yml

35
bot.php
View file

@ -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));
}