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

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

50
bot.php
View file

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