1const Apify = require('apify');
2
3Apify.main(async () => {
4 const input = await Apify.getValue('INPUT');
5
6 if (!input || !input.url) throw new Error('Invalid input, must be a JSON object with the "url" field!');
7
8 console.log('Launching Puppeteer...');
9 const browser = await Apify.launchPuppeteer();
10
11 console.log(`Opening URL: ${input.url}`);
12 let page = await browser.newPage();
13 await page.goto(input.url);
14
15 function imagesHaveLoaded() {
16 return Array.from(document.images).every((i) => i.complete);
17 }
18
19 await page.waitForFunction(imagesHaveLoaded, { timeout: 5000 });
20
21 const gamesList = await page.evaluate(() => {
22
23 let games = {},
24 id;
25
26 const game_elements = document.querySelectorAll("#games_list_row_container > #games_list_rows > .gameListRow")
27 for (const game_element of game_elements) {
28
29
30 id = game_element.id.match(/\d+/)[0]
31
32 games[id] = {
33 id,
34 name: game_element.querySelector('.gameListRowItem .gameListRowItemName.ellipsis').innerHTML,
35 play_hours: parseFloat(game_element.querySelector('.gameListRowItem .hours_played').innerHTML),
36 page_url: game_element.querySelector('.gameListRowLogo a').getAttribute('href'),
37 logo: game_element.querySelector('.gameListRowLogo img').getAttribute('src'),
38 }
39
40
41
42
43
44
45
46
47
48 }
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73 return games
74 });
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101 for (let game_id in gamesList) {
102 if (!gamesList.hasOwnProperty(game_id)) continue;
103 let game = gamesList[game_id]
104
105
106 }
107
108
109 await Apify.setValue('OUTPUT', gamesList);
110
111 console.log('Closing Puppeteer...');
112 await browser.close();
113
114 console.log('Done.');
115});