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
|
|
|
|
|
|
|
/**
|
|
|
|
* Парсер для Apero.ru
|
|
|
|
* Проблема парсера в том, что на Аперо часто поломана кодировка UTF-8.
|
|
|
|
*/
|
|
|
|
class Apero extends Source {
|
2020-01-04 19:46:58 +02:00
|
|
|
public $title = "Apero";
|
|
|
|
public $keyword = 'apero';
|
2020-01-05 10:39:13 +02:00
|
|
|
protected $urls = [];
|
2019-11-29 18:42:16 +02:00
|
|
|
|
2019-09-12 22:30:03 +03:00
|
|
|
public function parse() {
|
2019-09-12 20:03:56 +03:00
|
|
|
$text = $this->get_text('http://apero.ru/Текстовые-игры/Песочница', [
|
|
|
|
'order_by' => 'by_public',
|
|
|
|
]);
|
|
|
|
$text = mb_convert_encoding($text, 'UTF-8', 'auto');
|
|
|
|
$this->loadStr($text);
|
|
|
|
$this->parseIndex();
|
|
|
|
$text = $this->get_text('http://apero.ru/Текстовые-игры', [
|
|
|
|
'order_by' => 'by_public',
|
|
|
|
]);
|
|
|
|
$text = mb_convert_encoding($text, 'UTF-8', 'auto');
|
|
|
|
$this->loadStr($text);
|
|
|
|
$this->parseIndex();
|
2020-01-05 10:39:13 +02:00
|
|
|
foreach ($this->urls as $url) {
|
|
|
|
$text = $this->get_text($url);
|
|
|
|
$text = mb_convert_encoding($text, 'UTF-8', 'auto');
|
|
|
|
$this->loadStr($text);
|
|
|
|
$this->page($url);
|
|
|
|
}
|
2019-11-29 18:42:16 +02:00
|
|
|
}
|
|
|
|
|
2019-09-12 22:30:03 +03:00
|
|
|
public function parseIndex()
|
2019-09-12 20:03:56 +03:00
|
|
|
{
|
|
|
|
$this->dom->filter('.tabled-game-block')->each(function($gameBlock){
|
2020-01-04 19:46:58 +02:00
|
|
|
$url = trim($gameBlock->filter('h2 a')->first()->attr('href'));
|
2020-01-05 10:39:13 +02:00
|
|
|
$this->urls[] = $url;
|
2019-09-12 20:03:56 +03:00
|
|
|
});
|
|
|
|
}
|
2020-01-04 19:46:58 +02:00
|
|
|
|
2019-09-12 20:03:56 +03:00
|
|
|
public function checkPage($url) {
|
|
|
|
return (strpos($url,'http://apero.ru/') !== FALSE);
|
|
|
|
}
|
2020-01-04 19:46:58 +02:00
|
|
|
|
2019-09-12 20:03:56 +03:00
|
|
|
public function page($url) {
|
|
|
|
$game = new Game;
|
|
|
|
$game->url = $url;
|
2020-01-04 19:46:58 +02:00
|
|
|
if ($this->dom->filter('#printer')->count() > 0) {
|
|
|
|
$game->source_id = (int) $this->dom->filter('#printer')->first()->attr('data-game-id');
|
|
|
|
} else {
|
|
|
|
$id = $this->dom->filter('dd')->reduce(function($block) {
|
|
|
|
if ($block->attr('style') === 'color: #cccccc;') {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
if ($id->count() > 0) {
|
|
|
|
$game->source_id = (int) $id->text();
|
2019-09-12 20:03:56 +03:00
|
|
|
}
|
2020-01-04 19:46:58 +02:00
|
|
|
}
|
|
|
|
if (empty($game->source_id)) {
|
|
|
|
throw new \Exception('no id');
|
|
|
|
}
|
|
|
|
|
|
|
|
$game = $this->findGame($game);
|
|
|
|
if ($game->isClean()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$title = $this->dom->filter('dd')->reduce(function($block) {
|
|
|
|
if ($block->attr('itemprop') === 'name') {
|
2019-09-12 20:03:56 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
});
|
2020-01-04 19:46:58 +02:00
|
|
|
if ($title->count() > 0) {
|
|
|
|
$game->title = $title->text();
|
|
|
|
}
|
|
|
|
|
2019-09-12 20:03:56 +03:00
|
|
|
$date = $this->dom->filter('meta')->reduce(function($block){
|
|
|
|
if ($block->attr('itemprop') === 'datePublished') {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
})->first();
|
|
|
|
if ($date->count() > 0) {
|
|
|
|
$date = $date->attr('content');
|
|
|
|
} else {
|
|
|
|
$date = NULL;
|
|
|
|
}
|
2020-01-04 19:46:58 +02:00
|
|
|
if (!empty($date)) {
|
2020-01-05 10:39:13 +02:00
|
|
|
$game->release_date = \DateTime::createFromFormat('Y-m-d', $date);
|
2020-01-04 19:46:58 +02:00
|
|
|
}
|
2020-06-10 12:58:04 +03:00
|
|
|
|
|
|
|
$ddblock = $this->dom->filter('dt')->reduce(function($block){
|
|
|
|
return ($block->text() === 'Описание:');
|
|
|
|
});
|
|
|
|
if (!empty($ddblock) && $ddblock->count() > 0) {
|
|
|
|
$ddblock = $ddblock->first()->nextAll('dd');
|
|
|
|
if (!empty($ddblock) && $ddblock->count() > 0) {
|
|
|
|
$ddblock = $ddblock->first()->filter('div')->first();
|
|
|
|
$game->description = $ddblock->html();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-04 19:46:58 +02:00
|
|
|
$game->save();
|
|
|
|
|
|
|
|
$this->dom->filter('dd a')->reduce(function($block){
|
|
|
|
if ($block->attr('itemprop') === 'author') {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
})->each(function($block) use($game){
|
|
|
|
$author_name = $block->text();
|
|
|
|
$author_url = $block->attr('href');
|
|
|
|
|
|
|
|
$author_model = Author::findByName($author_name);
|
|
|
|
if (empty($author_model)) {
|
|
|
|
$author_model = new Author();
|
|
|
|
$author_model->name = $author_name;
|
|
|
|
$author_model->url = $author_url;
|
|
|
|
$author_model->save();
|
|
|
|
}
|
|
|
|
if (!$game->authors()->where('name', $author_name)->exists()) {
|
|
|
|
$game->authors()->attach($author_model);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
$language = Language::findByCode('ru');
|
|
|
|
if (!$game->languages()->where('code', 'ru')->exists()) {
|
|
|
|
$game->languages()->attach($language);
|
|
|
|
}
|
|
|
|
|
|
|
|
$model = Platform::where('title', 'Аперо')->first();
|
|
|
|
if (!$model) {
|
|
|
|
$model = new Platform();
|
|
|
|
$model->title = 'Аперо';
|
|
|
|
$model->save();
|
|
|
|
}
|
|
|
|
$game->platforms()->attach($model);
|
|
|
|
|
|
|
|
$this->dom->filter('dd')->reduce(function($block){
|
|
|
|
if ($block->attr('itemprop') === 'genre') {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
})->each(function($block) use($game, $language){
|
|
|
|
$genre = trim($block->text());
|
|
|
|
$model = Tag::where('language_id', $language->id)
|
|
|
|
->where('title', $genre)
|
|
|
|
->first();
|
|
|
|
if (!$model) {
|
|
|
|
$model = new Tag();
|
|
|
|
$model->language_id = $language->id;
|
|
|
|
$model->title = $genre;
|
|
|
|
$model->save();
|
|
|
|
}
|
|
|
|
$game->tags()->attach($model);
|
|
|
|
});
|
|
|
|
|
|
|
|
Log::info($game->title);
|
2019-09-12 20:03:56 +03:00
|
|
|
}
|
|
|
|
}
|