bot.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <?php
  2. /*
  3. Microblog bot for game release notifications
  4. Copyright (C) 2017 Alexander Yakovlev
  5. This program is free software: you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation, either version 3 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. require_once ("vendor/autoload.php");
  17. use Symfony\Component\Yaml\Yaml;
  18. use Revolution\Mastodon\MastodonClient;
  19. use Mremi\UrlShortener\Provider\Bitly\BitlyProvider;
  20. use Mremi\UrlShortener\Provider\Bitly\GenericAccessTokenAuthenticator;
  21. require_once "Game.php";
  22. require_once "Source.php";
  23. require_once "download.php";
  24. $loader = new \Aura\Autoload\Loader;
  25. $loader->register();
  26. $loader->addPrefix('Source', 'Source');
  27. $parsers = [
  28. // 'urq',
  29. 'anivisual',
  30. 'kvester',
  31. 'apero',
  32. 'instead',
  33. 'hyperbook_ru',
  34. 'hyperbook_en',
  35. 'questbook',
  36. 'textadventures',
  37. 'ifdb',
  38. 'dashingdon',
  39. 'itch',
  40. 'gamejolt',
  41. 'steam',
  42. ];
  43. $config = Yaml::parse(file_get_contents('config.yml'));
  44. $lastrun = 0;
  45. if (file_exists('.lastrun') && !$config['DRY_RUN']) {
  46. $lastrun = file_get_contents('.lastrun');
  47. }
  48. $pandoc = new \Pandoc\Pandoc();
  49. $bitlyProvider = new BitlyProvider(
  50. new GenericAccessTokenAuthenticator($config['BITLY_TOKEN'])
  51. );
  52. if ($config['TELEGRAM'] === true) {
  53. $telegram = new Longman\TelegramBot\Telegram(
  54. $config['TELEGRAM_API_KEY'],
  55. $config['TELEGRAM_BOT_NAME']
  56. );
  57. }
  58. if ($config['MASTODON'] === true) {
  59. $mastodon = new MastodonClient();
  60. }
  61. function check($classname, $command) {
  62. global $parsers;
  63. global $mastodon;
  64. global $telegram;
  65. global $config;
  66. if (in_array($command, $parsers)) {
  67. $cname = 'Source\\'.$classname;
  68. $source = (new $cname());
  69. $source->check();
  70. if (empty($source->games)) {
  71. return;
  72. }
  73. foreach ($source->games as $game) {
  74. $description = $game->text();
  75. if ($config['DRY_RUN']) {
  76. echo $description."\n";
  77. continue;
  78. }
  79. if ($config['TELEGRAM'] === true) {
  80. try {
  81. $result = \Longman\TelegramBot\Request::sendMessage([
  82. 'chat_id' => $config['TELEGRAM_CHAT_ID'],
  83. 'text' => $description,
  84. 'parse_mode' => 'Markdown'
  85. ]);
  86. if (!$config['DRY_RUN']) {
  87. file_put_contents('.lastrun', time());
  88. }
  89. } catch (Longman\TelegramBot\Exception\TelegramException $e) {
  90. echo $e;
  91. }
  92. }
  93. if ($config['MASTODON'] === true) {
  94. $mastodon->domain($config['MASTODON_SERVER'])->token($config['MASTODON_ACCESS_TOKEN']);
  95. /*
  96. if ($image) {
  97. download('https://ifhub.club'.$image, './'.basename($image));
  98. $attachment = $mastodon->post('/media', [
  99. 'file' => file_get_contents('./'.basename($image))
  100. ]);
  101. var_dump($attachment);
  102. unlink('./'.basename($image));
  103. $mdescription .= $attachment->url;
  104. }
  105. */
  106. $mastodon->status_post($description);
  107. if (!$config['DRY_RUN']) {
  108. file_put_contents('.lastrun', time());
  109. }
  110. }
  111. }
  112. }
  113. }
  114. foreach($config['parsers'] as $parser) {
  115. $command = strtolower($parser['classname']);
  116. if (isset($parser['command'])) {
  117. $command = $parser['command'];
  118. }
  119. check ($parser['classname'], $command);
  120. }