Hyperbook.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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 Hyperbook extends Source {
  19. public $title = "Гиперкнига";
  20. protected function parse() {
  21. $text = $this->get_text('http://hyperbook.ru/lib.php?sort=time');
  22. try {
  23. $this->dom->loadStr($text, []);
  24. } catch (\Exception $e) {
  25. echo $e->getMessage();
  26. echo $e->getTraceAsString();
  27. return "";
  28. }
  29. unset($text);
  30. $container = $this->dom->find('#listPubs');
  31. $games = [];
  32. try {
  33. $headings = $container->find("h3");
  34. } catch (\Exception $e ) {
  35. echo $e->getMessage();
  36. echo $e->getTraceAsString();
  37. return "";
  38. }
  39. foreach ($headings as $heading) {
  40. $game = new Game;
  41. $link = $heading->find('a')[0];
  42. $game->title = $link->innerHtml;
  43. $game->url = $link->getAttribute('href');
  44. $game->url = str_replace('file', 'http://hyperbook.ru/comments.php?id=', $game->url);
  45. $games[] = $game;
  46. }
  47. $i = 0;
  48. foreach ($container->find("div") as $author) {
  49. if ($author->getAttribute('style') !== 'text-align:left;margin-bottom:4px;') {
  50. continue;
  51. }
  52. $games[$i]->author = $author->innerHtml;
  53. $i++;
  54. }
  55. $i = 0;
  56. foreach ($container->find("div.small") as $small) {
  57. if(
  58. $small->getAttribute('style') === 'float: left; width: 20%; text-align:right;' &&
  59. is_null($games[$i]->date)
  60. ) {
  61. $games[$i]->date = $small->innerHtml;
  62. }
  63. elseif ($small->getAttribute('style') === NULL) {
  64. $games[$i]->description = $small->innerHtml;
  65. $i++;
  66. }
  67. }
  68. foreach ($games as $game) {
  69. $date = \DateTime::createFromFormat('d.m.y', $game->date);
  70. if ($date === false) continue;
  71. $date = $date->format('U');
  72. if ($date < $this->period) continue;
  73. $this->games[] = $game;
  74. }
  75. }
  76. }