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

113 lines
4.3 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\Tag;
use \App\Models\Language;
use \App\Source;
class Gamejolt extends Source {
public $title = "GameJolt";
public $keyword = 'gamejolt';
public function parse_tag($url) {
$data = json_decode($this->get_text($url));
if (empty($data) or !isset($data->payload)) {
echo 'GameJolt data empty';
return;
}
$language = Language::findByCode('en');
$games = $data->payload->games;
if (count($games) > 0) {
foreach ($games as $gameData) {
$descUrl = 'https://gamejolt.com/site-api/web/discover/games/'.$gameData->id;
$descData = json_decode($this->get_text($descUrl));
$game = new Game;
$game->source_id = $gameData->id;
$game->title = $gameData->title;
$game = $this->findGame($game);
$author_name = $gameData->developer->display_name;
$author_model = Author::findByName($author_name);
if (empty($author_model)) {
$author_model = new Author();
$author_model->name = $author_name;
$author_model->url = $gameData->developer->web_site;
$author_model->save();
}
$timestamp = floor($gameData->published_on / 1000);
$game->release_date = new \DateTime('@'.$timestamp);
$content = $descData->payload->game->description_content;
$content = json_decode($content);
if (isset($descData->payload->metaDescription)) {
$game->short_description = $descData->payload->metaDescription;
}
$tags = [];
foreach ($content->content as $bit) {
if ($bit->type === 'text') {
$game->description = $bit->content;
}
elseif($bit->type === 'paragraph') {
$game->description = '';
foreach ($bit->content as $paragraph) {
if (isset($paragraph->text)) {
$game->description .= $paragraph->text;
}
if (isset($paragraph->marks)) {
foreach ($paragraph->marks as $mark) {
if (isset($mark->attrs) && isset($mark->attrs->tag)) {
$tag_model = Tag::findByName($mark->attrs->tag, $language);
if (!$tag_model) {
$tag_model = new Tag;
$tag_model->title = $mark->attrs->tag;
$tag_model->language_id = $language->id;
$tag_model->save();
}
$tags[] = $tag_model;
}
}
}
}
}
elseif ($bit->type === 'tag') {
$tag = $bit->attrs->tag;
$tag_model = Tag::findByName($tag, $language);
$tags[] = $tag_model;
}
}
$game->url = 'https://gamejolt.com/games/'.$gameData->slug.'/'.$gameData->id;
$game->save();
$game->authors()->sync([$author_model->id]);
foreach ($tags as $t) {
$game->tags()->syncWithoutDetaching([$t->id]);
}
$game->guessPlatform();
}
}
}
public function parse() {
$this->parse_tag("https://gamejolt.com/site-api/web/search/games/?q=twine");
$this->parse_tag("https://gamejolt.com/site-api/web/search/games/?q=#twine");
$this->parse_tag("https://gamejolt.com/site-api/web/search/games/?q=renpy");
$this->parse_tag("https://gamejolt.com/site-api/web/search/games/?q=#renpy");
$this->parse_tag("https://gamejolt.com/site-api/web/search/games/?q=#text");
$this->parse_tag("https://gamejolt.com/site-api/web/search/games/?q=#ascii");
}
}