. */ namespace App\Sources; use \App\Models\Game; use \Symfony\Component\DomCrawler\Crawler; use \GuzzleHttp\Cookie\CookieJar; use \GuzzleHttp\Cookie\SetCookie; use \App\Models\Platform; use \App\Models\Language; use \App\Models\Author; use \App\Models\Tag; use \App\Source; use Log; class Steam extends Source { public $title = "Steam"; public $keyword = 'steam'; protected $games = []; public $delayedLoad = true; protected $months = [ 'янв.' => 'January', 'фев.' => 'February', 'мар.' => 'March', 'апр.' => 'April', 'мая.' => 'May', 'июн.' => 'June', 'июл.' => 'July', 'авг.' => 'August', 'сен.' => 'September', 'окт.' => 'October', 'ноя.' => 'November', 'дек.' => 'December', ]; public function parse_tag($tag) { $url = 'https://store.steampowered.com/search/'; $url .= '?'.http_build_query([ 'sort_by' => 'Released_DESC', 'term' => $tag, 'displayterm' => $tag, 'category1' => 998, // only games ]); $text = $this->get_text($url); $this->loadStr($text); unset($text); $this->dom->filter('#search_result_container a.search_result_row')->each(function($gameLink){ $url = $gameLink->attr('href'); $url = substr($url,0,strpos($url, '?')); // remove query string $this->page($url); }); } public function parse() { $this->parse_tag("text-based"); $this->parse_tag("interactive fiction"); $this->parse_tag("visual novel"); } public function checkPage($url) { return (strpos($url,'store.steampowered.com/') !== FALSE); } public function page($url) { $cookies = new CookieJar(true); $cookies->setCookie(new SetCookie([ 'Domain' => 'store.steampowered.com', 'Name' => 'mature_content', 'Value' => 1 ])); $cookies->setCookie(new SetCookie([ 'Domain' => 'store.steampowered.com', 'Name' => 'Steam_Language', 'Value' => 'russian' ])); $cookies->setCookie(new SetCookie([ 'Domain' => 'store.steampowered.com', 'Name' => 'timezoneOffset', 'Value' => '0,0' ])); $this->set_cookies($cookies); $this->loadStr($this->get_text($url)); $game = new Game; $game->url = $url; $title = $this->dom->filter('div.apphub_AppName')->first(); if ($title->count() > 0) { $game->title = trim($title->text()); } $game = $this->findGame($game); $dsc = $this->dom->filter('div.game_description_snippet')->first(); if ($dsc->count() > 0) { $game->description = trim($dsc->text()); } $image = $this->dom->filter('img.game_header_image_full')->first(); if ($image->count() > 0) { $game->image_url = $image->attr('src'); } $date = $this->dom->filter('div.date')->first(); if ($date->count() > 0) { $date = $date->text(); $game->release_date = \DateTime::createFromFormat('d M, Y', $date); if ($game->release_date === FALSE) { // если Steam отдал страницу на русском foreach ($this->months as $ruM => $enM) { $date = str_replace($ruM, $enM, $date); } $game->release_date = \DateTime::createFromFormat('d F Y', $date); } if (empty($game->release_date)) { $game->release_date = NULL; } } $game->save(); $languages = $this->dom->filter('.game_language_options tr td:first-child'); if ($languages->count() > 0) { $language_codes = []; foreach ($languages as $language) { $language_codes[] = trim($language->nodeValue); } foreach ($language_codes as $langCode) { $language_model = Language::findByCode($langCode); $game->languages()->attach($language_model); } } $author = $this->dom->filter('div#developers_list')->first(); $authors = []; if ($author->count() > 0) { $authors = [trim($author->text())]; if (strpos($authors[0], ',') !== FALSE) { $authors = explode(',', $authors[0]); $authors = array_map('trim', $authors); } } foreach ($authors as $author) { $author_model = Author::findByName($author); if (empty($author_model)) { $author_model = new Author(); $author_model->name = $author; $author_model->save(); } if (!$game->authors()->where('name', $author)->exists()) { $game->authors()->attach($author_model); } } } }