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;
|
2020-01-05 11:19:04 +02:00
|
|
|
use \App\Models\Platform;
|
|
|
|
use \App\Models\Language;
|
|
|
|
use \App\Models\Author;
|
|
|
|
use \App\Models\Tag;
|
2019-09-12 20:03:56 +03:00
|
|
|
use \App\Source;
|
2020-01-05 11:19:04 +02:00
|
|
|
use Log;
|
2019-09-12 20:03:56 +03:00
|
|
|
|
|
|
|
class Questbook extends Source {
|
|
|
|
public $title = "Сторигеймы";
|
2020-04-10 07:16:48 +03:00
|
|
|
public $keyword = 'questbook';
|
2020-01-05 11:19:04 +02:00
|
|
|
protected $platform_atril;
|
|
|
|
protected $platform_pdf;
|
|
|
|
protected $language_model;
|
2019-09-12 20:03:56 +03:00
|
|
|
|
2019-09-12 22:30:03 +03:00
|
|
|
public function parse() {
|
2020-01-05 11:19:04 +02:00
|
|
|
$this->language_model = Language::findByCode('ru');
|
|
|
|
$this->platform_atril = Platform::findByName('Атрил');
|
|
|
|
$this->platform_pdf = Platform::findByName('PDF');
|
|
|
|
$this->parseFeed('https://quest-book.ru/directory/rss/');
|
|
|
|
$this->parseFeed('https://quest-book.ru/online/rss.xml');
|
2019-09-12 20:03:56 +03:00
|
|
|
}
|
|
|
|
|
2019-09-12 22:30:03 +03:00
|
|
|
public function parseFeed($feedUrl) {
|
2019-09-12 20:03:56 +03:00
|
|
|
$string = $this->get_text($feedUrl);
|
|
|
|
$string = mb_convert_encoding($string, 'UTF-8', 'auto');
|
|
|
|
$service = new \Sabre\Xml\Service();
|
|
|
|
$service->elementMap = [
|
|
|
|
'{}item' => function(\Sabre\Xml\Reader $reader) {
|
|
|
|
return \Sabre\Xml\Deserializer\keyValue($reader, '');
|
|
|
|
},
|
|
|
|
'{}channel' => function(\Sabre\Xml\Reader $reader) {
|
|
|
|
return \Sabre\Xml\Deserializer\repeatingElements($reader, '{}item');
|
|
|
|
},
|
|
|
|
];
|
|
|
|
try {
|
2020-04-04 14:54:28 +03:00
|
|
|
$parsed = $service->parse($string);
|
|
|
|
if (is_array($parsed) && isset($parsed[0])) {
|
|
|
|
$games = $parsed[0]['value'] ?? [];
|
|
|
|
}
|
2019-09-12 20:03:56 +03:00
|
|
|
} catch (\Sabre\Xml\LibXMLException $e) {
|
|
|
|
echo $e->getMessage();
|
|
|
|
echo $e->getTraceAsString();
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
unset($string);
|
2020-04-04 14:54:28 +03:00
|
|
|
if (empty($games)) {
|
|
|
|
Log::error('Игр не найдено.');
|
|
|
|
return;
|
|
|
|
}
|
2019-09-12 20:03:56 +03:00
|
|
|
foreach ($games as $gameBlock) {
|
2020-01-05 11:19:04 +02:00
|
|
|
$game = new Game();
|
2019-09-12 20:03:56 +03:00
|
|
|
$game->title = trim($gameBlock['title'] ?? '');
|
2020-04-04 14:54:28 +03:00
|
|
|
$game->release_date = new \DateTime($gameBlock['pubDate']);
|
2019-09-12 20:03:56 +03:00
|
|
|
$game->url = trim($gameBlock['link'] ?? '');
|
|
|
|
$game->url = str_replace('http://', 'https://', $game->url);
|
2020-01-05 11:19:04 +02:00
|
|
|
$source_id = NULL;
|
2020-04-04 14:54:28 +03:00
|
|
|
if (strpos($game->url, 'online/game') !== false) {
|
2020-01-05 11:19:04 +02:00
|
|
|
$source_id = (int) str_replace('https://quest-book.ru/online/game', '', $game->url);
|
2020-04-04 14:54:28 +03:00
|
|
|
$game->url_play_online = $game->url;
|
2020-01-05 11:19:04 +02:00
|
|
|
}
|
2020-04-04 14:54:28 +03:00
|
|
|
if (strpos($game->url, '/directory/') !== false) {
|
2020-01-05 11:19:04 +02:00
|
|
|
$url = str_replace('https://quest-book.ru/directory/', '', $game->url);
|
|
|
|
$url = explode('/', $url);
|
|
|
|
$url = array_filter($url);
|
|
|
|
$source_id = reset($url);
|
|
|
|
}
|
|
|
|
if (!empty($source_id)) {
|
|
|
|
$game->source_id = $source_id;
|
|
|
|
}
|
2019-09-12 20:03:56 +03:00
|
|
|
$game->description = trim($gameBlock['description'] ?? '');
|
2020-01-05 11:19:04 +02:00
|
|
|
|
|
|
|
$game = $this->findGame($game);
|
|
|
|
if (!$game->isClean()) {
|
|
|
|
$game->save();
|
2020-04-04 14:54:28 +03:00
|
|
|
|
|
|
|
$author_name = trim($gameBlock['author'] ?? '');
|
|
|
|
$author_model = Author::findByName($author_name);
|
|
|
|
if (empty($author_model)) {
|
|
|
|
$author_model = new Author();
|
|
|
|
$author_model->name = $author_name;
|
|
|
|
$author_model->save();
|
|
|
|
}
|
|
|
|
if (!$game->authors()->where('name', $author_name)->exists()) {
|
|
|
|
$game->authors()->attach($author_model);
|
|
|
|
}
|
|
|
|
|
2020-05-04 20:21:36 +03:00
|
|
|
if (strpos($game->title, 'PDF') !== FALSE) {
|
|
|
|
$game->platforms()->attach($this->platform_pdf);
|
|
|
|
} else {
|
|
|
|
$game->platforms()->attach($this->platform_atril);
|
|
|
|
}
|
|
|
|
if (!$game->languages()->where('code', $this->language_model->code)->exists()) {
|
|
|
|
$game->languages()->attach($this->language_model);
|
|
|
|
}
|
2020-01-05 11:19:04 +02:00
|
|
|
}
|
2019-09-12 20:03:56 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function checkPage($url) {
|
|
|
|
return (strpos($url,'https://quest-book.ru/') !== FALSE);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|