Archived
1
0
Fork 0
This repository has been archived on 2021-07-30. You can view files and clone it, but cannot push or open issues or pull requests.
ifnews/app/Sources/Steam.php
2020-04-05 14:41:54 +07:00

166 lines
5.1 KiB
PHP

<?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;
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);
}
}
}
}