impress-2020/src/server/neopets-assets.js
2021-03-11 02:35:13 -08:00

47 lines
1.3 KiB
JavaScript

import fetch from "node-fetch";
async function loadAssetManifest(swfUrl) {
const manifestUrl = convertSwfUrlToManifestUrl(swfUrl);
const res = await fetch(manifestUrl);
if (res.status === 404) {
return null;
} else if (!res.ok) {
throw new Error(
`for asset manifest, images.neopets.com returned: ` +
`${res.status} ${res.statusText}. (${manifestUrl})`
);
}
const json = await res.json();
return {
assets: json["cpmanifest"]["assets"].map((asset) => ({
format: asset["format"],
assetData: asset["asset_data"].map((assetDatum) => ({
path: assetDatum["url"],
})),
})),
};
}
const SWF_URL_PATTERN = /^http:\/\/images\.neopets\.com\/cp\/(bio|items)\/swf\/(.+?)_([a-z0-9]+)\.swf$/;
function convertSwfUrlToManifestUrl(swfUrl) {
const match = swfUrl.match(SWF_URL_PATTERN);
if (!match) {
throw new Error(`unexpected SWF URL format: ${JSON.stringify(swfUrl)}`);
}
const type = match[1];
const folders = match[2];
const hash = match[3];
if (type === "bio") {
return `http://images.neopets.com/cp/bio/data/${folders}_${hash}/manifest.json`;
} else if (type === "items") {
return `http://images.neopets.com/cp/items/data/${folders}/manifest.json`;
} else {
throw new Error(`Assertion error: type should be bio or item.`);
}
}
module.exports = { loadAssetManifest };