fix cache-asset-manifests script

Just wanted to run it and see if much has been converted since we last checked!
This commit is contained in:
Emi Matchu 2020-12-28 14:00:11 -08:00
parent 1d498ef960
commit fd864ab8ec
3 changed files with 42 additions and 39 deletions

View file

@ -13,7 +13,7 @@ const { argv } = require("yargs");
const PromisePool = require("es6-promise-pool");
const connectToDb = require("../src/server/db");
const neopets = require("../src/server/neopets");
const neopetsAssets = require("../src/server/neopets-assets");
async function cacheAssetManifests(db) {
const [
@ -30,7 +30,7 @@ async function cacheAssetManifests(db) {
async function cacheAssetManifest(row) {
try {
let manifest = await neopets.loadAssetManifest(row.url);
let manifest = await neopetsAssets.loadAssetManifest(row.url);
// After loading, write the new manifest. We make sure to write an empty
// string if there was no manifest, to signify that it doesn't exist, so

View file

@ -0,0 +1,39 @@
const fetch = require("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\/(.+?)\/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`;
}
module.exports = { loadAssetManifest };

View file

@ -1,5 +1,5 @@
const fetch = require("node-fetch");
const { gql } = require("apollo-server");
const { loadAssetManifest } = require("../neopets-assets");
const typeDefs = gql`
enum LayerImageSize {
@ -202,29 +202,6 @@ const resolvers = {
},
};
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"],
})),
})),
};
}
async function loadAndCacheAssetManifest(db, layer) {
let manifest = await loadAssetManifest(layer.url);
@ -255,17 +232,4 @@ async function loadAndCacheAssetManifest(db, layer) {
return manifest;
}
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`;
}
module.exports = { typeDefs, resolvers };