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

138 lines
4.8 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\Platform;
use \App\Models\Language;
use \App\Models\Author;
use \App\Models\Tag;
use \App\Source;
use Log;
use \Pandoc\Pandoc;
class Itch extends Source {
public $title = "Itch.io";
public $keyword = 'itch';
public $queue = [];
public $games = [];
public $print_description = FALSE;
public function parse_tag($url) {
$max_pages = 4; // load 30*4 = 120 latest games
for ($i = 1; $i <= $max_pages; $i++) {
$cururl = $url.'?format=json&page='.$i;
$text = $this->get_json($cururl);
$this->loadStr($text->content);
$this->dom->filter('.game_cell')->each(function($cell) {
$game = new Game;
$game->source_id = (int) $cell->attr('data-game_id');
$game->url = $cell->filter('a.game_link')->attr('href');
$game->title = $cell->filter('a.title')->text();
$game->title = html_entity_decode($game->title);
$game = $this->findGame($game);
$game->image_url = $cell->filter('.game_thumb')->attr('data-background_image');
$author_name = $cell->filter('.game_author')->text();
$author_url = $cell->filter('.game_author > a')->first()->attr('href');
if ($game->save() && !empty($author_name)) {
$author_model = Author::findByName($author_name);
if (empty($author_model)) {
$author_model = new Author();
$author_model->name = $author_name;
$author_model->url = $author_url;
$author_model->save();
}
if (!$game->authors()->where('name', $author_name)->exists()) {
$game->authors()->attach($author_model);
}
}
$this->queue[] = $game->url;
});
if ($text->num_items < 30) {
// less than default number of games, probably last page
break;
}
}
}
public function parse() {
$this->parse_tag("https://itch.io/games/newest/tag-text-based");
$this->parse_tag("https://itch.io/games/newest/tag-twine");
$this->parse_tag("https://itch.io/games/newest/tag-interactive-fiction");
$this->queue = array_unique($this->queue);
foreach ($this->queue as $url) {
$game_page = $this->get_text($url);
$this->loadStr($game_page);
$game = $this->page($url);
$game->save();
}
}
public function checkPage($url) {
return (strpos($url,'.itch.io/') !== FALSE);
}
public function page($url) {
$game = new Game;
$game->url = $url;
$game = $this->findGame($game);
$title = trim($this->dom->filter("title")->first()->text());
[$game->title, $author_name] = explode(' by ', $title);
unset($title);
$this->dom->filter('script[type="application/ld+json"]')->each(function($script) use(&$game) {
$data = json_decode($script->html());
if ($data === false) {
return;
}
if ($data->{'@type'} === 'Product') {
if (isset($data->description)) {
$game->short_description = $data->description;
}
if (isset($data->name)) {
$game->title = $data->name;
}
/*
if (isset($data->offers) && isset($data->offers->seller)) {
$game->author = $data->offers->seller->name;
}
*/
}
});
$game->title = html_entity_decode($game->title);
$date = $this->dom->filter('td abbr');
if ($date->count() > 0) {
$date = $date->first()->attr('title');
$date = str_replace('@', '', $date);
$game->release_date = new \DateTime($date);
}
if ($this->print_description) {
$desc = $this->dom->filter('.formatted_description');
try {
$game->description = trim($desc->first()->html());
} catch (\Throwable $e) {
}
if (empty($game->short_description)) {
$converter = new Pandoc();
$description = $converter->convert($game->description, 'html', 'mediawiki');
$description = explode(' ',$description);
// 50 first words
$game->short_description = implode(' ', array_slice($description, 0, 50));
}
}
return $game;
}
}