Itch.php 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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 Itch extends Source {
  19. public $title = "Itch.io";
  20. protected function parse_tag($url) {
  21. $service = new \Sabre\Xml\Service();
  22. $xml = $this->get_text($url);
  23. $service->elementMap = [
  24. '{}item' => function(\Sabre\Xml\Reader $reader) {
  25. $game = new Game;
  26. $keyValue = \Sabre\Xml\Deserializer\keyValue($reader, '{}item');
  27. if (isset($keyValue['{}pubDate'])) {
  28. $game->date = strtotime($keyValue['{}pubDate']);
  29. if ($game->date < $this->period) {
  30. return $game;
  31. }
  32. }
  33. if (isset($keyValue['{}plainTitle'])) {
  34. $game->title = $keyValue['{}plainTitle'];
  35. }
  36. if (isset($keyValue['{}link'])) {
  37. $game->url = $keyValue['{}link'];
  38. }
  39. if (isset($keyValue['{}description'])) {
  40. $game->description = trim(strip_tags($keyValue['{}description'], '<p><a><br>'));
  41. }
  42. $game_page = $this->get_text($game->url);
  43. $this->dom->loadStr($game_page, []);
  44. $lines = $this->dom->find('.game_info_panel_widget tr');
  45. foreach ($lines as $line) {
  46. $text = $line->find('td');
  47. if ($text->innerHtml == 'Author') {
  48. $game->author = strip_tags($text->nextSibling()->innerHtml);
  49. }
  50. }
  51. $this->games[] = $game;
  52. return $game;
  53. },
  54. ];
  55. $dom = $service->parse($xml);
  56. }
  57. protected function parse() {
  58. $this->parse_tag("https://itch.io/games/newest/tag-text-based.xml");
  59. $this->parse_tag("https://itch.io/games/newest/tag-interactive-fiction.xml");
  60. }
  61. }