2020-04-25 06:50:34 -07:00
|
|
|
const fetch = require("node-fetch");
|
|
|
|
|
2020-05-23 13:55:59 -07:00
|
|
|
async function loadPetMetaData(petName) {
|
|
|
|
const url =
|
|
|
|
`http://www.neopets.com/amfphp/json.php/PetService.getPet` + `/${petName}`;
|
|
|
|
const res = await fetch(url);
|
|
|
|
if (!res.ok) {
|
|
|
|
throw new Error(
|
|
|
|
`for pet meta data, neopets.com returned: ` +
|
|
|
|
`${res.status} ${res.statusText}. (${url})`
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
const json = await res.json();
|
|
|
|
return json;
|
|
|
|
}
|
|
|
|
|
|
|
|
async function loadCustomPetData(petName) {
|
2020-05-11 21:19:34 -07:00
|
|
|
const url =
|
2020-04-25 06:50:34 -07:00
|
|
|
`http://www.neopets.com/amfphp/json.php/CustomPetService.getViewerData` +
|
2020-05-11 21:19:34 -07:00
|
|
|
`/${petName}`;
|
|
|
|
const res = await fetch(url);
|
2020-04-25 06:50:34 -07:00
|
|
|
if (!res.ok) {
|
2020-05-11 21:19:34 -07:00
|
|
|
throw new Error(
|
2020-05-23 13:55:59 -07:00
|
|
|
`for custom pet data, neopets.com returned: ` +
|
2020-05-11 21:19:34 -07:00
|
|
|
`${res.status} ${res.statusText}. (${url})`
|
|
|
|
);
|
2020-04-25 06:50:34 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
const json = await res.json();
|
|
|
|
if (!json.custom_pet) {
|
|
|
|
throw new Error(`missing custom_pet data`);
|
|
|
|
}
|
|
|
|
|
|
|
|
return json;
|
|
|
|
}
|
|
|
|
|
2020-05-11 21:19:34 -07:00
|
|
|
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\/(.+?)\/swf\/(.+?)\.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, folders] = match;
|
|
|
|
|
|
|
|
return `http://images.neopets.com/cp/${type}/data/${folders}/manifest.json`;
|
|
|
|
}
|
|
|
|
|
2020-05-23 13:55:59 -07:00
|
|
|
module.exports = { loadPetMetaData, loadCustomPetData, loadAssetManifest };
|