Подстройка размера репостов, текст вместо Markdown

Если до тега <cut> описание слишком короткое, то будет репоститься
начало текста самой статьи с ограничением до 500 символов.
This commit is contained in:
Alexander Yakovlev 2017-10-03 22:05:30 +07:00
parent 27cd7e69b1
commit 8fe51ad70d

46
bot.php
View file

@ -23,10 +23,9 @@ function get_text($url) {
return $resp;
}
function ellipse($str,$n_chars,$crop_str=' [...]')
function ellipse($str,$n_chars,$crop_str='[...]')
{
$str = trim($str);
$buff = strip_tags($str);
$buff = $str;
if(strlen($buff) > $n_chars)
{
$cut_index=strpos($buff,' ',$n_chars);
@ -35,6 +34,15 @@ function ellipse($str,$n_chars,$crop_str=' [...]')
return $buff;
}
function formatdsc($description) {
global $pandoc;
$description = strip_tags($description, 'img');
$description = $pandoc->convert($description, "html", "plain");
$description = str_replace('Читать дальше', '', $description);
$description = trim($description);
return $description;
}
function download($url, $outFile) {
$options = array(
CURLOPT_FILE => fopen($outFile, 'w'),
@ -48,7 +56,8 @@ function download($url, $outFile) {
curl_close($ch);
}
$string = get_text('https://ifhub.club/rss/new/');
// $string = get_text('https://ifhub.club/rss/full/');
$string = get_text('http://localhost/rss/full/');
$service = new \Sabre\Xml\Service();
$service->elementMap = [
'{}item' => function(\Sabre\Xml\Reader $reader) {
@ -77,27 +86,36 @@ foreach ($articles as $article) {
$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];
}
$description = strip_tags($description, 'img');
$description = $pandoc->convert($description, "html", "markdown_github");
$description = str_replace('####', '#', $description);
$description = str_replace(' Читать дальше', '', $description);
$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) {
$tdescription = "$title\n\n".$description;
$tdescription .= ": $link";
try {
$result = \Longman\TelegramBot\Request::sendMessage([
'chat_id' => $config['TELEGRAM_CHAT_ID'],
'text' => $tdescription,
'text' => $description,
'parse_mode' => 'Markdown'
]);
unset($tdescription);
@ -110,9 +128,6 @@ foreach ($articles as $article) {
}
if ($config['MASTODON'] === true) {
$mastodon->domain($config['MASTODON_SERVER'])->token($config['MASTODON_ACCESS_TOKEN']);
$limit = 500 - strlen($link) - strlen($title) - 20;
$mdescription = "$title\n\n".ellipse($description, $limit);
$mdescription .= "\n$link";
/*
if ($image) {
download('https://ifhub.club'.$image, './'.basename($image));
@ -123,13 +138,12 @@ foreach ($articles as $article) {
unlink('./'.basename($image));
$mdescription .= $attachment->url;
}*/
$mastodon->status_post($mdescription);
$mastodon->status_post($description);
if (!$config['DRY_RUN']) {
file_put_contents('.lastrun', time());
}
}
} else {
echo $description."\n";
echo $link;
}
}