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/Hyperbook.php

140 lines
4.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 Hyperbook extends Source {
public $title = "Гиперкнига";
protected $platform = 'AXMA Story Maker';
protected $platform_model;
protected $language_model;
protected $rootUrl = 'http://hyperbook.ru';
protected function set_language(): void {
$this->language_model = Language::findByCode('ru');
}
public function parse() {
$text = $this->get_text($this->rootUrl.'/lib.php?sort=time');
$this->loadStr($text);
unset($text);
$model = Platform::where('title', $this->platform)->first();
if (!$model) {
$model = new Platform();
$model->title = $this->platform;
$model->save();
}
$this->platform_model = $model;
$this->set_language();
$urls = [];
$this->dom->filter("#listPubs h3 a")->each(function($link) use (&$urls) {
$id = $link->attr('href');
$id = (int) str_replace('file', '', $id);
$url = $this->rootUrl.'/comments.php?id='.$id;
$urls[$id] = $url;
});
foreach ($urls as $id => $url) {
$this->page($url, $id);
}
}
public function checkPage($url) {
return (strpos($url,$this->rootUrl.'/comments.php') !== FALSE);
}
public function page($url, $id) {
$game = new Game;
$game->url = $url;
$game->source_id = $id;
if (empty($game->source_id)) {
throw new \Exception('no id');
return;
}
$game = $this->findGame($game);
if ($game->isClean()) {
return;
}
$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;
$date = $this->dom->filter(".content div.small")->reduce(function($node) {
if ($node->attr('style') === 'float: left; width: 20%; text-align:right;')
return true;
return false;
});
if ($date->count() > 0){
$date = $date->first()->text();
if (!empty($date)) {
$game->release_date = \DateTime::createFromFormat('d.m.y', $date);
}
}
$description = $this->dom->filter(".content div.small")->reduce(function($node) {
if ($node->attr('style') === NULL)
return true;
return false;
});
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);
if (!$game->languages()->where('code', $this->language_model->code)->exists()) {
$game->languages()->attach($this->language_model);
}
Log::info($game->title);
}
}