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

219 lines
6.9 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\Author;
use \App\Models\Platform;
use \App\Models\Tag;
use \App\Source;
use Symfony\Component\DomCrawler\Crawler;
use Log;
class Anivisual extends Source {
public $title = "Anivisual";
protected $months = [
'Января' => 'January',
'Февраля' => 'February',
'Марта' => 'March',
'Апреля' => 'April',
'Мая' => 'May',
'Июня' => 'June',
'Июля' => 'July',
'Августа' => 'August',
'Сентября' => 'September',
'Октября' => 'October',
'Ноября' => 'November',
'Декабря' => 'December',
];
public function parse() {
$text = $this->get_text('http://anivisual.net/stuff/1');
$this->loadStr($text);
unset($text);
$lastDate = $this->getLastDate();
$this->dom->filter('.entryBlock')->each(function($gameBlock) use($lastDate) {
// Check that the game date is after the date of the last parsed game
$date = trim($gameBlock->filter('.icon-calendar')->text());
foreach ($this->months as $ruM => $enM) {
$date = str_replace($ruM, $enM, $date);
}
$date = \DateTime::createFromFormat('d F Y', $date);
if (!empty($lastDate) && $date >= $lastDate) {
return;
}
// Get the game link
$link = $gameBlock->filter('.novel-ttl a')->first();
$link = 'http://anivisual.net'.$link->attr('href');
$this->page($link);
});
}
public function checkPage($url) {
return (strpos($url,'://anivisual.net/stuff/') !== FALSE);
}
public function page($url) {
$text = $this->get_text($url);
$this->loadStr($text);
unset($text);
$game = new Game();
$game->url = $url;
$game->source_id = str_replace('http://anivisual.net/stuff/', '', $url);
$game = $this->findGame($game);
$gameBlock = $this->dom->filter('#casing-box');
$dateBlock = $this->dom->filter('.icon-calendar');
$date = '';
if ($dateBlock->count() > 0) {
$date = trim($dateBlock->first()->text());
}
if (!empty($date)) {
foreach ($this->months as $ruM => $enM) {
$date = str_replace($ruM, $enM, $date);
}
$game->release_date = \DateTime::createFromFormat('d F Y', $date);
unset($date);
}
$title = $this->dom->filter('h1.logo')->first();
if ($title->count() > 0) {
$game->title = trim(htmlspecialchars_decode($title->text()));
}
$game->description = $this->dom->filter('#content > section > span')->first()->text();
$game->description = str_replace('(adsbygoogle = window.adsbygoogle || []).push({});', '', $game->description);
$game->description = str_replace('Доп. ссылки: Доступно только для пользователей', '', $game->description);
$game->description = trim($game->description);
$game->save();
$language = Language::findByCode('ru');
if (!$game->languages()->where('code', 'ru')->exists()) {
$game->languages()->attach($language);
}
$sidebar = $gameBlock->filter('#sidebar')->first()->html();
[$author, $author_url] = $this->getValue($sidebar, 'Автор');
if (!empty($author)) {
$author_model = Author::findByName($author);
if (empty($author_model)) {
$author_model = new Author();
$author_model->name = $author;
$author_model->url = $author_url;
}
$author = $author_model;
unset($author_model);
}
if (!empty($author)) {
$author->save();
if (!$game->authors()->where('name', $author->name)->exists()) {
$game->authors()->attach($author);
}
}
[$sidebar_search, $author_url] = $this->getValue($sidebar, 'Перевод');
if ($sidebar_search !== '') {
$language = Language::findByCode('en');
if (!$game->languages()->where('code', 'en')->exists()) {
$game->languages()->attach($language);
}
$author = Author::findByName($sidebar_search);
if (!$author) {
$author = new Author();
$author->name = $sidebar_search;
$author->is_translator = true;
$author->url = $author_url;
$author->save();
}
if (!$game->authors()->where('name', $author->name)->exists()) {
$game->authors()->attach($author);
}
}
[$platforms, ] = $this->getValue($sidebar, 'Платформа');
$platforms = explode(',', $platforms);
$platforms = array_map('trim', $platforms);
foreach ($platforms as $platform) {
$model = Platform::where('title', $platform)->first();
if (!$model) {
$model = new Platform();
$model->title = $platform;
$model->save();
}
$game->platforms()->attach($model);
}
[$genres, ] = $this->getValue($sidebar, 'Жанры');
[$tags, ] = $this->getValue($sidebar, 'Теги');
$tags = explode(',', $tags);
$genres = explode(',', $genres);
$tags = array_merge($tags, $genres);
unset($genres);
$tags = array_map('trim', $tags);
$tags = array_filter($tags);
$language_ru = Language::findByCode('ru');
foreach ($tags as $tag) {
$model = Tag::where('language_id', $language_ru->id)
->where('title', $tag)
->first();
if (!$model) {
$model = new Tag();
$model->language_id = $language_ru->id;
$model->title = $tag;
$model->save();
}
$game->tags()->attach($model);
}
Log::info($game->title);
}
protected function getLink($html) {
if (empty($html)) {
return '';
}
$author_dom = new Crawler($html);
$author_url = '';
if ($author_dom->filter('a')->count() === 0) {
return '';
}
$link = $author_dom->filter('a')->first();
if (!empty($link)) {
$author_url = $link->attr('href');
if (!empty($author_url)) {
$author_url = str_replace('/go?', '', $author_url);
}
}
return (string) $author_url;
}
protected function getValue($sidebar, $value) {
$pos_start = mb_strpos($sidebar, '<b>'.$value.':');
$sidebar_search = trim(mb_substr($sidebar, $pos_start));
$pos_end = mb_strpos($sidebar_search, '<br>');
$sidebar_search = trim(mb_substr($sidebar_search, 0, $pos_end));
$sidebar_search = str_replace('<b>'.$value.':</b>', '', $sidebar_search);
$text = trim(strip_tags($sidebar_search));
$url = $this->getLink($sidebar_search);
return [$text, $url];
}
}