2019-09-12 20:03:56 +03:00
|
|
|
<?php
|
|
|
|
/*
|
|
|
|
A set of utilities for tracking text-based game releases
|
|
|
|
Copyright (C) 2017-2018 Alexander Yakovlev
|
|
|
|
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
namespace App\Sources;
|
|
|
|
|
|
|
|
use \App\Models\Game;
|
|
|
|
use \Symfony\Component\DomCrawler\Crawler;
|
|
|
|
use \GuzzleHttp\Cookie\CookieJar;
|
|
|
|
use \GuzzleHttp\Cookie\SetCookie;
|
2020-04-05 10:41:54 +03:00
|
|
|
use \App\Models\Platform;
|
|
|
|
use \App\Models\Language;
|
|
|
|
use \App\Models\Author;
|
|
|
|
use \App\Models\Tag;
|
|
|
|
use \App\Source;
|
|
|
|
use Log;
|
2019-09-12 20:03:56 +03:00
|
|
|
|
|
|
|
class Steam extends Source {
|
|
|
|
public $title = "Steam";
|
2020-04-05 10:41:54 +03:00
|
|
|
public $keyword = 'steam';
|
2019-09-12 20:03:56 +03:00
|
|
|
protected $games = [];
|
|
|
|
public $delayedLoad = true;
|
|
|
|
protected $months = [
|
|
|
|
'янв.' => 'January',
|
|
|
|
'фев.' => 'February',
|
|
|
|
'мар.' => 'March',
|
|
|
|
'апр.' => 'April',
|
|
|
|
'мая.' => 'May',
|
|
|
|
'июн.' => 'June',
|
|
|
|
'июл.' => 'July',
|
|
|
|
'авг.' => 'August',
|
|
|
|
'сен.' => 'September',
|
|
|
|
'окт.' => 'October',
|
|
|
|
'ноя.' => 'November',
|
|
|
|
'дек.' => 'December',
|
|
|
|
];
|
2019-09-12 22:30:03 +03:00
|
|
|
public function parse_tag($tag) {
|
2019-09-12 20:03:56 +03:00
|
|
|
$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
|
2020-04-05 10:41:54 +03:00
|
|
|
$this->page($url);
|
2019-09-12 20:03:56 +03:00
|
|
|
});
|
|
|
|
}
|
2019-09-12 22:30:03 +03:00
|
|
|
public function parse() {
|
2020-04-05 10:41:54 +03:00
|
|
|
$this->parse_tag("text-based");
|
|
|
|
$this->parse_tag("interactive fiction");
|
|
|
|
$this->parse_tag("visual novel");
|
2019-09-12 20:03:56 +03:00
|
|
|
}
|
|
|
|
public function checkPage($url) {
|
|
|
|
return (strpos($url,'store.steampowered.com/') !== FALSE);
|
|
|
|
}
|
|
|
|
public function page($url) {
|
2020-01-05 10:39:13 +02:00
|
|
|
$cookies = new CookieJar(true);
|
|
|
|
$cookies->setCookie(new SetCookie([
|
2019-09-12 20:03:56 +03:00
|
|
|
'Domain' => 'store.steampowered.com',
|
|
|
|
'Name' => 'mature_content',
|
|
|
|
'Value' => 1
|
|
|
|
]));
|
2020-01-05 10:39:13 +02:00
|
|
|
$cookies->setCookie(new SetCookie([
|
2019-09-12 20:03:56 +03:00
|
|
|
'Domain' => 'store.steampowered.com',
|
|
|
|
'Name' => 'Steam_Language',
|
|
|
|
'Value' => 'russian'
|
|
|
|
]));
|
2020-01-05 10:39:13 +02:00
|
|
|
$cookies->setCookie(new SetCookie([
|
2019-09-12 20:03:56 +03:00
|
|
|
'Domain' => 'store.steampowered.com',
|
|
|
|
'Name' => 'timezoneOffset',
|
|
|
|
'Value' => '0,0'
|
|
|
|
]));
|
2020-01-05 10:39:13 +02:00
|
|
|
$this->set_cookies($cookies);
|
2019-09-12 20:03:56 +03:00
|
|
|
$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());
|
|
|
|
}
|
2020-04-05 10:41:54 +03:00
|
|
|
|
|
|
|
$game = $this->findGame($game);
|
|
|
|
|
2019-09-12 20:03:56 +03:00
|
|
|
$dsc = $this->dom->filter('div.game_description_snippet')->first();
|
|
|
|
if ($dsc->count() > 0) {
|
|
|
|
$game->description = trim($dsc->text());
|
|
|
|
}
|
2020-04-05 10:41:54 +03:00
|
|
|
|
2019-09-12 20:03:56 +03:00
|
|
|
$image = $this->dom->filter('img.game_header_image_full')->first();
|
|
|
|
if ($image->count() > 0) {
|
2020-04-05 10:41:54 +03:00
|
|
|
$game->image_url = $image->attr('src');
|
2019-09-12 20:03:56 +03:00
|
|
|
}
|
2020-04-05 10:41:54 +03:00
|
|
|
|
2019-09-12 20:03:56 +03:00
|
|
|
$date = $this->dom->filter('div.date')->first();
|
|
|
|
if ($date->count() > 0) {
|
|
|
|
$date = $date->text();
|
2020-04-05 10:41:54 +03:00
|
|
|
$game->release_date = \DateTime::createFromFormat('d M, Y', $date);
|
|
|
|
if ($game->release_date === FALSE) { // если Steam отдал страницу на русском
|
2019-09-12 20:03:56 +03:00
|
|
|
foreach ($this->months as $ruM => $enM) {
|
|
|
|
$date = str_replace($ruM, $enM, $date);
|
|
|
|
}
|
2020-04-05 10:41:54 +03:00
|
|
|
$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);
|
2019-09-12 20:03:56 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|