From 71238f6720b0e6a489d3d11bfcaf712ee2a17f3e Mon Sep 17 00:00:00 2001 From: Ziver Koc Date: Wed, 20 Apr 2022 21:54:16 +0200 Subject: [PATCH] Improved error reporting Failed http requests will now printout server response for easier troubleshooting. Also added new line on each printout. --- run.php | 126 ++++++++++++++++++++++++++++++-------------------------- 1 file changed, 68 insertions(+), 58 deletions(-) diff --git a/run.php b/run.php index 318058d..b809491 100755 --- a/run.php +++ b/run.php @@ -2,7 +2,7 @@ $_ENV['FORTELLER_EMAIL'],"returnSecureToken"=>true,"password"=>$_ENV['FORTELLER_PASSWORD']]); + $response = doCurl( + LOGIN_URL . "?key=" . APP_KEY, + "POST", + ['Content-Type: application/json'], + json_encode([ + "email" => $_ENV['FORTELLER_EMAIL'], + "returnSecureToken" => true, + "password"=> $_ENV['FORTELLER_PASSWORD'] + ]) + ); - curl_setopt_array($curl, [ - CURLOPT_URL => "https://www.googleapis.com/identitytoolkit/v3/relyingparty/verifyPassword?key=" . APP_KEY, - CURLOPT_RETURNTRANSFER => true, - CURLOPT_ENCODING => "", - CURLOPT_MAXREDIRS => 10, - CURLOPT_TIMEOUT => 30, - CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, - CURLOPT_CUSTOMREQUEST => "POST", - CURLOPT_HTTPHEADER => [ - 'Content-Type: application/json', - ], - CURLOPT_POSTFIELDS => $body, - ]); - - $response = curl_exec($curl); - $err = curl_error($curl); - - curl_close($curl); - - if ($err) { - die("cURL Error #:" . $err); + if (!$response) { + die("ERROR: Recieved empty refresh token.\n"); } + return json_decode($response)->refreshToken; } function getAccessToken($refreshToken){ - $curl = curl_init(); - $body = json_encode(["grantType"=>"refresh_token","refreshToken"=>$refreshToken]); + $response = doCurl( + TOKEN_URL . APP_KEY, + 'POST', + ['Content-Type: application/json'], + json_encode([ + "grantType" => "refresh_token", + "refreshToken" => $refreshToken] + ) + ); - curl_setopt_array($curl, [ - CURLOPT_URL => TOKEN_URL . APP_KEY, - CURLOPT_RETURNTRANSFER => true, - CURLOPT_ENCODING => "", - CURLOPT_MAXREDIRS => 10, - CURLOPT_TIMEOUT => 30, - CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, - CURLOPT_CUSTOMREQUEST => "POST", - CURLOPT_HTTPHEADER => [ - 'Content-Type: application/json', - ], - CURLOPT_POSTFIELDS => $body, - ]); - - $response = curl_exec($curl); - $err = curl_error($curl); - - curl_close($curl); - - if ($err) { - die("cURL Error #:" . $err); + if (!$response) { + die("ERROR: Recieved empty access token.\n"); } + return json_decode($response)->access_token; } @@ -76,18 +80,22 @@ function login() { function request($url, $accept = 'application/json') { global $jwt; - $opts = [ - "http" => [ - "method" => "GET", - "header" => "Accept: $accept\r\n" . - "User-Agent: Forteller%20Stage/1593634637 CFNetwork/1237 Darwin/20.4.0\r\n" . - "Authorization: Bearer $jwt\r\n" + + return doCurl( + $url, + 'GET', + [ + "Accept: $accept", + "User-Agent: Forteller%20Stage/1593634637 CFNetwork/1237 Darwin/20.4.0", + "Authorization: Bearer $jwt" ] - ]; - $context = stream_context_create($opts); - return file_get_contents($url, false, $context); + ); } +// --------------------------------------- +// Main Logic +// --------------------------------------- + if ($argc<2) { die("Please pass one of the SKUs: ceph_gh ceph_jaws suc_mid1 ceph_fh skg_iso"); } @@ -131,13 +139,15 @@ foreach (json_decode($containers, true) as $c) { $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"); + die("Failed to download $trackUrl\n"); } } } -} \ No newline at end of file +}