ifhub-telegram/bot.php

121 lines
3.5 KiB
PHP
Raw Normal View History

2017-05-16 06:42:20 +00:00
<?php
require_once ("vendor/autoload.php");
use Symfony\Component\Yaml\Yaml;
2017-06-16 09:33:01 +00:00
use Mremi\UrlShortener\Model\Link;
use Mremi\UrlShortener\Provider\Bitly\BitlyProvider;
use Mremi\UrlShortener\Provider\Bitly\GenericAccessTokenAuthenticator;
use Baguette\Mastodon;
2017-05-16 06:42:20 +00:00
$config = Yaml::parse(file_get_contents('config.yml'));
2017-05-16 06:52:32 +00:00
$lastrun = 0;
2017-06-16 09:33:01 +00:00
if (file_exists('.lastrun') && !$config['DRY_RUN']) {
2017-05-16 06:52:32 +00:00
$lastrun = file_get_contents('.lastrun');
}
2017-05-16 06:42:20 +00:00
function get_text($url) {
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => $url,
));
$resp = curl_exec($curl);
curl_close($curl);
return $resp;
}
2017-06-16 09:33:01 +00:00
function ellipse($str,$n_chars,$crop_str=' [...]')
{
$str = trim($str);
$buff = strip_tags($str);
if(strlen($buff) > $n_chars)
{
$cut_index=strpos($buff,' ',$n_chars);
$buff=substr($buff,0,($cut_index===false? $n_chars: $cut_index+1)).$crop_str;
}
return $buff;
}
2017-05-16 06:42:20 +00:00
$string = get_text('https://ifhub.club/rss/new/');
$service = new \Sabre\Xml\Service();
$service->elementMap = [
'{}item' => function(\Sabre\Xml\Reader $reader) {
return \Sabre\Xml\Deserializer\keyValue($reader, '');
},
'{}channel' => function(\Sabre\Xml\Reader $reader) {
return \Sabre\Xml\Deserializer\repeatingElements($reader, '{}item');
},
];
$articles = $service->parse($string)[0]['value'];
unset($string);
$pandoc = new \Pandoc\Pandoc();
2017-06-16 09:33:01 +00:00
$bitlyProvider = new BitlyProvider(
new GenericAccessTokenAuthenticator($config['BITLY_TOKEN'])
);
$telegram = new Longman\TelegramBot\Telegram(
$config['TELEGRAM_API_KEY'],
$config['TELEGRAM_BOT_NAME']
);
$mastodon = Mastodon\session(
$config['MASTODON_SERVER'],
$config['MASTODON_CLIENT_ID'],
$config['MASTODON_SECRET'],
[
'scope' => 'read write',
]
);
2017-05-16 06:42:20 +00:00
foreach ($articles as $article) {
2017-05-16 06:52:32 +00:00
if (strtotime($article['pubDate']) <= $lastrun) {
continue;
}
2017-05-16 06:42:20 +00:00
$title = $article['title'];
2017-06-16 09:33:01 +00:00
$link = new Link;
$link->setLongUrl($article['link']);
$bitlyProvider->shorten($link);
$link = $link->getShortUrl();
2017-05-16 06:42:20 +00:00
$description = $article['description'];
2017-06-16 09:33:01 +00:00
$image = NULL;
preg_match('/<img.+src=[\'"](?P<src>.+?)[\'"].*>/i', $description, $image);
if (isset($image[1])) {
$image = $image[1];
}
$description = strip_tags($description, 'img');
2017-05-16 06:42:20 +00:00
$description = $pandoc->convert($description, "html", "markdown_github");
2017-05-19 07:48:48 +00:00
$description = str_replace('####', '#', $description);
2017-06-16 09:33:01 +00:00
$description = str_replace(' Читать дальше', '', $description);
2017-05-16 06:42:20 +00:00
if (!$config['DRY_RUN']) {
2017-06-16 09:33:01 +00:00
if ($config['TELEGRAM'] === true) {
$tdescription = "$title\n\n".$description;
$tdescription .= ": $link";
try {
$result = \Longman\TelegramBot\Request::sendMessage([
'chat_id' => $config['TELEGRAM_CHAT_ID'],
'text' => $tdescription,
'parse_mode' => 'Markdown'
]);
unset($tdescription);
} catch (Longman\TelegramBot\Exception\TelegramException $e) {
echo $e;
}
}
if ($config['MASTODON'] === true) {
$mdescription = "$title\n\n".ellipse($description, 400);
$mdescription .= ": $link";
if ($image) {
$attachment = Mastodon\request($mastodon, 'POST', '/api/v1/media', [
'file' => $image
], Mastodon\Entity\Account::class);
$mdescription .= $attachment->url;
}
echo $mdescription;
//$mastodon->postStatus(Mastodon\toot($mdescription));
2017-05-16 06:42:20 +00:00
}
} else {
2017-06-16 09:33:01 +00:00
echo $description."\n";
echo $link;
2017-05-16 06:42:20 +00:00
}
}
2017-06-16 09:33:01 +00:00
if (!$config['DRY_RUN']) {
file_put_contents('.lastrun', time());
}