1
0
Fork 0

Okay, not _everything_ is on the new manifest URLs

Gonna have to start guessing stuff now!
This commit is contained in:
Emi Matchu 2021-03-11 02:57:44 -08:00
parent e220b4e5e1
commit dc3008a675

View file

@ -1,18 +1,29 @@
import fetch from "node-fetch"; import fetch from "node-fetch";
async function loadAssetManifest(swfUrl) { async function loadAssetManifest(swfUrl) {
const manifestUrl = convertSwfUrlToManifestUrl(swfUrl); const possibleManifestUrls = convertSwfUrlToPossibleManifestUrls(swfUrl);
const res = await fetch(manifestUrl);
if (res.status === 404) { const responses = await Promise.all(
return null; possibleManifestUrls.map((url) => fetch(url))
} else if (!res.ok) { );
throw new Error(
`for asset manifest, images.neopets.com returned: ` + // Print errors for any responses with unexpected statuses. We'll do this
`${res.status} ${res.statusText}. (${manifestUrl})` // 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: ` +
`${res.status} ${res.statusText}. (${res.url})`
);
}
} }
const json = await res.json(); const successfulResponse = responses.find((res) => res.ok);
if (!successfulResponse) {
return null;
}
const json = await successfulResponse.json();
return { return {
assets: json["cpmanifest"]["assets"].map((asset) => ({ assets: json["cpmanifest"]["assets"].map((asset) => ({
format: asset["format"], format: asset["format"],
@ -25,7 +36,7 @@ async function loadAssetManifest(swfUrl) {
const SWF_URL_PATTERN = /^http:\/\/images\.neopets\.com\/cp\/(bio|items)\/swf\/(.+?)_([a-z0-9]+)\.swf$/; const SWF_URL_PATTERN = /^http:\/\/images\.neopets\.com\/cp\/(bio|items)\/swf\/(.+?)_([a-z0-9]+)\.swf$/;
function convertSwfUrlToManifestUrl(swfUrl) { function convertSwfUrlToPossibleManifestUrls(swfUrl) {
const match = swfUrl.match(SWF_URL_PATTERN); const match = swfUrl.match(SWF_URL_PATTERN);
if (!match) { if (!match) {
throw new Error(`unexpected SWF URL format: ${JSON.stringify(swfUrl)}`); throw new Error(`unexpected SWF URL format: ${JSON.stringify(swfUrl)}`);
@ -35,13 +46,12 @@ function convertSwfUrlToManifestUrl(swfUrl) {
const folders = match[2]; const folders = match[2];
const hash = match[3]; const hash = match[3];
if (type === "bio") { // TODO: There are a few potential manifest URLs in play! Long-term, we
return `http://images.neopets.com/cp/bio/data/${folders}_${hash}/manifest.json`; // should get this from modeling data. But these are some good guesses!
} else if (type === "items") { return [
return `http://images.neopets.com/cp/items/data/${folders}/manifest.json`; `http://images.neopets.com/cp/${type}/data/${folders}/manifest.json`,
} else { `http://images.neopets.com/cp/${type}/data/${folders}_${hash}/manifest.json`,
throw new Error(`Assertion error: type should be bio or item.`); ];
}
} }
module.exports = { loadAssetManifest }; module.exports = { loadAssetManifest };