ifhub-telegram/bot.php

153 lines
4.4 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;
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;
}
return $buff;
}
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'])
);
$telegram = new Longman\TelegramBot\Telegram(
$config['TELEGRAM_API_KEY'],
$config['TELEGRAM_BOT_NAME']
);
2017-06-16 19:03:07 +00:00
$mastodon = new MastodonClient();
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']);
try {
$bitlyProvider->shorten($link);
$link = $link->getShortUrl();
} catch (Exception $e) {
echo $e->getMessage()."\n";
$link = $article['link'];
}
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) - strlen($title) - 5;
$description = "$title\n\n".ellipse($description, $limit);
$description .= "\n$link";
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,
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->status_post($description);
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
}
} else {
2017-06-16 09:33:01 +00:00
echo $description."\n";
2017-05-16 06:42:20 +00:00
}
}