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;
|
2019-11-30 08:08:17 +02:00
|
|
|
use \App\Models\Language;
|
|
|
|
use \App\Models\Author;
|
2019-11-30 09:03:43 +02:00
|
|
|
use \App\Models\Platform;
|
|
|
|
use \App\Models\Tag;
|
2019-09-12 20:03:56 +03:00
|
|
|
use \App\Source;
|
2019-11-30 08:08:17 +02:00
|
|
|
use Symfony\Component\DomCrawler\Crawler;
|
|
|
|
use Log;
|
2019-09-12 20:03:56 +03:00
|
|
|
|
|
|
|
class Anivisual extends Source {
|
2020-04-10 08:26:05 +03:00
|
|
|
public $title = "Anivisual";
|
|
|
|
public $keyword = 'anivisual';
|
2019-09-12 20:03:56 +03:00
|
|
|
protected $months = [
|
|
|
|
'Января' => 'January',
|
|
|
|
'Февраля' => 'February',
|
|
|
|
'Марта' => 'March',
|
|
|
|
'Апреля' => 'April',
|
|
|
|
'Мая' => 'May',
|
|
|
|
'Июня' => 'June',
|
|
|
|
'Июля' => 'July',
|
|
|
|
'Августа' => 'August',
|
|
|
|
'Сентября' => 'September',
|
|
|
|
'Октября' => 'October',
|
|
|
|
'Ноября' => 'November',
|
|
|
|
'Декабря' => 'December',
|
|
|
|
];
|
2019-11-29 18:42:16 +02:00
|
|
|
|
2020-04-10 08:26:05 +03:00
|
|
|
public function parse() {
|
|
|
|
// Parsing first 5 pages
|
|
|
|
for ($i = 1; $i <= 5; $i++) {
|
|
|
|
$this->parseIndex($i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function parseIndex($i) {
|
2020-04-10 08:27:32 +03:00
|
|
|
$text = $this->get_text('http://anivisual.net/stuff/1-'.$i);
|
2019-09-12 20:03:56 +03:00
|
|
|
$this->loadStr($text);
|
|
|
|
unset($text);
|
2020-04-10 08:26:05 +03:00
|
|
|
$this->dom->filter('#allEntries .entryBlock')->each(function($gameBlock) {
|
2019-11-30 08:08:17 +02:00
|
|
|
// Get the game link
|
2020-04-10 08:26:05 +03:00
|
|
|
$link = $gameBlock->filter('.novel-tmb a')->first();
|
2020-01-04 17:56:58 +02:00
|
|
|
$link = 'http://anivisual.net'.$link->attr('href');
|
2019-11-30 08:08:17 +02:00
|
|
|
$this->page($link);
|
2019-09-12 20:03:56 +03:00
|
|
|
});
|
2020-04-10 08:26:05 +03:00
|
|
|
}
|
2019-09-12 20:03:56 +03:00
|
|
|
|
|
|
|
public function checkPage($url) {
|
|
|
|
return (strpos($url,'://anivisual.net/stuff/') !== FALSE);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function page($url) {
|
|
|
|
$text = $this->get_text($url);
|
|
|
|
$this->loadStr($text);
|
2019-11-30 08:08:17 +02:00
|
|
|
unset($text);
|
|
|
|
$game = new Game();
|
|
|
|
$game->url = $url;
|
|
|
|
$game->source_id = str_replace('http://anivisual.net/stuff/', '', $url);
|
|
|
|
|
|
|
|
$game = $this->findGame($game);
|
2020-01-04 17:56:58 +02:00
|
|
|
if ($game->isClean()) {
|
|
|
|
return;
|
|
|
|
}
|
2019-11-30 08:08:17 +02:00
|
|
|
|
2019-09-12 20:03:56 +03:00
|
|
|
$gameBlock = $this->dom->filter('#casing-box');
|
2020-05-04 20:03:40 +03:00
|
|
|
$dateBlock = $this->dom->filter('.post-dts .icon-calendar');
|
2019-09-12 20:03:56 +03:00
|
|
|
$date = '';
|
|
|
|
if ($dateBlock->count() > 0) {
|
|
|
|
$date = trim($dateBlock->first()->text());
|
2020-05-04 20:03:40 +03:00
|
|
|
if (!empty($date)) {
|
|
|
|
$release_date = \DateTime::createFromFormat('d.m.Y', $date);
|
|
|
|
if (!empty($release_date)) {
|
|
|
|
$game->release_date = $release_date;
|
|
|
|
}
|
|
|
|
unset($date);
|
2020-01-04 17:56:58 +02:00
|
|
|
}
|
2019-09-12 20:03:56 +03:00
|
|
|
}
|
|
|
|
$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);
|
2019-11-30 08:08:17 +02:00
|
|
|
$game->description = trim($game->description);
|
|
|
|
|
|
|
|
$game->save();
|
|
|
|
|
|
|
|
$language = Language::findByCode('ru');
|
|
|
|
if (!$game->languages()->where('code', 'ru')->exists()) {
|
2020-10-23 11:15:12 +03:00
|
|
|
$game->languages()->syncWithoutDetaching([$language->id]);
|
2019-11-30 08:08:17 +02:00
|
|
|
}
|
2019-09-12 20:03:56 +03:00
|
|
|
|
|
|
|
$sidebar = $gameBlock->filter('#sidebar')->first()->html();
|
2019-11-30 09:03:43 +02:00
|
|
|
[$author, $author_url] = $this->getValue($sidebar, 'Автор');
|
2019-11-30 08:08:17 +02:00
|
|
|
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);
|
|
|
|
}
|
2020-10-23 11:15:12 +03:00
|
|
|
if ($author instanceof Author) {
|
2019-11-30 08:08:17 +02:00
|
|
|
$author->save();
|
2020-10-23 11:15:12 +03:00
|
|
|
if (!$game->authors()->where('authors.id', $author->id)->exists()) {
|
2019-11-30 08:08:17 +02:00
|
|
|
$game->authors()->attach($author);
|
|
|
|
}
|
|
|
|
}
|
2019-09-12 20:03:56 +03:00
|
|
|
|
2019-11-30 09:03:43 +02:00
|
|
|
[$sidebar_search, $author_url] = $this->getValue($sidebar, 'Перевод');
|
2019-09-12 20:03:56 +03:00
|
|
|
if ($sidebar_search !== '') {
|
2019-11-30 08:08:17 +02:00
|
|
|
$language = Language::findByCode('en');
|
2020-10-23 11:15:12 +03:00
|
|
|
if (!$game->languages()->where('languages.id', $language->id)->exists()) {
|
|
|
|
$game->languages()->attach($language->id);
|
2019-11-30 08:08:17 +02:00
|
|
|
}
|
|
|
|
$author = Author::findByName($sidebar_search);
|
|
|
|
if (!$author) {
|
|
|
|
$author = new Author();
|
|
|
|
$author->name = $sidebar_search;
|
|
|
|
$author->is_translator = true;
|
|
|
|
$author->url = $author_url;
|
|
|
|
$author->save();
|
|
|
|
}
|
2020-10-23 11:15:12 +03:00
|
|
|
if (!$game->authors()->where('authors.id', $author->id)->exists()) {
|
2019-11-30 08:08:17 +02:00
|
|
|
$game->authors()->attach($author);
|
|
|
|
}
|
|
|
|
}
|
2019-11-30 09:03:43 +02:00
|
|
|
|
|
|
|
[$platforms, ] = $this->getValue($sidebar, 'Платформа');
|
|
|
|
$platforms = explode(',', $platforms);
|
|
|
|
$platforms = array_map('trim', $platforms);
|
2020-10-23 11:15:12 +03:00
|
|
|
$platform_ids = [];
|
2019-11-30 09:03:43 +02:00
|
|
|
foreach ($platforms as $platform) {
|
|
|
|
$model = Platform::where('title', $platform)->first();
|
|
|
|
if (!$model) {
|
|
|
|
$model = new Platform();
|
|
|
|
$model->title = $platform;
|
|
|
|
$model->save();
|
|
|
|
}
|
2020-10-23 11:15:12 +03:00
|
|
|
$platform_ids[] = $model->id;
|
2019-11-30 09:03:43 +02:00
|
|
|
}
|
2020-10-23 11:15:12 +03:00
|
|
|
$game->platforms()->sync($platform_ids);
|
2019-11-30 09:03:43 +02:00
|
|
|
|
|
|
|
[$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');
|
2020-10-23 11:15:12 +03:00
|
|
|
if (!$game->languages()->where('languages.id', $language->id)->exists()) {
|
2020-06-08 08:53:02 +03:00
|
|
|
$game->languages()->attach($language);
|
|
|
|
}
|
2020-10-23 11:15:12 +03:00
|
|
|
$tag_ids = [];
|
2019-11-30 09:03:43 +02:00
|
|
|
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();
|
|
|
|
}
|
2020-10-23 11:15:12 +03:00
|
|
|
$tag_ids[] = $model->id;
|
2019-11-30 09:03:43 +02:00
|
|
|
}
|
2020-10-23 11:15:12 +03:00
|
|
|
$game->tags()->sync($tag_ids);
|
2019-11-30 08:08:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
2019-11-30 09:03:43 +02:00
|
|
|
|
|
|
|
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];
|
|
|
|
}
|
2019-09-12 20:03:56 +03:00
|
|
|
}
|