Skip to content

Commit 5098ba1

Browse files
authored
fix: gog cron dom parsing (#105)
1 parent 09a0852 commit 5098ba1

File tree

2 files changed

+37
-16
lines changed

2 files changed

+37
-16
lines changed

src/crons/GoG.ts

Lines changed: 36 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,11 @@ export default new Cron({
4646
const embed = new EmbedBuilder()
4747
.setTitle(`GoG - ${game.title}`)
4848
.setURL('https://www.gog.com/#giveaway')
49-
.setDescription(game.description)
49+
.setDescription(
50+
game.description.length > 1000
51+
? game.description.slice(0, 999) + '…'
52+
: game.description,
53+
)
5054
.setImage(game.banner)
5155
.addFields({
5256
name: 'URL du jeu',
@@ -112,18 +116,20 @@ export async function getOfferedGame(logger: Logger): Promise<Game | null> {
112116
if (!homeBody) return null;
113117
const html = parse(homeBody);
114118
if (!html) return null;
115-
const SEOLink = html.querySelector('a#giveaway');
119+
const giveawayNode = html.querySelector('#giveaway');
120+
if (!giveawayNode) return null;
121+
const SEOLink = giveawayNode.querySelector('a.giveaway__overlay-link');
116122
if (!SEOLink) return null;
123+
const gameURL = SEOLink.getAttribute('href');
124+
if (!gameURL) return null;
125+
117126
const endTimestamp = Number(
118127
html
119128
.querySelector('.giveaway-banner__footer gog-countdown-timer')
120129
?.getAttribute('end-date'),
121130
);
122131

123-
const { body: gameBody } = await got<string>(
124-
`https://www.gog.com${SEOLink.getAttribute('ng-href')}`,
125-
GOG_GOT_OPTIONS,
126-
);
132+
const { body: gameBody } = await got<string>(gameURL, GOG_GOT_OPTIONS);
127133
if (!gameBody) return null;
128134
const gameHTML = parse(gameBody);
129135
if (!gameHTML) return null;
@@ -135,15 +141,16 @@ export async function getOfferedGame(logger: Logger): Promise<Game | null> {
135141
if (!gameJSON) {
136142
// the gift link redirect to incorrect page
137143
const title =
138-
SEOLink.querySelector('.giveaway-banner__title')?.textContent ??
139-
SEOLink.getAttribute('giveaway-banner-id') ??
144+
giveawayNode.querySelector('.giveaway-banner__title')?.textContent ??
145+
giveawayNode.getAttribute('giveaway-banner-id') ??
140146
'';
141147
const description =
142-
SEOLink.querySelector('.giveaway-banner__description')?.textContent ?? '';
148+
giveawayNode.querySelector('.giveaway-banner__description')
149+
?.textContent ?? '';
143150
const srcset =
144-
SEOLink.querySelector(
145-
'.giveaway-banner__image source[type="image/png"]',
146-
)?.getAttribute('srcset') ?? '';
151+
giveawayNode
152+
.querySelector('.giveaway-banner__image source[type="image/png"]')
153+
?.getAttribute('srcset') ?? '';
147154
/*
148155
* srcset="
149156
* //images-1.gog-statics.com/c26a3d08d01005d92fbc4b658ab226fc5de374d3746f313a01c83e615cc066c1_giveaway_banner_logo_502_2x.png 2x,
@@ -172,8 +179,9 @@ export async function getOfferedGame(logger: Logger): Promise<Game | null> {
172179
}
173180

174181
const description =
175-
gameHTML.querySelector('.content-summary-item__description')?.textContent ??
176-
'';
182+
gameHTML
183+
.querySelector('.content-summary-section .description')
184+
?.textContent?.trim() ?? '';
177185
const banner =
178186
gameHTML
179187
.querySelector('head meta[property="og:image"]')
@@ -185,15 +193,27 @@ export async function getOfferedGame(logger: Logger): Promise<Game | null> {
185193
);
186194

187195
const rating = gameJSON.aggregateRating?.ratingValue ?? '?';
196+
let link: string = gameJSON.offers[0].url;
197+
{
198+
const searchIndex = link.indexOf('?');
199+
if (searchIndex !== -1) {
200+
link = link.slice(0, searchIndex);
201+
}
202+
}
203+
204+
const frOffer = gameJSON.offers.find(
205+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
206+
(o: any) => new URL(o.url).searchParams.get('countryCode') === 'FR',
207+
);
188208

189209
return {
190210
title: gameJSON.name.trim(),
191211
description: decode(description),
192212
// link: 'https://www.gog.com/fr#giveaway',
193-
link: gameJSON.offers.url,
213+
link,
194214
thumbnail: gameJSON.image,
195215
banner,
196-
originalPrice: gameJSON.offers.price,
216+
originalPrice: `${frOffer.price} €`,
197217
discountEndDate: new Date(endTimestamp),
198218
rating: `${rating} / 5`,
199219
};

tests/cron/Gog.spec.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ test('getOfferedGames', async () => {
2525
// expect(game.originalPrice).toBeTruthy();
2626
// expect(typeof game.originalPrice).toBe('string');
2727

28+
// TODO: check the date is valid and find the discountEndDate in GoG pages
2829
expect(game.discountEndDate).toBeTruthy();
2930
expect(game.discountEndDate instanceof Date).toBe(true);
3031

0 commit comments

Comments
 (0)