File size: 4,156 Bytes
8d21059
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
<?php

class SteamGridAPI {
	# API Endpoint [Default v2]
	public $endpoint = 'https://www.steamgriddb.com/api/v2';
	# API Token
	private $token = 'API_TOKEN';
	# Database class
	private $db = [];
	# Cache time
	public $cache_time = 60 * 60 * 2;
	# Request timeout
	public $timeout = 5;
	# Default API language (Not supported by API)
	public $lang = 'en';
	# Supported languages by API
	private $langs = ['en', 'it', 'pt'];
	# Libraries
	private $libs = [
		"steam",	// Steam
		"origin",	// Origin
		"egs",		// Epic Games
		"bnet",		// Battle Net
		"uplay",	// uPlay
		"flashpoint",// Flashpoint Archive
		"eshop"		// Nintendo eShop
	];
	
	# Set configs
	public function __construct ($db = [], $lang = 'en') {
		if (getenv('SGRD_API_KEY')) $this->token = getenv('SGRD_API_KEY');
		if (in_array($lang, $this->langs)) $this->lang = $lang;
		if (is_a($db, 'Database') && $db->configs['redis']['status']) $this->db = $db;
		$this->header = [
			'Authorization: Bearer ' . $this->token
		];
	}
	
	# Custom API requests
	public function request (string $method = '', array $args = [], int $timeout = null) {
		if (!isset($this->curl))	$this->curl = curl_init();
		$url = $this->endpoint . '/' . $method . '?' . http_build_query($args);
		if (is_null($timeout)) $timeout = $this->timeout;
		if (is_a($this->db, 'Database')) {
			$cache = $this->db->rget($url);
			if ($r = json_decode($cache, 1)) return $r;
		}
		curl_setopt_array($this->curl, [
			CURLOPT_URL				=> $url,
			CURLOPT_SSL_VERIFYHOST	=> false,
			CURLOPT_SSL_VERIFYPEER	=> false,
			CURLOPT_HTTPHEADER		=> $this->header,
			CURLOPT_HTTPGET			=> true,
			CURLOPT_TIMEOUT			=> $timeout,
			CURLOPT_RETURNTRANSFER	=> true
		]);
		$output = curl_exec($this->curl);
		if ($json_output = json_decode($output, true)) {
			if (is_a($db, 'Database') && $this->db->configs['redis']['status']) $this->db->rset($url, json_encode($json_output), $this->cache_time);
			$json_output['url'] = $url;
			return $json_output;
		}
		if ($output) return [$output];
		if ($error = curl_error($this->curl)) return ['ok' => false, 'error_code' => 500, 'description' => 'CURL Error: ' . $error, 'url' => $url];
		if (!$output) return ['ok' => false, 'error_code' => 500, 'description' => curl_getinfo($this->curl), 'url' => $url];
	}
	
	# Retrieve game by game ID.
	public function getGameByID ($gameId) {
		return $this->request('games/id/' . $gameId);
	}
	
	# Retrieve game by Steam App ID.
	public function getGameBySteamID ($steamAppId, $oneoftag = null) {
		$args = [];
		if (!is_null($oneoftag)) $args['oneoftag'] = $oneoftag;
		return $this->request('games/steam/' . $steamAppId, $args);
	}
	
	# Retrieve grids by game ID.
	public function getGrids ($gameId, $page = 0, $oneoftag = null) {
		$args['types'] = 'animated,static';
		$args['humor'] = $args['epilepsy'] = $args['oneoftag'] = 'any';
		if (!is_null($oneoftag)) $args['oneoftag'] = $oneoftag;
		$args['page'] = $page;
		return $this->request('grids/game/' . $gameId, $args);
	}
	
	# Retrieve heroes by game ID.
	public function getHeroes ($gameId, $page = 0, $oneoftag = null) {
		$args['types'] = 'animated,static';
		$args['humor'] = $args['epilepsy'] = $args['oneoftag'] = 'any';
		if (!is_null($oneoftag)) $args['oneoftag'] = $oneoftag;
		$args['page'] = $page;
		return $this->request('heroes/game/' . $gameId, $args);
	}
	
	# Retrieve logos by game ID.
	public function getLogos ($gameId, $page = 0, $oneoftag = null) {
		$args['types'] = 'animated,static';
		$args['humor'] = $args['epilepsy'] = $args['oneoftag'] = 'any';
		if (!is_null($oneoftag)) $args['oneoftag'] = $oneoftag;
		$args['page'] = $page;
		return $this->request('logos/game/' . $gameId, $args);
	}
	
	# Retrieve icons by game ID.
	public function getIcons ($gameId, $page = 0, $oneoftag = null) {
		$args['types'] = 'animated,static';
		$args['humor'] = $args['epilepsy'] = $args['oneoftag'] = 'any';
		if (!is_null($oneoftag)) $args['oneoftag'] = $oneoftag;
		$args['page'] = $page;
		return $this->request('icons/game/' . $gameId, $args);
	}
	
	# Search for a game by name
	public function getGamesByName ($term) {
		return $this->request('search/autocomplete/' . $term);
	}
}

?>