You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

162 lines
4.7 KiB

<?php
require_once ("vendor/autoload.php");
use Symfony\Component\Yaml\Yaml;
use Mremi\UrlShortener\Model\Link;
use Mremi\UrlShortener\Provider\Bitly\BitlyProvider;
use Mremi\UrlShortener\Provider\Bitly\GenericAccessTokenAuthenticator;
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');
}
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='[...]')
{
$buff = $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;
// 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;
}
return $buff;
}
function formatdsc($description) {
global $pandoc;
$description = strip_tags($description, 'img');
$description = $pandoc->runWith($description, [
'from' => 'html',
'to' => 'plain',
'wrap' => 'none'
]);
$description = str_replace('Читать дальше', '', $description);
$description = trim($description);
return $description;
}
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/');
$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();
$bitlyProvider = new BitlyProvider(
new GenericAccessTokenAuthenticator($config['BITLY_TOKEN'])
);
$telegram = new Longman\TelegramBot\Telegram(
$config['TELEGRAM_API_KEY'],
$config['TELEGRAM_BOT_NAME']
);
$mastodon = new MastodonClient(new Client);
foreach ($articles as $article) {
if (strtotime($article['pubDate']) <= $lastrun) {
continue;
}
$title = $article['title'];
$link = new Link;
$link->setLongUrl($article['link']);
try {
$bitlyProvider->shorten($link);
$link = $link->getShortUrl();
} catch (Exception $e) {
echo $e->getMessage()."\n";
$link = $article['link'];
}
$description = $article['description'];
$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";
if (!$config['DRY_RUN']) {
if ($config['TELEGRAM'] === true) {
try {
$result = \Longman\TelegramBot\Request::sendMessage([
'chat_id' => $config['TELEGRAM_CHAT_ID'],
'text' => $description,
'parse_mode' => 'Markdown'
]);
unset($tdescription);
if (!$config['DRY_RUN']) {
file_put_contents('.lastrun', time());
}
} catch (Longman\TelegramBot\Exception\TelegramException $e) {
echo $e;
}
}
if ($config['MASTODON'] === true) {
$mastodon->domain($config['MASTODON_SERVER'])->token($config['MASTODON_ACCESS_TOKEN']);
/*
if ($image) {
download('https://ifhub.club'.$image, './'.basename($image));
$attachment = $mastodon->post('/media', [
'file' => file_get_contents('./'.basename($image))
]);
var_dump($attachment);
unlink('./'.basename($image));
$mdescription .= $attachment->url;
}*/
$mastodon->createStatus($description, [
'language' => 'ru'
]);
if (!$config['DRY_RUN']) {
file_put_contents('.lastrun', time());
}
}
} else {
echo $description."\n";
}
}