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 trim($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(); try { $telegram = new Longman\TelegramBot\Telegram( $config['TELEGRAM_API_KEY'], $config['TELEGRAM_BOT_NAME'] ); } catch (\Exception $e) { if (!$config['DRY_RUN']) { echo 'Wrong Telegram API key.'; return; }; } try { $oAuth = new Colorfield\Mastodon\MastodonOAuth('IFHub.club', $config['MASTODON_SERVER']); $oAuth->config->setAuthorizationCode($config['MASTODON_ACCESS_TOKEN']); $mastodon = new Colorfield\Mastodon\MastodonAPI($oAuth->config); } catch (\Exception $e) { echo 'WARNING: could not start Mastodon client'; } foreach ($articles as $article) { if (in_array($article['link'], $seen)) { continue; } $title = $article['title']; $link_mastodon = $article['link']; $description = $article['description']; $image = NULL; preg_match('/.+?)[\'"].*>/i', $description, $image); if (isset($image[1])) { $image = $image[1]; } $long_description = $description; $description = substr( $description, 0, strpos($description, ' $config['TELEGRAM_CHAT_ID'], 'text' => $description, 'parse_mode' => 'Markdown' ]); if (!$config['DRY_RUN']) { file_put_contents('.lastrun', time()); } } catch (Longman\TelegramBot\Exception\TelegramException $e) { echo $e; } } if ($config['MASTODON'] === true && isset($mastodon)) { /* 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->post('statuses', [ 'content' => $description, 'language' => 'ru', ]); $seen[] = $article['link']; } if (!$config['DRY_RUN'] && !empty($config['INDEXNOW'])) { $client = new \GuzzleHttp\Client(); $client->request('GET', 'https://www.bing.com/indexnow?url='.$link.'&key='.$config['INDEXNOW']); } } else { echo $description."\n---\n"; } } if (!$config['DRY_RUN']) { file_put_contents('.seen.yml', Yaml::dump($seen)); }