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/Instory.php
2020-04-19 13:44:50 +07:00

101 lines
3.3 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\Language;
use \App\Models\Tag;
use \App\Models\Author;
use \App\Models\Platform;
use \App\Source;
class Instory extends Source {
public $title = "Instory";
public $keyword = 'instory';
public $platform = 'Instory';
public function parse() {
$this->parseIndex('https://instory.su/catalog/sandbox');
$this->parseIndex('https://instory.su/catalog');
}
public function parseIndex($feedUrl) {
$string = $this->get_text($feedUrl);
$string = mb_convert_encoding($string, 'UTF-8', 'auto');
$this->loadStr($string);
unset($string);
$this->dom->filter('.container .at-card')->each(functioN($gameBlock) {
$game = new Game;
$game->title = trim($gameBlock->filter('h5')->text());
$game->url = 'https://instory.su'.trim($gameBlock->filter('h5 a')->attr('href'));
$game->description = trim($gameBlock->filter('.post-card__excerpt')->html());
$game->release_date = $this->downloader->convertDate($gameBlock->filter('.at-author__extend span')->text());
$game = $this->findGame($game);
$author = trim($gameBlock->filter('.at-author__name a')->text());
$game->save();
$tags = [];
$gameBlock->filter('.at-badge--cats .at-badge__content')->each(function($tag) use(&$tags){
$tags[] = trim($tag->text());
});
$language = Language::findByCode('ru');
if (!$game->languages()->where('code', 'ru')->exists()) {
$game->languages()->attach($language);
}
foreach ($tags as $tag) {
$model = Tag::where('language_id', $language->id)
->where('title', $tag)
->first();
if (!$model) {
$model = new Tag();
$model->language_id = $language->id;
$model->title = $tag;
$model->save();
}
$game->tags()->attach($model);
}
$model = Platform::where('title', $this->platform)->first();
if (!$model) {
$model = new Platform();
$model->title = $this->platform;
$model->save();
}
$game->platforms()->attach($model);
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);
}
}
});
}
public function checkPage($url) {
return (strpos($url,'//instory.su/') !== FALSE);
}
}