2021-02-02 22:26:55 -08:00
|
|
|
import fetch from "node-fetch";
|
2020-12-28 14:00:11 -08:00
|
|
|
|
2023-11-11 13:28:47 -08:00
|
|
|
async function loadAssetManifest(manifestUrl, swfUrl) {
|
|
|
|
|
const possibleManifestUrls =
|
|
|
|
|
manifestUrl != null
|
|
|
|
|
? [manifestUrl]
|
|
|
|
|
: convertSwfUrlToPossibleManifestUrls(swfUrl);
|
2021-03-11 02:57:44 -08:00
|
|
|
|
|
|
|
|
const responses = await Promise.all(
|
2023-11-11 13:28:47 -08:00
|
|
|
possibleManifestUrls.map((url) => fetch(url)),
|
2021-03-11 02:57:44 -08:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// Print errors for any responses with unexpected statuses. We'll do this
|
|
|
|
|
// even if other requests succeeded, or failed with an expected 404.
|
|
|
|
|
for (const res of responses) {
|
|
|
|
|
if (!res.ok && res.status !== 404) {
|
|
|
|
|
console.error(
|
|
|
|
|
`for asset manifest, images.neopets.com returned: ` +
|
2023-11-11 13:28:47 -08:00
|
|
|
`${res.status} ${res.statusText}. (${res.url})`,
|
2021-03-11 02:57:44 -08:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const successfulResponse = responses.find((res) => res.ok);
|
|
|
|
|
if (!successfulResponse) {
|
2020-12-28 14:00:11 -08:00
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-11 02:57:44 -08:00
|
|
|
const json = await successfulResponse.json();
|
2020-12-28 14:00:11 -08:00
|
|
|
return {
|
|
|
|
|
assets: json["cpmanifest"]["assets"].map((asset) => ({
|
|
|
|
|
format: asset["format"],
|
|
|
|
|
assetData: asset["asset_data"].map((assetDatum) => ({
|
|
|
|
|
path: assetDatum["url"],
|
|
|
|
|
})),
|
|
|
|
|
})),
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-11 13:28:47 -08:00
|
|
|
const SWF_URL_PATTERN =
|
|
|
|
|
/^https?:\/\/images\.neopets\.com\/cp\/(bio|items)\/swf\/(.+?)_([a-z0-9]+)\.swf$/;
|
2020-12-28 14:00:11 -08:00
|
|
|
|
2021-03-11 02:57:44 -08:00
|
|
|
function convertSwfUrlToPossibleManifestUrls(swfUrl) {
|
2022-08-03 14:36:07 -07:00
|
|
|
const match = new URL(swfUrl, "https://images.neopets.com")
|
2021-03-14 07:16:01 -07:00
|
|
|
.toString()
|
|
|
|
|
.match(SWF_URL_PATTERN);
|
2020-12-28 14:00:11 -08:00
|
|
|
if (!match) {
|
|
|
|
|
throw new Error(`unexpected SWF URL format: ${JSON.stringify(swfUrl)}`);
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-11 02:16:42 -08:00
|
|
|
const type = match[1];
|
|
|
|
|
const folders = match[2];
|
2021-03-11 02:35:13 -08:00
|
|
|
const hash = match[3];
|
2020-12-28 14:00:11 -08:00
|
|
|
|
2021-03-11 02:57:44 -08:00
|
|
|
// TODO: There are a few potential manifest URLs in play! Long-term, we
|
|
|
|
|
// should get this from modeling data. But these are some good guesses!
|
|
|
|
|
return [
|
2022-08-03 14:36:07 -07:00
|
|
|
`https://images.neopets.com/cp/${type}/data/${folders}/manifest.json`,
|
|
|
|
|
`https://images.neopets.com/cp/${type}/data/${folders}_${hash}/manifest.json`,
|
2021-03-11 02:57:44 -08:00
|
|
|
];
|
2020-12-28 14:00:11 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
module.exports = { loadAssetManifest };
|