diff --git a/bot.php b/bot.php index c743833..b10c31a 100644 --- a/bot.php +++ b/bot.php @@ -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']); - $bitlyProvider->shorten($link); - $link = $link->getShortUrl(); + try { + $bitlyProvider->shorten($link); + $link = $link->getShortUrl(); + } catch (Exception $e) { + echo $e->getMessage()."\n"; + $link = $article['link']; + } $description = $article['description']; $image = NULL; preg_match('/.+?)[\'"].*>/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, ' $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; } }