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

95 lines
3.7 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\Author;
use \App\Models\Language;
use \App\Source;
use \Symfony\Component\DomCrawler\Crawler;
class Textadventures extends Source {
public $title = "Textadventures.co.uk";
public $keyword = 'textadventures';
public $baseUrl = 'https://textadventures.co.uk';
public function parse() {
$text = $this->get_text($this->baseUrl.'/games/latest');
$this->loadStr($text);
$language = Language::findByCode('en');
unset($text);
$this->dom->filter('.games-item')->each(function($gameBlock) use($language){
$game = new Game;
$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;
$game->title = $gameBlock->filter('.games-title a')->text();
$game = $this->findGame($game);
$date = $gameBlock->filter('.games-date')->text();
$game->release_date = new \DateTime($date);
$text = $this->get_text($game->url);
$game_page = new Crawler($text);
$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();
}
}
unset($text);
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);
}
$image = $game_page->filter('img.cover-image')->first();
if ($image->count()) {
$game->image_url = $image->attr('src');
}
$game->save();
$game->languages()->sync([$language->id]);
if (isset($author)) {
$game->authors()->sync([$model->id]);
}
});
}
}