Skip to content

Commit

Permalink
fix: try to avoid multiple notifications from GoG (#93)
Browse files Browse the repository at this point in the history
* schedule hourly instead all half hours
* normalize and trim game identity
* compare with last cron AND last game founded
  as a counter measure of not able to parse page but free game did not change
* ensure got followRedirect and throw HTTP errors

Co-authored-by: Michaël Zasso <targos@protonmail.com>
  • Loading branch information
Purexo and targos authored Feb 16, 2024
1 parent d3dc302 commit 20bced4
Showing 1 changed file with 14 additions and 6 deletions.
20 changes: 14 additions & 6 deletions src/crons/GoG.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,27 @@ export default new Cron({
enabled: true,
name: 'GoG',
description:
'Vérifie toutes les demi heures si GoG offre un jeu (promotion gratuite) et alerte dans #jeux',
schedule: '5,35 * * * *',
'Vérifie toutes les heures si GoG offre un jeu (promotion gratuite) et alerte dans #jeux',
schedule: '5 * * * *',
// schedule: '* * * * *', // switch for testing
async handle(context) {
const game = await getOfferedGame(context.logger);

// vérifie le jeu trouvé avec la dernière entrée
const lastGame = await KeyValue.get<string>('Last-Cron-GOG');
const gameStoreIdentity = game?.title ?? null;
if (lastGame === gameStoreIdentity) return; // skip si identique

const lastCron = await KeyValue.get<string>('Last-Cron-GOG');
if (gameStoreIdentity === lastCron) return; // skip si identique

await KeyValue.set('Last-Cron-GOG', gameStoreIdentity); // met à jour sinon

const lastGame = await KeyValue.get<string>('Last-Cron-GOG-Found');
if (gameStoreIdentity === lastGame) return; // skip si identique

if (!game) return; // skip si pas de jeu

await KeyValue.set('Last-Cron-GOG-Found', gameStoreIdentity); // met à jour sinon

const channel = findTextChannelByName(context.client.channels, 'jeux');

const embed = new EmbedBuilder()
Expand Down Expand Up @@ -86,7 +92,7 @@ interface Game {

/**
* Fetches offered games from the Epic Games GraphQL API. If there are any, and they
* were offered between the previous and current cron execution, returns them.
* were offered between the previous and current cron execution, return them.
* @param logger
*/
export async function getOfferedGame(logger: Logger): Promise<Game | null> {
Expand All @@ -95,6 +101,8 @@ export async function getOfferedGame(logger: Logger): Promise<Game | null> {
'Accept-Language':
'fr,fr-FR;q=0.8,fr-FR;q=0.7,en-US;q=0.5,en-US;q=0.3,en;q=0.2',
},
followRedirect: true,
throwHttpErrors: true,
};
const { body: homeBody } = await got<string>(
'https://www.gog.com/fr#giveaway', // ensure get fr info or it's geolocalized
Expand Down Expand Up @@ -155,7 +163,7 @@ export async function getOfferedGame(logger: Logger): Promise<Game | null> {
);

return {
title: title.trim(),
title: title.normalize().trim(),
description: decode(description.trim()),
link: 'https://www.gog.com/#giveaway',
banner: `https:${banner}`,
Expand Down

0 comments on commit 20bced4

Please sign in to comment.