eve-indy-job-tracker/populate_cache.php

103 lines
2.6 KiB
PHP

<?php
require_once __DIR__ . "/functions.php"; // Ensure this has no conflicts
// Fetch all type IDs from ESI using pagination
function get_all_type_ids()
{
$all_ids = [];
$page = 1;
do {
$url = "https://esi.evetech.net/latest/universe/types/?page=$page";
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($http_code !== 200) {
echo "Failed to fetch type IDs on page $page. HTTP Code: $http_code\n";
break;
}
$ids = json_decode($response, true);
if (empty($ids)) {
break;
}
$all_ids = array_merge($all_ids, $ids);
$page++;
usleep(250000); // Avoid API rate limit
} while (true);
return $all_ids;
}
// Filter IDs to include only blueprints by checking for names ending in "Blueprint"
function filter_blueprint_ids($ids)
{
$blueprint_ids = [];
$chunks = array_chunk($ids, 1000);
foreach ($chunks as $chunk) {
$names = fetch_type_names($chunk);
foreach ($names as $id => $name) {
if (str_ends_with($name, "Blueprint")) {
$blueprint_ids[] = $id;
}
}
usleep(250000);
}
return $blueprint_ids;
}
// Batch fetch type names using /universe/names/
// Populate the blueprint cache
function populate_blueprint_cache()
{
echo "Fetching all type IDs...\n";
$all_type_ids = get_all_type_ids();
if (empty($all_type_ids)) {
echo "No type IDs retrieved.\n";
return;
}
echo "Filtering blueprint IDs...\n";
$blueprint_ids = filter_blueprint_ids($all_type_ids);
if (empty($blueprint_ids)) {
echo "No blueprint IDs found.\n";
return;
}
echo "Fetching blueprint names...\n";
$cached_data = [];
$chunks = array_chunk($blueprint_ids, 1000);
foreach ($chunks as $chunk) {
$batch_names = fetch_type_names($chunk);
foreach ($batch_names as $id => $name) {
$cached_data[$id] = $name;
}
usleep(250000);
}
// Save to JSON cache file
$cache_dir = __DIR__ . "/cache";
if (!is_dir($cache_dir)) {
mkdir($cache_dir, 0775, true);
}
$cache_file = $cache_dir . "/blueprint_cache.json";
file_put_contents(
$cache_file,
json_encode($cached_data, JSON_PRETTY_PRINT)
);
echo "Cache populated with " . count($cached_data) . " blueprint names.\n";
}
// Run it
populate_blueprint_cache();