Steam.php 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. use \PHPHtmlParser\Dom;
  19. class Steam extends Source {
  20. public $title = "Steam";
  21. protected function parse_tag($tag) {
  22. $url = 'http://store.steampowered.com/search/';
  23. $url .= '?'.http_build_query([
  24. 'sort_by' => 'Released_DESC',
  25. 'term' => $tag,
  26. 'displayterm' => $tag,
  27. 'category1' => 998, // only games
  28. ]);
  29. try {
  30. $text = $this->get_text($url);
  31. $this->dom->loadStr($text, []);
  32. } catch (\Exception $e) {
  33. echo $e->getMessage();
  34. echo $e->getTraceAsString();
  35. return "";
  36. }
  37. unset($text);
  38. $games = $this->dom->find('#search_result_container a.search_result_row');
  39. foreach ($games as $gameLink) {
  40. $url = $gameLink->getAttribute('href');
  41. $url = substr($url,0,strpos($url, '?')); // remove query string
  42. $game = new Game;
  43. $game->url = $url;
  44. try {
  45. $text = $this->get_text($url);
  46. $this->dom = new Dom;
  47. $this->dom->loadStr($text, []);
  48. unset($text);
  49. $name = $this->dom->find('div.apphub_AppName');
  50. $description = $this->dom->find('meta[property=og:description]');
  51. try {
  52. $game->title = $name->innerHtml;
  53. $game->description = $description->innerHtml;
  54. } catch (\Exception $e) {
  55. echo 'No title or description found for '.$url.PHP_EOL;
  56. }
  57. } catch (\Exception $e) {
  58. echo $e->getMessage();
  59. echo $e->getTraceAsString();
  60. }
  61. $this->games[] = $game;
  62. }
  63. }
  64. protected function parse() {
  65. $this->parse_tag("text-based");
  66. }
  67. }