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-04-04 16:32:20 +03:00
|
|
|
use \App\Models\Platform;
|
|
|
|
use \App\Models\Language;
|
|
|
|
use \App\Models\Author;
|
|
|
|
use \App\Models\Tag;
|
2019-09-12 20:03:56 +03:00
|
|
|
use \App\Source;
|
2020-04-04 16:32:20 +03:00
|
|
|
use Log;
|
2019-09-12 20:03:56 +03:00
|
|
|
use \Pandoc\Pandoc;
|
|
|
|
|
|
|
|
class Itch extends Source {
|
|
|
|
public $title = "Itch.io";
|
2020-04-04 16:32:20 +03:00
|
|
|
public $keyword = 'itch';
|
2019-09-12 20:03:56 +03:00
|
|
|
public $queue = [];
|
|
|
|
public $games = [];
|
|
|
|
public $print_description = FALSE;
|
2020-04-04 16:32:20 +03:00
|
|
|
|
2019-09-12 22:30:03 +03:00
|
|
|
public function parse_tag($url) {
|
2019-09-12 20:03:56 +03:00
|
|
|
$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;
|
2020-04-04 16:32:20 +03:00
|
|
|
$game->source_id = (int) $cell->attr('data-game_id');
|
2019-09-12 20:03:56 +03:00
|
|
|
$game->url = $cell->filter('a.game_link')->attr('href');
|
|
|
|
$game->title = $cell->filter('a.title')->text();
|
2020-04-04 16:32:20 +03:00
|
|
|
$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;
|
2019-09-12 20:03:56 +03:00
|
|
|
});
|
|
|
|
if ($text->num_items < 30) {
|
|
|
|
// less than default number of games, probably last page
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-09-12 22:30:03 +03:00
|
|
|
public function parse() {
|
2020-04-04 16:32:20 +03:00
|
|
|
$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");
|
2020-05-25 16:31:16 +03:00
|
|
|
$this->parse_tag("https://itch.io/games/newest/made-with-twine");
|
|
|
|
$this->parse_tag("https://itch.io/games/newest/made-with-renpy");
|
2020-04-04 16:32:20 +03:00
|
|
|
$this->queue = array_unique($this->queue);
|
|
|
|
foreach ($this->queue as $url) {
|
|
|
|
$game_page = $this->get_text($url);
|
2019-09-12 22:30:03 +03:00
|
|
|
$this->loadStr($game_page);
|
2020-04-04 16:32:20 +03:00
|
|
|
$game = $this->page($url);
|
2020-06-08 08:53:02 +03:00
|
|
|
if (!empty($game)) {
|
|
|
|
$game->save();
|
|
|
|
}
|
2019-09-12 20:03:56 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
public function checkPage($url) {
|
|
|
|
return (strpos($url,'.itch.io/') !== FALSE);
|
|
|
|
}
|
|
|
|
public function page($url) {
|
|
|
|
$game = new Game;
|
|
|
|
$game->url = $url;
|
2020-04-04 16:32:20 +03:00
|
|
|
$game = $this->findGame($game);
|
2019-09-12 20:03:56 +03:00
|
|
|
$title = trim($this->dom->filter("title")->first()->text());
|
2020-04-04 16:32:20 +03:00
|
|
|
[$game->title, $author_name] = explode(' by ', $title);
|
2020-06-08 08:53:02 +03:00
|
|
|
unset($author_name);
|
2019-09-12 20:03:56 +03:00
|
|
|
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;
|
|
|
|
}
|
2020-04-04 16:32:20 +03:00
|
|
|
/*
|
2019-09-12 20:03:56 +03:00
|
|
|
if (isset($data->offers) && isset($data->offers->seller)) {
|
|
|
|
$game->author = $data->offers->seller->name;
|
|
|
|
}
|
2020-04-04 16:32:20 +03:00
|
|
|
*/
|
2019-09-12 20:03:56 +03:00
|
|
|
}
|
|
|
|
});
|
2020-04-04 16:32:20 +03:00
|
|
|
$game->title = html_entity_decode($game->title);
|
2020-06-08 08:53:02 +03:00
|
|
|
$this->dom->filter('.game_info_panel_widget td abbr')->each(function($date) use(&$game) {
|
|
|
|
$label = $date->closest('td')->siblings()->first()->text();
|
|
|
|
$date = $date->attr('title');
|
2019-09-12 20:03:56 +03:00
|
|
|
$date = str_replace('@', '', $date);
|
2020-06-08 08:53:02 +03:00
|
|
|
switch ($label) {
|
|
|
|
case 'Updated':
|
|
|
|
$game->updated_at = (new \DateTime($date))->format('c');
|
|
|
|
break;
|
|
|
|
case 'Published':
|
|
|
|
case 'Release date':
|
|
|
|
$game->release_date = new \DateTime($date);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
$tags = [];
|
|
|
|
$platforms = [];
|
|
|
|
$this->dom->filter('.game_info_panel_widget tr > td > a')->each(function($link) use(&$tags, &$platforms) {
|
|
|
|
if (preg_match('/^https:\/\/itch.io\/games\/tag-/', $link->attr('href'))) {
|
|
|
|
$tags[] = $link->text();
|
|
|
|
}
|
|
|
|
if (preg_match('/^https:\/\/itch.io\/games\/platform-/', $link->attr('href'))) {
|
|
|
|
$platforms[] = $link->text();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
$language_en = Language::findByCode('en');
|
|
|
|
if (!$game->languages()->where('code', 'en')->exists()) {
|
|
|
|
$game->languages()->attach($language_en);
|
|
|
|
}
|
|
|
|
foreach ($tags as $tag) {
|
|
|
|
$model = Tag::where('language_id', $language_en->id)
|
|
|
|
->where('title', $tag)
|
|
|
|
->first();
|
|
|
|
if (!$model) {
|
|
|
|
$model = new Tag();
|
|
|
|
$model->language_id = $language_en->id;
|
|
|
|
$model->title = $tag;
|
|
|
|
$model->save();
|
|
|
|
}
|
|
|
|
$game->tags()->attach($model);
|
|
|
|
}
|
|
|
|
foreach ($platforms as $platform) {
|
|
|
|
$model = Platform::findByName($platform);
|
|
|
|
if (!$game->platforms()->where('title', $platform)->exists()) {
|
|
|
|
$game->platforms()->attach($model);
|
|
|
|
}
|
2019-09-12 20:03:56 +03:00
|
|
|
}
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|