1, CURLOPT_URL => $url, )); $resp = curl_exec($curl); curl_close($curl); return $resp; } function ellipse($str,$n_chars,$crop_str=' [...]') { $str = trim($str); $buff = strip_tags($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; } return $buff; } $string = get_text('https://ifhub.club/rss/new/'); $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 = Mastodon\session( $config['MASTODON_SERVER'], $config['MASTODON_CLIENT_ID'], $config['MASTODON_SECRET'], [ 'scope' => 'read write', ] ); foreach ($articles as $article) { if (strtotime($article['pubDate']) <= $lastrun) { continue; } $title = $article['title']; $link = new Link; $link->setLongUrl($article['link']); $bitlyProvider->shorten($link); $link = $link->getShortUrl(); $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); 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, 'parse_mode' => 'Markdown' ]); unset($tdescription); } catch (Longman\TelegramBot\Exception\TelegramException $e) { echo $e; } } if ($config['MASTODON'] === true) { $mdescription = "$title\n\n".ellipse($description, 400); $mdescription .= ": $link"; if ($image) { $attachment = Mastodon\request($mastodon, 'POST', '/api/v1/media', [ 'file' => $image ], Mastodon\Entity\Account::class); $mdescription .= $attachment->url; } echo $mdescription; //$mastodon->postStatus(Mastodon\toot($mdescription)); } } else { echo $description."\n"; echo $link; } } if (!$config['DRY_RUN']) { file_put_contents('.lastrun', time()); }