Skip to content
This repository has been archived by the owner on May 12, 2024. It is now read-only.

Commit

Permalink
some refactoring, try to be more consistent
Browse files Browse the repository at this point in the history
  • Loading branch information
aloop committed Jan 6, 2024
1 parent aa097a5 commit 1bbee32
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 43 deletions.
65 changes: 35 additions & 30 deletions api-client/epic-games-store.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const productBaseUrl = "https://www.epicgames.com/store/en-US/product/";
const freeGamesApiUrl =
"https://store-site-backend-static.ak.epicgames.com/freeGamesPromotions?locale=en-US&country=US&allowCountries=US";

const isCurrentlyFree = (game) => {
function isCurrentlyFree(game) {
const currentDate = Date.now();
const giveawayStart =
game?.promotions?.promotionalOffers[0]?.promotionalOffers[0]?.startDate;
Expand All @@ -17,9 +17,9 @@ const isCurrentlyFree = (game) => {
}

return false;
};
}

const getFormattedEndDate = (game) => {
function getFormattedEndDate(game) {
const giveawayEnd =
game?.promotions?.promotionalOffers[0]?.promotionalOffers[0]?.endDate;

Expand All @@ -32,7 +32,35 @@ const getFormattedEndDate = (game) => {
}

return null;
};
}

function selectFreeGames(game) {
return (
// Check for a discounted price of $0
game?.price?.totalPrice?.discountPrice === 0 &&
// Make sure it has "promotional offers"
game?.promotions?.promotionalOffers?.length > 0 &&
// Make sure it's actually free
isCurrentlyFree(game)
);
}

function formatGameData(game) {
return {
title: game.title,
description: game.description,
id: game.id,
url: new URL(`${game?.productSlug}`, productBaseUrl).href,
thumbnailUrl:
game?.keyImages?.find(
(img) => img?.type?.toLowerCase() === "thumbnail"
)?.url ?? game?.keyImages[0]?.url,
freeUntil: getFormattedEndDate(game),
endDate:
game?.promotions?.promotionalOffers[0]?.promotionalOffers[0]
?.endDate,
};
}

export async function fetchFreeGames() {
const response = await fetch(freeGamesApiUrl);
Expand All @@ -45,32 +73,9 @@ export async function fetchFreeGames() {

const result = await response.json();

if (result?.data?.Catalog?.searchStore?.elements) {
const elements = result.data.Catalog.searchStore.elements;
const elements = result?.data?.Catalog?.searchStore?.elements;

return elements
.filter(
(game) =>
// Check for a discounted price of $0
game?.price?.totalPrice?.discountPrice === 0 &&
// Make sure it has "promotional offers"
game?.promotions?.promotionalOffers?.length > 0 &&
// Make sure it's actually free
isCurrentlyFree(game)
)
.map((game) => ({
title: game.title,
description: game.description,
id: game.id,
url: new URL(`${game?.productSlug}`, productBaseUrl).href,
thumbnailUrl:
game?.keyImages?.find(
(img) => img?.type?.toLowerCase() === "thumbnail"
)?.url ?? game?.keyImages[0]?.url,
freeUntil: getFormattedEndDate(game),
endDate:
game?.promotions?.promotionalOffers[0]?.promotionalOffers[0]
?.endDate,
}));
if (elements) {
return elements.filter(selectFreeGames).map(formatGameData);
}
}
4 changes: 2 additions & 2 deletions cron-tasks/fetch-free-epic-games.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const {
channels: { deals },
} = await loadConfig();

const postNewDeals = (client, games) => {
function postNewDeals(client, games) {
if (games.length > 0) {
try {
const channel = client.channels.cache.get(deals);
Expand Down Expand Up @@ -40,7 +40,7 @@ const postNewDeals = (client, games) => {
console.error(err);
}
}
};
}

// 2 hours in milliseconds
const intervalLength = 2 * 60 * 60 * 1000;
Expand Down
6 changes: 1 addition & 5 deletions models/epic-games-store.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { fetchFreeGames } from "../api-client/epic-games-store.js";
const pickGameId = (game) => game.id;

export async function fetchNewGames() {
let freeGames = null;
let freeGames = [];

try {
freeGames = await fetchFreeGames();
Expand All @@ -15,10 +15,6 @@ export async function fetchNewGames() {
);
}

if (freeGames === null) {
return;
}

const currentDate = Date.now();
const store = new Store("../db/epic-games-store-free-games.json");
const currentGames = await store.read();
Expand Down
14 changes: 8 additions & 6 deletions models/wow-token-price.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ const db = await open({
driver: sqlite3.cached.Database,
});

const closeDB = async () => {
async function closeDB() {
await db.close();
};
}

process.on("SIGINT", closeDB);
process.on("SIGTERM", closeDB);
Expand All @@ -32,10 +32,12 @@ const insertPriceQuery = `
(:updated_at,:price);
`;

const formatResult = ({ updated_at, price }) => ({
updatedAt: updated_at,
price,
});
function formatResult({ updated_at, price }) {
return {
updatedAt: updated_at,
price,
};
}

export async function getLatest() {
const result = await db.get(getLatestQuery);
Expand Down

0 comments on commit 1bbee32

Please sign in to comment.