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-04 19:46:58 +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-04 19:46:58 +02:00
|
|
|
use Log;
|
2019-09-12 20:03:56 +03:00
|
|
|
|
|
|
|
class Hyperbook extends Source {
|
|
|
|
public $title = "Гиперкнига";
|
2020-04-10 07:16:48 +03:00
|
|
|
public $keyword = 'hyperbook';
|
2020-01-04 19:46:58 +02:00
|
|
|
protected $platform = 'AXMA Story Maker';
|
|
|
|
protected $platform_model;
|
|
|
|
protected $language_model;
|
2019-09-12 20:03:56 +03:00
|
|
|
protected $rootUrl = 'http://hyperbook.ru';
|
2020-01-05 10:10:04 +02:00
|
|
|
protected function set_language(): void {
|
|
|
|
$this->language_model = Language::findByCode('ru');
|
|
|
|
}
|
2019-09-12 22:30:03 +03:00
|
|
|
public function parse() {
|
2019-09-12 20:03:56 +03:00
|
|
|
$text = $this->get_text($this->rootUrl.'/lib.php?sort=time');
|
|
|
|
$this->loadStr($text);
|
|
|
|
unset($text);
|
2020-01-04 19:46:58 +02:00
|
|
|
$model = Platform::where('title', $this->platform)->first();
|
|
|
|
if (!$model) {
|
|
|
|
$model = new Platform();
|
|
|
|
$model->title = $this->platform;
|
|
|
|
$model->save();
|
|
|
|
}
|
|
|
|
$this->platform_model = $model;
|
2020-01-05 10:10:04 +02:00
|
|
|
$this->set_language();
|
2020-01-04 19:46:58 +02:00
|
|
|
|
2020-01-05 10:10:04 +02:00
|
|
|
$urls = [];
|
|
|
|
$this->dom->filter("#listPubs h3 a")->each(function($link) use (&$urls) {
|
2020-01-04 19:46:58 +02:00
|
|
|
$id = $link->attr('href');
|
|
|
|
$id = (int) str_replace('file', '', $id);
|
|
|
|
$url = $this->rootUrl.'/comments.php?id='.$id;
|
2020-01-05 10:10:04 +02:00
|
|
|
$urls[$id] = $url;
|
2019-09-12 20:03:56 +03:00
|
|
|
});
|
2020-01-05 10:10:04 +02:00
|
|
|
foreach ($urls as $id => $url) {
|
|
|
|
$this->page($url, $id);
|
|
|
|
}
|
2019-09-12 20:03:56 +03:00
|
|
|
}
|
|
|
|
public function checkPage($url) {
|
|
|
|
return (strpos($url,$this->rootUrl.'/comments.php') !== FALSE);
|
|
|
|
}
|
2020-01-04 19:46:58 +02:00
|
|
|
public function page($url, $id) {
|
2019-09-12 20:03:56 +03:00
|
|
|
$game = new Game;
|
|
|
|
$game->url = $url;
|
2020-01-04 19:46:58 +02:00
|
|
|
$game->source_id = $id;
|
2020-01-05 10:10:04 +02:00
|
|
|
if (empty($game->source_id)) {
|
|
|
|
throw new \Exception('no id');
|
|
|
|
return;
|
|
|
|
}
|
2020-01-04 19:46:58 +02:00
|
|
|
|
|
|
|
$game = $this->findGame($game);
|
|
|
|
if ($game->isClean()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-01-05 10:10:04 +02:00
|
|
|
$this->loadStr($this->get_text($url));
|
|
|
|
$title = $this->dom->filter(".content h1")->first()->text();
|
|
|
|
$this->dom->filter(".content h1 span")->each(function($span) use(&$title) {
|
|
|
|
$title = trim(str_replace($span->text(), '', $title));
|
|
|
|
});
|
|
|
|
$game->title = $title;
|
2020-01-04 19:46:58 +02:00
|
|
|
|
|
|
|
$date = $this->dom->filter(".content div.small")->reduce(function($node) {
|
2019-09-12 20:03:56 +03:00
|
|
|
if ($node->attr('style') === 'float: left; width: 20%; text-align:right;')
|
|
|
|
return true;
|
|
|
|
return false;
|
2020-01-04 19:46:58 +02:00
|
|
|
});
|
|
|
|
if ($date->count() > 0){
|
|
|
|
$date = $date->first()->text();
|
|
|
|
if (!empty($date)) {
|
2020-01-05 10:10:04 +02:00
|
|
|
$game->release_date = \DateTime::createFromFormat('d.m.y', $date);
|
2020-01-04 19:46:58 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$description = $this->dom->filter(".content div.small")->reduce(function($node) {
|
2019-09-12 20:03:56 +03:00
|
|
|
if ($node->attr('style') === NULL)
|
|
|
|
return true;
|
|
|
|
return false;
|
2020-01-04 19:46:58 +02:00
|
|
|
});
|
|
|
|
if ($description->count() > 0) {
|
|
|
|
$game->description = $description->first()->text();
|
|
|
|
}
|
|
|
|
$game->save();
|
|
|
|
|
|
|
|
$author = $this->dom->filter(".content > div")->reduce(function($node) {
|
|
|
|
if ($node->attr('style') === 'float: left; width: 50%; margin-bottom:14px; text-align: left;') {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
if ($author->count() > 0) {
|
|
|
|
$author = $author->first()->text();
|
|
|
|
} else {
|
|
|
|
$author = '';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!empty($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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$game->platforms()->attach($this->platform_model);
|
2020-01-05 10:10:04 +02:00
|
|
|
if (!$game->languages()->where('code', $this->language_model->code)->exists()) {
|
2020-01-04 19:46:58 +02:00
|
|
|
$game->languages()->attach($this->language_model);
|
|
|
|
}
|
|
|
|
|
|
|
|
Log::info($game->title);
|
2019-09-12 20:03:56 +03:00
|
|
|
}
|
|
|
|
}
|