Get rid of info.js

This commit is contained in:
Jordan Eldredge 2020-04-24 22:52:55 -04:00
parent cb379c6b23
commit 82766c1d08
4 changed files with 32 additions and 48 deletions

View file

@ -5,6 +5,10 @@ const iaItems = db.get("internetArchiveItems");
const S3 = require("../s3");
const logger = require("../logger");
const CLASSIC_QUERY = {
type: "CLASSIC",
};
const TWEETABLE_QUERY = {
tweeted: { $ne: true },
approved: true,
@ -30,7 +34,7 @@ function getSkinRecord(skin) {
imageHash,
uploader,
} = skin;
const fileNames = filePaths.map((p) => path.basename(p));
const fileNames = filePaths.map(p => path.basename(p));
const skinUrl = `https://s3.amazonaws.com/webamp-uploaded-skins/skins/${md5}.wsz`;
return {
skinUrl,
@ -150,6 +154,10 @@ function getTweetableSkinCount() {
return skins.count(TWEETABLE_QUERY);
}
function getClassicSkinCount() {
return skins.count(CLASSIC_QUERY);
}
async function markAsTweeted(md5) {
await skins.findOneAndUpdate({ md5 }, { $set: { tweeted: true } });
return S3.markAsTweeted(md5);
@ -217,13 +225,13 @@ async function reconcile() {
S3.getAllTweeted(),
]);
await Promise.all([
...approved.map((md5) =>
...approved.map(md5 =>
skins.findOneAndUpdate({ md5 }, { $set: { approved: true } })
),
...rejected.map((md5) =>
...rejected.map(md5 =>
skins.findOneAndUpdate({ md5 }, { $set: { rejected: true } })
),
...tweeted.map((md5) =>
...tweeted.map(md5 =>
skins.findOneAndUpdate({ md5 }, { $set: { tweeted: true } })
),
]);
@ -233,6 +241,17 @@ async function setImageHash(md5, imageHash) {
await skins.findOneAndUpdate({ md5 }, { $set: { imageHash } });
}
async function getRandomClassicSkinMd5() {
const random = await skins.aggregate([
{ $match: CLASSIC_QUERY },
{ $sample: { size: 1 } },
]);
if (random.length === 0) {
return null;
}
return random[0].md5;
}
module.exports = {
addSkin,
getMd5sMatchingImageHash,
@ -253,4 +272,6 @@ module.exports = {
reconcile,
getSkinToTweet,
setImageHash,
getClassicSkinCount,
getRandomClassicSkinMd5,
};

View file

@ -1,15 +1,11 @@
const Utils = require("../utils");
const { getCache } = require("../info");
const { getRandomClassicSkinMd5 } = require("../../data/skins");
async function handler(message) {
const cache = getCache();
const skins = Object.values(cache).filter((skin) => skin.type === "CLASSIC");
const skin = skins[Math.floor(Math.random() * skins.length)];
const { md5 } = skin;
const md5 = await getRandomClassicSkinMd5();
await Utils.postSkin({
md5,
dest: message.channel,
dest: message.channel
});
}
@ -17,5 +13,5 @@ module.exports = {
usage: "",
description: "Show information about a random skin",
command: "random",
handler,
handler
};

View file

@ -1,15 +1,8 @@
const { getCache } = require("../info");
const { getStats } = require("../../data/skins");
const { getStats, getClassicSkinCount } = require("../../data/skins");
async function handler(message) {
const info = getCache();
let classic = 0;
let classic = await getClassicSkinCount();
const { tweeted, approved, rejected, tweetable } = await getStats();
Object.values(info).forEach((skin) => {
if (skin.type === "CLASSIC") {
classic++;
}
});
await message.channel.send(`Unique Skins: ${classic.toLocaleString()}
Tweeted: ${tweeted.toLocaleString()}
Rejected: ${rejected.toLocaleString()}
@ -22,5 +15,5 @@ module.exports = {
command: "stats",
handler,
usage: "",
description: "Give some statistics about the skin archive",
description: "Give some statistics about the skin archive"
};

View file

@ -1,26 +0,0 @@
const path = require("path");
const fs = require("fs");
let cache = null;
function getCache() {
if (cache == null) {
cache = JSON.parse(
fs.readFileSync(path.join(__dirname, "../info.json"), "utf8")
);
}
return cache;
}
// TODO: Make async and rewriting using DB
function getInfo(md5) {
return getCache()[md5];
}
// TODO: Make async and rewriting using DB
function getFilename(md5) {
const info = getInfo(md5);
return info.filePaths.map((filepath) => path.basename(filepath))[0];
}
module.exports = { getInfo, getCache, getFilename };