ifhub-telegram/bot.php

163 lines
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;
2017-06-16 19:03:07 +00:00
use Revolution\Mastodon\MastodonClient;
2018-06-06 08:08:52 +00:00
use GuzzleHttp\Client;
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;
}
function ellipse($str,$n_chars,$crop_str='[...]')
2017-06-16 09:33:01 +00:00
{
$buff = $str;
2017-06-16 09:33:01 +00:00
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;
// then cut for last newline or dot
$cut_index_a=strpos($buff,'.',$n_chars);
$cut_index_b=strpos($buff,PHP_EOL,$n_chars);
$cut_index = max((int) $cut_index_a, (int) $cut_index_b);
$buff=substr($buff,0,($cut_index===false? $n_chars: $cut_index+1)).$crop_str;
2017-06-16 09:33:01 +00:00
}
return trim($buff);
2017-06-16 09:33:01 +00:00
}
function formatdsc($description) {
global $pandoc;
$description = strip_tags($description, 'img');
2017-10-16 08:10:28 +00:00
$description = $pandoc->runWith($description, [
'from' => 'html',
'to' => 'plain',
'wrap' => 'none'
]);
$description = str_replace('Читать дальше', '', $description);
$description = trim($description);
return $description;
}
2017-06-16 19:11:28 +00:00
function download($url, $outFile) {
$options = array(
CURLOPT_FILE => fopen($outFile, 'w'),
CURLOPT_TIMEOUT => 28800, // set this to 8 hours so we dont timeout on big files
CURLOPT_URL => $url
);
$ch = curl_init();
curl_setopt_array($ch, $options);
curl_exec($ch);
curl_close($ch);
}
$string = get_text('https://ifhub.club/rss/full/');
2017-05-16 06:42:20 +00:00
$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'])
);
*/
2017-06-16 09:33:01 +00:00
$telegram = new Longman\TelegramBot\Telegram(
$config['TELEGRAM_API_KEY'],
$config['TELEGRAM_BOT_NAME']
);
2018-06-06 08:08:52 +00:00
$mastodon = new MastodonClient(new Client);
2017-06-16 19:03:07 +00:00
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'];
$link_mastodon = $article['link'];
$link_telegram = 'https://t.me/iv?url='.urlencode($article['link']).'&rhash=2db6db1261cc57';
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];
}
$long_description = $description;
$description = substr( $description, 0, strpos($description, '<a name="cut"'));
$description = formatdsc($description);
/*
if (strlen($description) < 50) { // description is too small
$description = $long_description;
$description = formatdsc($description);
}
*/
$limit = 500 - strlen($link_telegram) - strlen($title) - 20;
$description = "$title\n\n".ellipse($description, $limit);
$description_mastodon = $description."\nСсылка на статью и голосование за рейтинг: $link_mastodon";
2024-09-09 04:06:24 +00:00
$description_telegram = $description.PHP_EOL.$link_mastodon;
2017-05-16 06:42:20 +00:00
if (!$config['DRY_RUN']) {
2017-06-16 09:33:01 +00:00
if ($config['TELEGRAM'] === true) {
try {
$result = \Longman\TelegramBot\Request::sendMessage([
'chat_id' => $config['TELEGRAM_CHAT_ID'],
'text' => $description_telegram,
2017-06-16 09:33:01 +00:00
'parse_mode' => 'Markdown'
]);
unset($tdescription);
2017-07-19 06:46:31 +00:00
if (!$config['DRY_RUN']) {
file_put_contents('.lastrun', time());
}
2017-06-16 09:33:01 +00:00
} catch (Longman\TelegramBot\Exception\TelegramException $e) {
echo $e;
}
}
if ($config['MASTODON'] === true) {
2017-06-16 19:03:07 +00:00
$mastodon->domain($config['MASTODON_SERVER'])->token($config['MASTODON_ACCESS_TOKEN']);
/*
2017-06-16 09:33:01 +00:00
if ($image) {
2017-06-16 19:11:28 +00:00
download('https://ifhub.club'.$image, './'.basename($image));
2017-06-16 19:03:07 +00:00
$attachment = $mastodon->post('/media', [
2017-06-16 19:11:28 +00:00
'file' => file_get_contents('./'.basename($image))
2017-06-16 19:03:07 +00:00
]);
var_dump($attachment);
2017-06-16 19:11:28 +00:00
unlink('./'.basename($image));
2017-06-16 09:33:01 +00:00
$mdescription .= $attachment->url;
2017-06-16 19:03:07 +00:00
}*/
$mastodon->createStatus($description_mastodon, [
'language' => 'ru'
]);
2017-07-19 06:46:31 +00:00
if (!$config['DRY_RUN']) {
file_put_contents('.lastrun', time());
}
2017-05-16 06:42:20 +00:00
}
2023-10-18 07:06:15 +00:00
if (!empty($config['INDEXNOW'])) {
$client = new \GuzzleHttp\Client();
$client->request('GET', 'https://www.bing.com/indexnow?url='.$link.'&key='.$config['INDEXNOW']);
}
2017-05-16 06:42:20 +00:00
} else {
2017-06-16 09:33:01 +00:00
echo $description."\n";
2017-05-16 06:42:20 +00:00
}
}