. */ 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()->attach($author_model); foreach ($tags as $t) { $game->tags()->attach($t); } } } } 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"); } }