diff --git a/index.js b/index.js index 32ec392..c884627 100644 --- a/index.js +++ b/index.js @@ -1,14 +1,12 @@ const { name, version } = require("./package.json") -const { ada1Mods } = require("./src/tweet-types/ada-1-mods.js") -const { banshee44Mods } = require("./src/tweet-types/banshee-44-mods.js") +const { mods } = require("./src/tweet-types/mods.js") const { xurExotics } = require("./src/tweet-types/xur-exotics") const { highStatLegendaryArmor } = require("./src/tweet-types/high-stat-legendary-armor.js") exports.handler = async (event, context, callback) => { console.log(`${name} ${version} called`) - let ada1ModsResult - let banshee44ModsResult + let modsResult let xurExoticsResult let ada1LegendaryArmorResults let theDrifterLegendaryArmorResults @@ -19,15 +17,8 @@ exports.handler = async (event, context, callback) => { let xurLegendaryArmorResults try { - ada1ModsResult = await ada1Mods() - console.log({ ada1ModsResult }) - } catch (error) { - console.log(error) - } - - try { - banshee44ModsResult = await banshee44Mods() - console.log({ banshee44ModsResult }) + modsResult = await mods() + console.log({ modsResult }) } catch (error) { console.log(error) } @@ -88,8 +79,7 @@ exports.handler = async (event, context, callback) => { console.log(error) } - const result = ada1ModsResult - && banshee44ModsResult + const result = modsResult && xurExoticsResult && ada1LegendaryArmorResults && theDrifterLegendaryArmorResults diff --git a/src/tweet-types/ada-1-mods.js b/src/tweet-types/ada-1-mods.js deleted file mode 100644 index d722394..0000000 --- a/src/tweet-types/ada-1-mods.js +++ /dev/null @@ -1,39 +0,0 @@ -const { getVendorInventory } = require("../integrations/destiny-insights-backend.js") -const { addItem, getLastSoldItems } = require("../integrations/dynamodb.js") -const { getModInfo } = require("../util/get-mod-info.js") -const { isNewInventory } = require ("../util/is-new-inventory.js") -const { tweet } = require("../integrations/twitter.js") - -module.exports.getAda1TweetMessage = (mod1Info, mod2Info) => { - return `Ada-1 is selling: - -${mod1Info} - -${mod2Info} - -#Destiny2 #TwitterBot` -} - -module.exports.ada1Mods = async () => { - let result - const { inventory: { mods: currentMods } } = await getVendorInventory("ada-1") - const lastSoldMods = await getLastSoldItems("ada-1", 1) - const newInventory = await isNewInventory(currentMods, lastSoldMods) - - if (newInventory) { - const timestamp = new Date().toISOString() - for (const mod of currentMods) { - mod.source = "ada-1" - await addItem(mod, timestamp) - } - const [mod1, mod2] = currentMods - const mod1Info = await getModInfo(mod1) - const mod2Info = await getModInfo(mod2) - const message = this.getAda1TweetMessage(mod1Info, mod2Info) - await tweet(message) - result = `Tweeted:\n${message}` - } else { - result = "New Ada-1 mods tweet is not ready" - } - return result -} diff --git a/src/tweet-types/banshee-44-mods.js b/src/tweet-types/banshee-44-mods.js deleted file mode 100644 index 1ed3160..0000000 --- a/src/tweet-types/banshee-44-mods.js +++ /dev/null @@ -1,39 +0,0 @@ -const { getVendorInventory } = require("../integrations/destiny-insights-backend.js") -const { addItem, getLastSoldItems } = require("../integrations/dynamodb.js") -const { getModInfo } = require("../util/get-mod-info.js") -const { isNewInventory } = require ("../util/is-new-inventory.js") -const { tweet } = require("../integrations/twitter.js") - -module.exports.getBanshee44TweetMessage = (mod1Info, mod2Info) => { - return `Banshee-44 is selling: - -${mod1Info} - -${mod2Info} - -#Destiny2 #TwitterBot` -} - -module.exports.banshee44Mods = async () => { - let result - const { inventory: { mods: currentMods } } = await getVendorInventory("banshee-44") - const lastSoldMods = await getLastSoldItems("banshee-44", 1) - const newInventory = await isNewInventory(currentMods, lastSoldMods) - - if (newInventory) { - const timestamp = new Date().toISOString() - for (const mod of currentMods) { - mod.source = "banshee-44" - await addItem(mod, timestamp) - } - const [mod1, mod2] = currentMods - const mod1Info = await getModInfo(mod1) - const mod2Info = await getModInfo(mod2) - const message = this.getBanshee44TweetMessage(mod1Info, mod2Info) - await tweet(message) - result = `Tweeted:\n${message}` - } else { - result = "New Banshee-44 mods tweet is not ready" - } - return result -} diff --git a/src/tweet-types/mods.js b/src/tweet-types/mods.js new file mode 100644 index 0000000..aa3436a --- /dev/null +++ b/src/tweet-types/mods.js @@ -0,0 +1,51 @@ +const { getVendorInventory } = require("../integrations/destiny-insights-backend.js") +const { addItem, getLastSoldItems } = require("../integrations/dynamodb.js") +const { isNewInventory } = require ("../util/is-new-inventory.js") +const { tweet } = require("../integrations/twitter.js") + +module.exports.getmodTweetMessage = (ada1Mod1, ada1Mod2, banshee44Mod1, banshee44Mod2) => { + return `Ada-1 mods for sale: +- ${ada1Mod1} +- ${ada1Mod2} + +Banshee-44 mods for sale: +- ${banshee44Mod1} +- ${banshee44Mod2} + +Mod sale stats available at https://destinyinsights.com + +#Destiny2 #TwitterBot` +} + +module.exports.mods = async () => { + let result + const { inventory: { mods: currentAda1Mods } } = await getVendorInventory("ada-1") + const lastSoldAda1Mods = await getLastSoldItems("ada-1", 1) + const newAda1Inventory = await isNewInventory(currentAda1Mods, lastSoldAda1Mods) + + if (newAda1Inventory) { + const timestamp = new Date().toISOString() + for (const mod of currentAda1Mods) { + mod.source = "ada-1" + await addItem(mod, timestamp) + } + const ada1Mod1 = currentAda1Mods[0].name + const ada1Mod2 = currentAda1Mods[1].name + + const { inventory: { mods: currentBanshee44Mods } } = await getVendorInventory("banshee-44") + for (const mod of currentBanshee44Mods) { + mod.source = "banshee-44" + await addItem(mod, timestamp) + } + const banshee44Mod1 = currentBanshee44Mods[0].name + const banshee44Mod2 = currentBanshee44Mods[1].name + + const message = this.getmodTweetMessage(ada1Mod1, ada1Mod2, banshee44Mod1, banshee44Mod2) + + await tweet(message) + result = `Tweeted:\n${message}` + } else { + result = "New mods tweet is not ready" + } + return result +} diff --git a/src/util/get-mod-info.js b/src/util/get-mod-info.js deleted file mode 100644 index 8e9d88f..0000000 --- a/src/util/get-mod-info.js +++ /dev/null @@ -1,39 +0,0 @@ -const { getModSalesInLastYear } = require("../integrations/dynamodb.js") -const { getOrdinal } = require("../util/get-ordinal.js") - -module.exports.getModInfo = async (mod) => { - const modSales = await getModSalesInLastYear(mod) - const timesSoldInLastYear = modSales.length - - let timesSoldInLastYearMessage = `Sold ${timesSoldInLastYear} times in the last year` - if (mod && timesSoldInLastYear === 1) { - timesSoldInLastYearMessage = "First time sold in the last year" - } - - let lastSoldDateMessage = undefined - if (timesSoldInLastYear > 1) { - // eslint-disable-next-line max-len - const months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"] - // eslint-disable-next-line newline-per-chained-call - const lastSoldDate = new Date(modSales[1]).toISOString().split("T")[0] - const year = lastSoldDate.substring(0, 4) - // monthNumber - 1 to account for zero based array counting - const monthNumber = Number(lastSoldDate.substring(5, 7)) - 1 - const month = months[monthNumber] - let day = lastSoldDate.substring(8) - if (day.startsWith("0")) { - day = day.substring(1) - } - const ordinal = getOrdinal(day) - lastSoldDateMessage = `Last sold ${month} ${day}${ordinal} ${year}` - } - - let message = `${mod.name} -- ${timesSoldInLastYearMessage}` - - if (mod && timesSoldInLastYear > 1) { - message += `\n- ${lastSoldDateMessage}` - } - - return message -} diff --git a/src/util/is-new-inventory.js b/src/util/is-new-inventory.js index 7733aeb..a2f78b4 100644 --- a/src/util/is-new-inventory.js +++ b/src/util/is-new-inventory.js @@ -1,14 +1,9 @@ module.exports.isNewInventory = async (currentItems, lastSoldItems) => { - console.log({ currentItems }) - console.log({ lastSoldItems }) let newItemsFound = false const lastSoldItemNames = lastSoldItems.map((value) => value.name) - console.log({ lastSoldItemNames }) - for (const currentItem of currentItems) { if (!lastSoldItemNames.includes(currentItem.name)) { - console.log(`New item found: ${currentItem.name}`) newItemsFound = true } }