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/Questbook.php
2020-04-04 18:54:28 +07:00

161 lines
5.2 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 \App\Models\Platform;
use \App\Models\Language;
use \App\Models\Author;
use \App\Models\Tag;
use \App\Source;
use Log;
class Questbook extends Source {
public $title = "Сторигеймы";
protected $platform_atril;
protected $platform_pdf;
protected $language_model;
public function parse() {
$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');
}
public function parseFeed($feedUrl) {
$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 {
$parsed = $service->parse($string);
if (is_array($parsed) && isset($parsed[0])) {
$games = $parsed[0]['value'] ?? [];
}
} catch (\Sabre\Xml\LibXMLException $e) {
echo $e->getMessage();
echo $e->getTraceAsString();
return "";
}
unset($string);
if (empty($games)) {
Log::error('Игр не найдено.');
return;
}
foreach ($games as $gameBlock) {
$game = new Game();
$game->title = trim($gameBlock['title'] ?? '');
$game->release_date = new \DateTime($gameBlock['pubDate']);
$game->url = trim($gameBlock['link'] ?? '');
$game->url = str_replace('http://', 'https://', $game->url);
$source_id = NULL;
if (strpos($game->url, 'online/game') !== false) {
$source_id = (int) str_replace('https://quest-book.ru/online/game', '', $game->url);
$game->url_play_online = $game->url;
}
if (strpos($game->url, '/directory/') !== false) {
$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;
}
$game->description = trim($gameBlock['description'] ?? '');
$game = $this->findGame($game);
if (!$game->isClean()) {
$game->save();
$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);
}
$this->page($game->url);
}
}
}
public function checkPage($url) {
return (strpos($url,'https://quest-book.ru/') !== FALSE);
}
public function page($url) {
$game = Game::where('url', $url)->first();
if (!$game) {
$game = new Game();
$game->url = $url;
}
$text = $this->get_text($url);
$this->loadStr($text);
$title = $this->dom->filter('h2 a b');
if ($title->count() > 0) {
$title = $title->first()->text();
$game->title = $title;
} else {
$title = $this->dom->filter('h2.mt-1');
if ($title->count() > 0) {
$title = $title->first()->text();
$game->title = $title;
}
}
$game->platform = 'Книга-игра';
if (strpos($game->url, 'quest-book.ru/online/') !== FALSE) {
$game->platform = 'Атрил';
}
$description = $this->dom->filter('div.col-md-8.col-sm-12 > div > div')->reduce(function($node) {
if ($node->attr('style') === 'padding:5px;margin:2px;text-align:justify')
return true;
return false;
});
if ($description->count() > 0) {
$description = $description->text();
$game->description = $description;
}
$game->description = trim($game->description);
$author = $this->dom->filter('div.col-md-8.col-sm-12 em span')->reduce(function($node) {
if ($node->attr('itemprop') === 'author')
return true;
return false;
});
if ($author->count() > 0) {
$author = $author->text();
$game->author = $author;
}
return $game;
}
}