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-10-21 09:22:02 +03:00
|
|
|
use \App\Models\Author;
|
|
|
|
use \App\Models\Language;
|
2019-09-12 20:03:56 +03:00
|
|
|
use \App\Source;
|
|
|
|
use \Symfony\Component\DomCrawler\Crawler;
|
|
|
|
|
|
|
|
class Textadventures extends Source {
|
|
|
|
public $title = "Textadventures.co.uk";
|
2020-10-21 09:22:02 +03:00
|
|
|
public $keyword = 'textadventures';
|
|
|
|
public $baseUrl = 'https://textadventures.co.uk';
|
2019-09-12 22:30:03 +03:00
|
|
|
public function parse() {
|
2020-10-21 09:22:02 +03:00
|
|
|
$text = $this->get_text($this->baseUrl.'/games/latest');
|
2019-09-12 20:03:56 +03:00
|
|
|
$this->loadStr($text);
|
2020-10-21 09:22:02 +03:00
|
|
|
$language = Language::findByCode('en');
|
2019-09-12 20:03:56 +03:00
|
|
|
unset($text);
|
2020-10-21 09:22:02 +03:00
|
|
|
$this->dom->filter('.games-item')->each(function($gameBlock) use($language){
|
2019-09-12 20:03:56 +03:00
|
|
|
$game = new Game;
|
2020-10-21 09:22:02 +03:00
|
|
|
$url = (string) $gameBlock->filter('.games-title a')->attr('href');
|
|
|
|
$game->url = $this->baseUrl.$url;
|
|
|
|
$temp = str_replace('/games/view/', '', $url);
|
|
|
|
$temp = explode('/', $temp);
|
|
|
|
$id = $temp[0];
|
|
|
|
unset($temp);
|
|
|
|
$game->source_id = $id;
|
|
|
|
$game->url_discussion = $game->url;
|
|
|
|
$game->url_download = $this->baseUrl.'/games/download/'.$id;
|
|
|
|
$game->url_play_online = $this->baseUrl.'/games/play/'.$id;
|
2019-09-12 20:03:56 +03:00
|
|
|
$game->title = $gameBlock->filter('.games-title a')->text();
|
2020-10-21 09:22:02 +03:00
|
|
|
$game = $this->findGame($game);
|
|
|
|
$date = $gameBlock->filter('.games-date')->text();
|
|
|
|
$game->release_date = new \DateTime($date);
|
2019-09-12 20:03:56 +03:00
|
|
|
$text = $this->get_text($game->url);
|
2020-10-21 09:22:02 +03:00
|
|
|
|
2019-09-12 20:03:56 +03:00
|
|
|
$game_page = new Crawler($text);
|
2020-10-21 09:22:02 +03:00
|
|
|
$model = NULL;
|
|
|
|
if ($game_page->filter('h1 > small')->count() > 0) {
|
|
|
|
$author = $game_page->filter('h1 > small')->text();
|
|
|
|
$author = str_replace('by ', '', $author);
|
|
|
|
$model = Author::findByName($author);
|
|
|
|
if (!$model) {
|
|
|
|
$model = new Author;
|
|
|
|
$model->name = $author;
|
|
|
|
$model->is_person = true;
|
|
|
|
$author_url = '';
|
|
|
|
$game_page->filter('.col-md-3 a')->each(function($link) use(&$author_url) {
|
|
|
|
$matches = [];
|
|
|
|
if (preg_match('/\/user\/view\/(\S+)\/(\S+)/', $link->attr('href'), $matches)) {
|
|
|
|
$author_url = $this->baseUrl.$link->attr('href');
|
|
|
|
// $author_id = $matches[1] ?? '';
|
|
|
|
}
|
|
|
|
});
|
|
|
|
if (!empty($author_url)) {
|
|
|
|
$model->url = $author_url;
|
|
|
|
}
|
|
|
|
$model->save();
|
|
|
|
}
|
|
|
|
}
|
2019-09-12 20:03:56 +03:00
|
|
|
unset($text);
|
2020-10-21 09:22:02 +03:00
|
|
|
if ($game_page->filter('body > div:nth-child(5) > div:nth-child(1) > div > div.container > div > div.col-md-9 > div.row > div.col-md-7')->count()) {
|
|
|
|
$game->description = trim($game_page->filter('body > div:nth-child(5) > div:nth-child(1) > div > div.container > div > div.col-md-9 > div.row > div.col-md-7')->first()->text());
|
|
|
|
$game->description = preg_replace('/ Play online$/', '', $game->description);
|
|
|
|
}
|
2019-09-12 20:03:56 +03:00
|
|
|
$image = $game_page->filter('img.cover-image')->first();
|
|
|
|
if ($image->count()) {
|
2020-10-21 09:22:02 +03:00
|
|
|
$game->image_url = $image->attr('src');
|
2019-09-12 20:03:56 +03:00
|
|
|
}
|
2020-10-21 09:22:02 +03:00
|
|
|
$game->save();
|
2020-10-23 11:15:12 +03:00
|
|
|
$game->languages()->sync([$language->id]);
|
2020-10-21 09:22:02 +03:00
|
|
|
if (isset($author)) {
|
2020-10-23 11:15:12 +03:00
|
|
|
$game->authors()->sync([$model->id]);
|
2019-09-12 20:03:56 +03:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|