-
Notifications
You must be signed in to change notification settings - Fork 3
/
run.php
executable file
·158 lines (130 loc) · 4.23 KB
/
run.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
157
158
#!/usr/bin/env php
<?php
if(getenv('FORTELLER_EMAIL') === false || getenv('FORTELLER_PASSWORD') === false) {
die("Please set FORTELLER_EMAIL and FORTELLER_PASSWORD in the environment variables.\n");
}
const GAME_BASE_URL = 'https://us-central1-forteller-platform.cloudfunctions.net/games';
const AUDIO_BASE_URL = 'https://us-central1-forteller-platform.cloudfunctions.net/audio/';
const LOGIN_URL = 'https://www.googleapis.com/identitytoolkit/v3/relyingparty/verifyPassword';
const TOKEN_URL = 'https://securetoken.googleapis.com/v1/token?key=';
const APP_KEY = 'AIzaSyAs2zSk1Xx-yq6pu4GNCqOUPLuCD1HPDYo';
function doCurl($url, $type, $header=[], $body="") {
$handle = curl_init();
curl_setopt($handle, CURLOPT_URL, $url);
curl_setopt($handle, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($handle, CURLOPT_CUSTOMREQUEST, $type);
curl_setopt($handle, CURLOPT_HEADER, true);
curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
curl_setopt($handle, CURLOPT_HTTPHEADER, $header);
curl_setopt($handle, CURLOPT_POSTFIELDS, $body);
$response = curl_exec($handle);
$hlength = curl_getinfo($handle, CURLINFO_HEADER_SIZE);
$httpCode = curl_getinfo($handle, CURLINFO_HTTP_CODE);
$body = substr($response, $hlength);
curl_close($handle);
// If HTTP response is not 200, throw exception
if ($httpCode != 200) {
throw new Exception("$httpCode: $body");
}
return $body;
}
function getRefreshToken() {
$response = doCurl(
LOGIN_URL . "?key=" . APP_KEY,
"POST",
['Content-Type: application/json'],
json_encode([
"email" => getenv('FORTELLER_EMAIL'),
"returnSecureToken" => true,
"password"=> getenv('FORTELLER_PASSWORD')
])
);
if (!$response) {
die("ERROR: Recieved empty refresh token.\n");
}
return json_decode($response)->refreshToken;
}
function getAccessToken($refreshToken){
$response = doCurl(
TOKEN_URL . APP_KEY,
'POST',
['Content-Type: application/json'],
json_encode([
"grantType" => "refresh_token",
"refreshToken" => $refreshToken]
)
);
if (!$response) {
die("ERROR: Recieved empty access token.\n");
}
return json_decode($response)->access_token;
}
function login() {
$refreshToken = getRefreshToken();
return getAccessToken($refreshToken);
}
function request($url, $accept = 'application/json') {
global $jwt;
echo "Fetching $url\n";
return doCurl(
$url,
'GET',
[
"Accept: $accept",
"User-Agent: Forteller%20Stage/1593634637 CFNetwork/1237 Darwin/20.4.0",
"Authorization: Bearer $jwt"
]
);
}
// ---------------------------------------
// Main Logic
// ---------------------------------------
if ($argc<2) {
die("Please pass one of the SKUs: ceph_gh ceph_jaws suc_mid1 ceph_fh skg_iso");
}
if (!in_array($argv[1], ['ceph_gh','ceph_jaws','suc_mid1','ceph_fh','skg_iso'])) {
die("Invalid SKU");
}
$basedir = getcwd();
if (isset($argv[2])) {
@mkdir($argv[2]);
$basedir = $argv[2];
echo("Saving files in $basedir\n");
}
$jwt = login();
// Print the JWT token without the Signature
print_r(array_map(function($x) {
return base64_decode($x);
}, array_slice(explode('.', $jwt), 0, 2)));
$sku = $argv[1];
$game = null;
foreach (json_decode(request(GAME_BASE_URL), true) as $game) {
if ($game['sku'] == $sku) {
break;
}
}
$gameName = $game['name'];
@mkdir("$basedir/$gameName");
$containerUrl = GAME_BASE_URL . "/" . $game['id'] . "/containers";
$containers = request($containerUrl);
foreach (json_decode($containers, true) as $c) {
$cid = $c['id'];
$scenarioName = str_replace('#','', $c['name']);
$itemsUrl = $containerUrl . "/$cid/items";
$data = json_decode(request($itemsUrl), true);
foreach ($data as $track) {
$trackName = $track['name'];
$streamUrlParts = explode('/', $track['streamUri']);
$trackUrl = AUDIO_BASE_URL . $streamUrlParts[2] . '/' . $streamUrlParts[3] . '/' . $streamUrlParts[4];
$filename = "$basedir/$gameName/$scenarioName - $trackName.mp3";
echo "$filename\n";
if (!file_exists($filename)) {
$track_data = request($trackUrl, 'audio/mpeg');
if($track_data) {
file_put_contents($filename, $track_data);
} else {
die("Failed to download $trackUrl\n");
}
}
}
}