Last active
February 5, 2022 09:52
-
-
Save deadphoenix8091/75bccdd3c9210ada611f34ae84462f9a to your computer and use it in GitHub Desktop.
Project M json download script
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 | |
function downloadFromApi($endpointUrl) { | |
if (!is_dir('./apicache')) | |
mkdir('./apicache'); | |
if (file_exists('./apicache/' . md5($endpointUrl))) { | |
$content = file_get_contents('./apicache/' . md5($endpointUrl)); | |
} else { | |
$baseApiUrl = 'https://flyff-api.sniegu.fr'; | |
$retryCount = 0; | |
$downloadSuccess = false; | |
$content = ''; | |
while (!$content) { | |
if ($retryCount < 15) { | |
usleep(250); | |
$content = @file_get_contents($baseApiUrl . $endpointUrl); | |
if ($content) { | |
$downloadSuccess = true; | |
} | |
$retryCount++; | |
} | |
} | |
if (!$downloadSuccess) { | |
return false; | |
} | |
file_put_contents('./apicache/' . md5($endpointUrl), $content); | |
} | |
return json_decode($content, true); | |
} | |
function batchDownloadFromApi ($dataTypeApiPrefix, $dataCallbackFunction) { | |
$itemIds = downloadFromApi($dataTypeApiPrefix); | |
$batchItemIds = []; | |
for($i = 0; $i < count($itemIds); $i++) { | |
array_push($batchItemIds, $itemIds[$i]); | |
if ((count($batchItemIds) == 25) || ($i == (count($itemIds) - 1))) { | |
$returnedData = downloadFromApi($dataTypeApiPrefix . '/' . implode(',', $batchItemIds)); | |
if (count($returnedData) > 0) { | |
if (is_callable($dataCallbackFunction)) { | |
foreach($returnedData as $currentReturnedElement) { | |
$dataCallbackFunction($currentReturnedElement); | |
} | |
} | |
} | |
echo ($i+1) . " of " . count($itemIds) . " objects processed.".PHP_EOL; | |
$batchItemIds = []; | |
} | |
} | |
} | |
$monsterByDroppingItem = []; | |
$endpoints = [[ | |
'url' => '/monster', | |
'postProcessing' => null, | |
],[ | |
'url' => '/item', | |
'postProcessing' => null, | |
],[ | |
'url' => '/world', | |
'postProcessing' => null, | |
],[ | |
'url' => '/class', | |
'postProcessing' => null, | |
],[ | |
'url' => '/equipset', | |
'postProcessing' => null, | |
],[ | |
'url' => '/skill', | |
'postProcessing' => null, | |
],[ | |
'url' => '/partyskill', | |
'postProcessing' => null, | |
],[ | |
'url' => '/npc', | |
'postProcessing' => null, | |
],[ | |
'url' => '/quest', | |
'postProcessing' => null, | |
],[ | |
'url' => '/karma', | |
'postProcessing' => null, | |
],[ | |
'url' => '/achievement', | |
'postProcessing' => null, | |
]]; | |
$timeStart = microtime(true); | |
foreach($endpoints as $currentEndpoint) { | |
echo "Downloading ".$currentEndpoint['url'].PHP_EOL; | |
batchDownloadFromApi($currentEndpoint['url'], function ($currentItem) use ($currentEndpoint) { | |
if (!is_dir('./content')) mkdir('./content'); | |
if (!is_dir('./content'. $currentEndpoint['url'] .'s')) mkdir('./content'. $currentEndpoint['url'] .'s'); | |
$currentItem['flyffdb_meta_id'] = substr($currentEndpoint['url'] . '_' . $currentItem['id'], 1); | |
//make sure we dont use these fields for input data because they cant be indexed as in api | |
$fulltextSearchFields = ['title', 'description', 'slug', 'text']; | |
foreach($fulltextSearchFields as $currentFieldToAvoid) { | |
if (isset($currentItem[$currentFieldToAvoid])) { | |
$currentItem['raw_' . $currentFieldToAvoid] = $currentItem[$currentFieldToAvoid]; | |
unset($currentItem[$currentFieldToAvoid]); | |
} | |
} | |
if (is_callable($currentEndpoint['postProcessing'])) { | |
$currentEndpoint['postProcessing']($currentItem); | |
} | |
file_put_contents('./content'. $currentEndpoint['url'] .'s' . $currentEndpoint['url'] . '_' . $currentItem['id'] . '.json', json_encode($currentItem, JSON_PRETTY_PRINT)); | |
}); | |
} | |
echo "Took: " . (microtime(true) - $timeStart)/60 . 'minutes'; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment