-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathmetacritic.php
156 lines (130 loc) · 5.06 KB
/
metacritic.php
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
<?php
namespace Metacritic\API;
use Unirest;
require_once 'libs/Unirest.php';
require_once 'libs/simple_html_dom.php';
class MetacriticAPI
{
private $response_body = "";
private $baseUrl = "http://www.metacritic.com/game/";
private $arrSystems = array();
public function __construct($system = "pc")
{
$this->arrSystems[] = $system;
}
public function getMetacriticPage($game_name)
{
$returnValue = "";
# Remove spaces
$game_name = trim($game_name);
# convert spaces to -
$game_name = str_replace(' ', '-', $game_name);
# Remove &<space>
$game_name = str_replace('& ', '', $game_name);
# lowercase
$game_name = strtolower($game_name);
# Remove all special chars execept a-z, digits, --sign, ?-sign, !-sign
$game_name = preg_replace('/[^a-z\d\?!\-]/', '', $game_name);
# Get the webpage
$i = 0;
do {
$system = $this->arrSystems[$i++];
$url = $this->baseUrl . $system . "/" . $game_name;
$response = Unirest\Request::get($url, $headers = array(), $parameters = null);
} while ($response->code <> 200 and $i < count($this->arrSystems));
if ($response->code == 200) {
$returnValue = $response->raw_body;
}
$this->response_body = $returnValue;
}
public function getMetacriticScores()
{
# Get DOM by string content
$html = str_get_html($this->response_body);
# Define json output array
$json_output = array();
$error = false;
# init all vars
$name = "";
$metascritic_score = 0;
$user_score = 0.0;
$rating = "";
$developer = "";
$publisher = "";
$genres = "";
$release_date = "";
$image_url = "";
$cheat_url = "";
if (!$html) {
$json_output['error'] = "Page could not be loaded!";
$error = true;
}
if (!$error) {
foreach ($html->find('div.product_title h1') as $element) {
$name = trim($element->plaintext);
}
foreach ($html->find('div.metascore_w.game span') as $element) {
$metascritic_score = intval($element->plaintext);
}
foreach ($html->find("div.metascore_w.user.large.game") as $element) {
$user_score = floatval($element->plaintext);
}
foreach ($html->find('li.summary_detail.product_rating span.data') as $element) {
$rating = trim($element->plaintext);
}
$genres = array();
foreach ($html->find('li.summary_detail.product_genre span.data') as $element) {
array_push($genres, trim($element->plaintext));
}
foreach ($html->find('li.summary_detail.developer span.data') as $element) {
$developer = trim($element->plaintext);
}
$developers = explode(", ", $developer);
foreach ($html->find('li.summary_detail.publisher span.data a') as $element) {
$publisher = trim($element->plaintext);
}
foreach ($html->find('li.summary_detail.release_data span.data') as $element) {
$release_date = trim($element->plaintext);
}
$also_on = array();
$also_on_url = array();
foreach ($html->find('li.summary_detail.product_platforms span.data a') as $element) {
array_push($also_on, trim($element->plaintext));
array_push($also_on_url, $element->href);
}
foreach ($html->find('img.product_image.large_image') as $element) {
$image_url = $element->src;
}
foreach ($html->find('li.summary_detail.product_cheats span.data a') as $element) {
$cheat_url = $element->href;
}
# Prevent memory leak
$html->clear();
unset($html);
# Fill-in the array
$json_output['name'] = $name;
$json_output['metascritic_score'] = $metascritic_score;
$json_output['users_score'] = $user_score;
$json_output['rating'] = $rating;
$json_output['genres'] = $genres;
$json_output['developers'] = $developers;
$json_output['publishers'] = $publisher;
$json_output['release_date'] = $release_date;
$json_output['also_on'] = $also_on;
$json_output['also_on_url'] = $also_on_url;
$json_output['thumbnail_url'] = $image_url;
$json_output['cheat_url'] = $cheat_url;
}
# Return JSON format
return json_encode($json_output);
}
}
if ($_SERVER['SCRIPT_FILENAME'] == __FILE__) {
if (isset($_GET['game_title'])) {
$metacritic_api = new MetacriticAPI();
$metacritic_api->getMetacriticPage($_GET['game_title']);
echo $metacritic_api->getMetacriticScores();
} else {
echo json_encode(array("error" => "Game title is empty"));
}
}