Questbook.php 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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. namespace Source;
  17. use \Game;
  18. class Questbook extends Source {
  19. public $title = "Библиотека книг-игр";
  20. protected function parse() {
  21. $string = $this->get_text('http://quest-book.ru/directory/rss/');
  22. $service = new \Sabre\Xml\Service();
  23. $service->elementMap = [
  24. '{}item' => function(\Sabre\Xml\Reader $reader) {
  25. return \Sabre\Xml\Deserializer\keyValue($reader, '');
  26. },
  27. '{}channel' => function(\Sabre\Xml\Reader $reader) {
  28. return \Sabre\Xml\Deserializer\repeatingElements($reader, '{}item');
  29. },
  30. ];
  31. try {
  32. $games = $service->parse($string)[0]['value'];
  33. } catch (\Sabre\Xml\LibXMLException $e) {
  34. echo $e->getMessage();
  35. echo $e->getTraceAsString();
  36. return "";
  37. }
  38. unset($string);
  39. foreach ($games as $gameBlock) {
  40. $date = strtotime($gameBlock['pubDate']);
  41. if ($date < $this->period) continue;
  42. $game = new Game;
  43. $game->title = trim($gameBlock['title']);
  44. $game->url = trim($gameBlock['link']);
  45. $game->description = trim($gameBlock['description']);
  46. $this->games[] = $game;
  47. }
  48. }
  49. }