Last active
March 21, 2022 22:49
-
-
Save Subtixx/ecde36df90f4e142d454e1e255794243 to your computer and use it in GitHub Desktop.
Steam Workshop downloader
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
* Usage Multiples: | |
* http://localhost/steamworkshop.php?ids[]=779297608&ids[]=468207596&ids[]=568872931&ids[]=898613611 | |
* Usage Single: | |
* http://localhost/steamworkshop.php?id=779297608 | |
*/ | |
set_time_limit(0); | |
define("API_URL", "http://api.steampowered.com/ISteamRemoteStorage/GetPublishedFileDetails/v0001/"); | |
function getWorkshopFileUrl($id) | |
{ | |
$ch = curl_init(API_URL); | |
curl_setopt($ch, CURLOPT_HEADER, 0); | |
curl_setopt($ch, CURLOPT_POST, 1); | |
curl_setopt($ch, CURLOPT_POSTFIELDS, "itemcount=1&publishedfileids[0]=" . $id . "&format=json"); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); | |
$output = curl_exec($ch); | |
$sharedfile = json_decode($output); | |
curl_close($ch); | |
if(!isset($sharedfile->response)) | |
return null; | |
if(!isset($sharedfile->response->publishedfiledetails) || sizeof($sharedfile->response->publishedfiledetails) == 0) | |
return null; | |
return array( | |
"url" => $sharedfile->response->publishedfiledetails[0]->file_url, | |
"name" => $sharedfile->response->publishedfiledetails[0]->filename | |
); | |
} | |
function getWorkshopFileDetails($num, $ids) | |
{ | |
$ch = curl_init(API_URL); | |
curl_setopt($ch, CURLOPT_HEADER, 0); | |
curl_setopt($ch, CURLOPT_POST, 1); | |
curl_setopt($ch, CURLOPT_POSTFIELDS, "itemcount=" . $num . "&" . $ids . "&format=json"); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); | |
$output = curl_exec($ch); | |
$sharedfile = json_decode($output); | |
curl_close($ch); | |
if(!isset($sharedfile->response)) | |
return null; | |
if(!isset($sharedfile->response->publishedfiledetails) || sizeof($sharedfile->response->publishedfiledetails) == 0) | |
return null; | |
return $sharedfile->response->publishedfiledetails; | |
} | |
function convertIdsToParameter($ids) | |
{ | |
$res = array(); | |
for ($i = 0; $i < sizeof($ids); $i++) | |
array_push($res, "publishedfileids[" . $i . "]=" . $ids[$i]); | |
return join("&", $res); | |
} | |
function downloadFileTemp($fileUrl, $fileName, $method = 0) | |
{ | |
//$tempFileName = sys_get_temp_dir() . "/" . $fileName; | |
$tempFileName = "temp/" . $fileName; | |
if ($method == 0) { | |
if (!file_put_contents($tempFileName, fopen($fileUrl, 'r'))) | |
throw new Exception("Downloading file failed."); | |
} elseif ($method == 1) { | |
$fp = fopen($tempFileName, "w"); | |
$ch = curl_init(); | |
$options = array( | |
CURLOPT_FILE => $fp, | |
CURLOPT_TIMEOUT => 28800, | |
CURLOPT_URL => $fileUrl, | |
); | |
curl_setopt_array($ch, $options); | |
curl_exec($ch); | |
curl_close($ch); | |
fclose($fp); | |
} | |
return $tempFileName; | |
} | |
if (isset($_GET['id'])) { | |
$id = $_GET['id']; | |
if (!is_numeric($id)) | |
exit("Unsupported ID."); | |
$file = getWorkshopFileUrl($id); | |
if($file == null) | |
die("File not found."); | |
$fileUrl = $file['url']; | |
$fileName = $file['name']; | |
header('Content-Type: application/octet-stream'); | |
header("Content-Transfer-Encoding: Binary"); | |
header("Content-disposition: attachment; filename=\"" . $fileName . "\""); | |
if( ini_get('allow_url_fopen') ) { | |
readfile($fileUrl); | |
exit(); | |
}else{ | |
exit("Not supported."); | |
} | |
} elseif (isset($_GET['ids'])) { | |
$ids = $_GET['ids']; | |
if (!is_array($ids)) | |
exit("Unsupported."); | |
$files = getWorkshopFileDetails(sizeof($ids), convertIdsToParameter($ids)); | |
if($files == null) | |
die("Not supported."); | |
$fileNames = array(); | |
foreach ($files as $file) { | |
$fileNames[] = downloadFileTemp($file->file_url, basename($file->filename)); | |
} | |
$zipname = time() . "_" . $_SERVER['REMOTE_ADDR'] . ".zip"; | |
$zip = new ZipArchive; | |
if(!$zip->open($zipname, ZipArchive::CREATE)) | |
die("Error creating zip.."); | |
foreach ($fileNames as $file) { | |
$zip->addFile($file, basename($file)); | |
} | |
$zip->close(); | |
foreach ($fileNames as $file) { | |
@unlink($file); | |
} | |
header('Content-Type: application/zip'); | |
header('Content-disposition: attachment; filename=' . $zipname); | |
header('Content-Length: ' . filesize($zipname)); | |
readfile($zipname); | |
@unlink($zipname); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment