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

96 lines
3.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\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);
$language = Language::findByCode('ru');
$platform = Platform::findByName($this->platform);
$this->dom->filter('.container .at-card')->each(functioN($gameBlock) use($language, $platform) {
$game = new Game;
$game->title = trim($gameBlock->filter('h5')->text());
$url = trim($gameBlock->filter('h5 a')->attr('href'));
$game->url = 'https://instory.su'.$url;
if (strpos($url, '/story/') !== false) {
$game->source_id = (int) str_replace('/story/', '', $url);
}
$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());
});
$game->languages()->sync([$language->id]);
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()->syncWithoutDetaching([$model->id]);
}
$game->platforms()->sync([$platform->id]);
if (!empty($author)) {
$author_model = Author::findByName($author);
if (empty($author_model)) {
$author_model = new Author();
$author_model->name = $author;
$author_model->save();
}
$game->authors()->sync([$author_model->id]);
}
});
}
public function checkPage($url) {
return (strpos($url,'//instory.su/') !== FALSE);
}
}