diff --git a/bot.php b/bot.php index 79d0644..1c6584c 100644 --- a/bot.php +++ b/bot.php @@ -1,10 +1,14 @@ $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 = [ @@ -32,32 +48,73 @@ $service->elementMap = [ $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 = $article['link']; + $link = new Link; + $link->setLongUrl($article['link']); + $bitlyProvider->shorten($link); + $link = $link->getShortUrl(); $description = $article['description']; - // $description = strip_tags($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 = "$title\n\n".$description; - $description .= ": $link"; + $description = str_replace(' Читать дальше', '', $description); if (!$config['DRY_RUN']) { - try { - $telegram = new Longman\TelegramBot\Telegram($config['API_KEY'], $config['BOT_NAME']); - $result = \Longman\TelegramBot\Request::sendMessage([ - 'chat_id' => $config['CHAT_ID'], - 'text' => $description, - 'parse_mode' => 'Markdown' - ]); - } catch (Longman\TelegramBot\Exception\TelegramException $e) { - echo $e; + 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; + echo $description."\n"; + echo $link; } } -file_put_contents('.lastrun', time()); +if (!$config['DRY_RUN']) { + file_put_contents('.lastrun', time()); +} diff --git a/composer.json b/composer.json index ca4c926..a62a29f 100644 --- a/composer.json +++ b/composer.json @@ -3,6 +3,7 @@ "longman/telegram-bot": "^0.44.1", "sabre/xml": "^2.0", "ryakad/pandoc-php": "^1.0", - "symfony/yaml": "^3.2" + "symfony/yaml": "^3.2", + "mremi/url-shortener": "^2.1" } } diff --git a/composer.lock b/composer.lock index 3967747..2d2637e 100644 --- a/composer.lock +++ b/composer.lock @@ -4,8 +4,8 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", "This file is @generated automatically" ], - "hash": "3b9571489db96ed50e67fe5c3156da05", - "content-hash": "a72d9afdda2b504f52dfc273b73835c5", + "hash": "71302a13e71405a5c30d66ccb32c62da", + "content-hash": "a474f6360fb73f8cd1cfc26729f5c03a", "packages": [ { "name": "guzzlehttp/guzzle", @@ -316,6 +316,55 @@ ], "time": "2017-03-13 07:08:03" }, + { + "name": "mremi/url-shortener", + "version": "v2.1.0", + "source": { + "type": "git", + "url": "https://github.com/mremi/UrlShortener.git", + "reference": "ad47697e899d30e167c45c5e937db421cf768dc1" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/mremi/UrlShortener/zipball/ad47697e899d30e167c45c5e937db421cf768dc1", + "reference": "ad47697e899d30e167c45c5e937db421cf768dc1", + "shasum": "" + }, + "require": { + "guzzlehttp/guzzle": "^6.0", + "php": "^5.5 || ^7.0", + "symfony/console": "^2.7 || ^3.1" + }, + "require-dev": { + "fabpot/php-cs-fixer": "~0.1 || ~1.0", + "phpunit/phpunit": "~4.8", + "symfony/phpunit-bridge": "~2.7 || ~3.0" + }, + "type": "library", + "autoload": { + "psr-4": { + "Mremi\\UrlShortener\\": "src/Mremi/UrlShortener" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Rémi Marseille", + "email": "marseille.remi@gmail.com" + } + ], + "description": "A PHP5 library using API to shorten/expand URL", + "homepage": "https://github.com/mremi/UrlShortener", + "keywords": [ + "api", + "shortener", + "url" + ], + "time": "2017-03-29 18:03:19" + }, { "name": "psr/http-message", "version": "1.0.1", @@ -413,6 +462,71 @@ ], "time": "2016-10-10 12:19:37" }, + { + "name": "respect/validation", + "version": "1.1.12", + "source": { + "type": "git", + "url": "https://github.com/Respect/Validation.git", + "reference": "5ab87d1dd932872f6670136a513f72ff9ea41c67" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/Respect/Validation/zipball/5ab87d1dd932872f6670136a513f72ff9ea41c67", + "reference": "5ab87d1dd932872f6670136a513f72ff9ea41c67", + "shasum": "" + }, + "require": { + "php": ">=5.4", + "symfony/polyfill-mbstring": "^1.2" + }, + "require-dev": { + "egulias/email-validator": "~1.2", + "malkusch/bav": "~1.0", + "mikey179/vfsstream": "^1.5", + "phpunit/phpunit": "~4.0", + "symfony/validator": "~2.6.9", + "zendframework/zend-validator": "~2.3" + }, + "suggest": { + "egulias/email-validator": "Strict (RFC compliant) email validation", + "ext-bcmath": "Arbitrary Precision Mathematics", + "ext-mbstring": "Multibyte String Functions", + "fabpot/php-cs-fixer": "Fix PSR2 and other coding style issues", + "malkusch/bav": "German bank account validation", + "symfony/validator": "Use Symfony validator through Respect\\Validation", + "zendframework/zend-validator": "Use Zend Framework validator through Respect\\Validation" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.1-dev" + } + }, + "autoload": { + "psr-4": { + "Respect\\Validation\\": "library/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD Style" + ], + "authors": [ + { + "name": "Respect/Validation Contributors", + "homepage": "https://github.com/Respect/Validation/graphs/contributors" + } + ], + "description": "The most awesome validation engine ever created for PHP", + "homepage": "http://respect.github.io/Validation/", + "keywords": [ + "respect", + "validation", + "validator" + ], + "time": "2017-03-14 09:44:11" + }, { "name": "ryakad/pandoc-php", "version": "v1.0.0", @@ -553,6 +667,190 @@ ], "time": "2016-11-16 00:41:01" }, + { + "name": "symfony/console", + "version": "v3.3.2", + "source": { + "type": "git", + "url": "https://github.com/symfony/console.git", + "reference": "70d2a29b2911cbdc91a7e268046c395278238b2e" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/symfony/console/zipball/70d2a29b2911cbdc91a7e268046c395278238b2e", + "reference": "70d2a29b2911cbdc91a7e268046c395278238b2e", + "shasum": "" + }, + "require": { + "php": ">=5.5.9", + "symfony/debug": "~2.8|~3.0", + "symfony/polyfill-mbstring": "~1.0" + }, + "conflict": { + "symfony/dependency-injection": "<3.3" + }, + "require-dev": { + "psr/log": "~1.0", + "symfony/config": "~3.3", + "symfony/dependency-injection": "~3.3", + "symfony/event-dispatcher": "~2.8|~3.0", + "symfony/filesystem": "~2.8|~3.0", + "symfony/http-kernel": "~2.8|~3.0", + "symfony/process": "~2.8|~3.0" + }, + "suggest": { + "psr/log": "For using the console logger", + "symfony/event-dispatcher": "", + "symfony/filesystem": "", + "symfony/process": "" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "3.3-dev" + } + }, + "autoload": { + "psr-4": { + "Symfony\\Component\\Console\\": "" + }, + "exclude-from-classmap": [ + "/Tests/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Fabien Potencier", + "email": "fabien@symfony.com" + }, + { + "name": "Symfony Community", + "homepage": "https://symfony.com/contributors" + } + ], + "description": "Symfony Console Component", + "homepage": "https://symfony.com", + "time": "2017-06-02 19:24:58" + }, + { + "name": "symfony/debug", + "version": "v3.3.2", + "source": { + "type": "git", + "url": "https://github.com/symfony/debug.git", + "reference": "e9c50482841ef696e8fa1470d950a79c8921f45d" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/symfony/debug/zipball/e9c50482841ef696e8fa1470d950a79c8921f45d", + "reference": "e9c50482841ef696e8fa1470d950a79c8921f45d", + "shasum": "" + }, + "require": { + "php": ">=5.5.9", + "psr/log": "~1.0" + }, + "conflict": { + "symfony/http-kernel": ">=2.3,<2.3.24|~2.4.0|>=2.5,<2.5.9|>=2.6,<2.6.2" + }, + "require-dev": { + "symfony/http-kernel": "~2.8|~3.0" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "3.3-dev" + } + }, + "autoload": { + "psr-4": { + "Symfony\\Component\\Debug\\": "" + }, + "exclude-from-classmap": [ + "/Tests/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Fabien Potencier", + "email": "fabien@symfony.com" + }, + { + "name": "Symfony Community", + "homepage": "https://symfony.com/contributors" + } + ], + "description": "Symfony Debug Component", + "homepage": "https://symfony.com", + "time": "2017-06-01 21:01:25" + }, + { + "name": "symfony/polyfill-mbstring", + "version": "v1.4.0", + "source": { + "type": "git", + "url": "https://github.com/symfony/polyfill-mbstring.git", + "reference": "f29dca382a6485c3cbe6379f0c61230167681937" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/symfony/polyfill-mbstring/zipball/f29dca382a6485c3cbe6379f0c61230167681937", + "reference": "f29dca382a6485c3cbe6379f0c61230167681937", + "shasum": "" + }, + "require": { + "php": ">=5.3.3" + }, + "suggest": { + "ext-mbstring": "For best performance" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.4-dev" + } + }, + "autoload": { + "psr-4": { + "Symfony\\Polyfill\\Mbstring\\": "" + }, + "files": [ + "bootstrap.php" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Nicolas Grekas", + "email": "p@tchwork.com" + }, + { + "name": "Symfony Community", + "homepage": "https://symfony.com/contributors" + } + ], + "description": "Symfony polyfill for the Mbstring extension", + "homepage": "https://symfony.com", + "keywords": [ + "compatibility", + "mbstring", + "polyfill", + "portable", + "shim" + ], + "time": "2017-06-09 14:24:12" + }, { "name": "symfony/yaml", "version": "v3.2.8", @@ -607,6 +905,98 @@ "description": "Symfony Yaml Component", "homepage": "https://symfony.com", "time": "2017-05-01 14:55:58" + }, + { + "name": "zonuexe/mastodon-api", + "version": "0.0.6", + "source": { + "type": "git", + "url": "https://github.com/BaguettePHP/mastodon-api.git", + "reference": "ee030a06d5018b54b55352deb988582b30f1dca1" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/BaguettePHP/mastodon-api/zipball/ee030a06d5018b54b55352deb988582b30f1dca1", + "reference": "ee030a06d5018b54b55352deb988582b30f1dca1", + "shasum": "" + }, + "require": { + "guzzlehttp/guzzle": "^6.2", + "respect/validation": "^1.1", + "zonuexe/objectsystem": "^0.6.0" + }, + "require-dev": { + "filp/whoops": "^2.1", + "monolog/monolog": "^1.22", + "paragonie/random_compat": "^2.0", + "phpmd/phpmd": "^2.6", + "phpunit/phpunit": "^4.8", + "symfony/polyfill-php70": "^1.3", + "symfony/var-dumper": "^3.2", + "vlucas/phpdotenv": "^2.4", + "zonuexe/simple-routing": "^0.5.3" + }, + "suggest": { + "vlucas/phpdotenv": "Load .env format config file" + }, + "type": "library", + "autoload": { + "files": [ + "src/functions.php", + "src/Entity/helpers.php" + ], + "psr-4": { + "Baguette\\Mastodon\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "GPL-3.0" + ], + "description": "A PHP interface for Mastodon.", + "time": "2017-05-29 16:21:47" + }, + { + "name": "zonuexe/objectsystem", + "version": "0.6.0", + "source": { + "type": "git", + "url": "https://github.com/BaguettePHP/objectsystem.git", + "reference": "9a5f4413b82ea394c1018d40098269c86ac232c1" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/BaguettePHP/objectsystem/zipball/9a5f4413b82ea394c1018d40098269c86ac232c1", + "reference": "9a5f4413b82ea394c1018d40098269c86ac232c1", + "shasum": "" + }, + "require": { + "php": ">=5.5|>=7.0" + }, + "require-dev": { + "phploc/phploc": "*", + "phpmd/phpmd": "@stable", + "phpunit/phpunit": "^5.5|^4.8", + "theseer/phpdox": "^0.8" + }, + "type": "library", + "autoload": { + "psr-4": { + "Teto\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "Apache-2.0" + ], + "authors": [ + { + "name": "USAMI Kenta", + "email": "tadsan@zonu.me" + } + ], + "description": "Object system for PHP", + "time": "2016-10-08 15:52:57" } ], "packages-dev": [], diff --git a/config.yml.example b/config.yml.example index bc3b09c..768289c 100644 --- a/config.yml.example +++ b/config.yml.example @@ -1,4 +1,12 @@ DRY_RUN: true -API_KEY: some-key-here -BOT_NAME: bot name -CHAT_ID: id +TELEGRAM: true +MASTODON: true +MASTODON_CLIENT_ID: some-id +MASTODON_SECRET: oauth_secret +MASTODON_ACCESS_TOKEN: token +MASTODON_SERVER: https://botsin.space +MASTODON_USER: user +TELEGRAM_API_KEY: some-key-here +TELEGRAM_BOT_NAME: bot name +TELEGRAM_CHAT_ID: id +BITLY_TOKEN: token