Actor

sergeylukin/steam-puppeteer

  • Builds
  • latest 0.0.54 / 2018-09-18
  • Created 2018-09-05
  • Last modified 2018-09-18
  • grade 1

Description

Fetch all purchased games from Steam profile page


API

To run the actor, send a HTTP POST request to:

https://api.apify.com/v2/acts/sergeylukin~steam-puppeteer/runs?token=<YOUR_API_TOKEN>

The POST payload will be passed as input for the actor. For more information, read the docs.


Example input

Content type: application/json; charset=utf-8

{ "url": "https://steamcommunity.com/id/pogran_/games/?tab=all" }

Source code

Based on the apify/actor-node-chrome Docker image (see docs).

const Apify = require('apify');

Apify.main(async () => {
    const input = await Apify.getValue('INPUT');
    
    if (!input || !input.url) throw new Error('Invalid input, must be a JSON object with the "url" field!');
    
    console.log('Launching Puppeteer...');
    const browser = await Apify.launchPuppeteer();
    
    console.log(`Opening URL: ${input.url}`);  
    let page = await browser.newPage();
    await page.goto(input.url);
    
    function imagesHaveLoaded() {
        return Array.from(document.images).every((i) => i.complete);
    }
    
    await page.waitForFunction(imagesHaveLoaded, { timeout: 5000 });
    
    const gamesList = await page.evaluate(() => {
        
        let games = {},
            id;
            
        const game_elements = document.querySelectorAll("#games_list_row_container > #games_list_rows > .gameListRow")
        for (const game_element of game_elements) {
            //if (counter > 2) break
            
            id = game_element.id.match(/\d+/)[0]
            
            games[id] = {
                id,
                name: game_element.querySelector('.gameListRowItem .gameListRowItemName.ellipsis').innerHTML,
                play_hours: parseFloat(game_element.querySelector('.gameListRowItem .hours_played').innerHTML),
                page_url: game_element.querySelector('.gameListRowLogo a').getAttribute('href'),
                logo: game_element.querySelector('.gameListRowLogo img').getAttribute('src'),
            }
            
            //item.name = game_element.querySelector('.gameListRowItem .gameListRowItemName.ellipsis').innerHTML
            
            //game_link_element = game_element.querySelector('.gameListRowLogo a');
            //await game_element.click();
            //await new Promise((resolve) => setTimeout(resolve, 2000));
            //item.score = document.querySelector('.user_reviews_summary_row').getAttribute("data-tooltip-text");
            //item.release = document.querySelector('.release_date .date').innerHTML
            //console.log('Release: ' + item.release)
        }
        
        /*jQuery("div[id='games_list_row_container'] > div[id='games_list_rows'] > div").each(async function(index) {
            if (index > 2) return;
            
            var name = jQuery(this).find('.gameListRowItem .gameListRowItemName.ellipsis').text(),
                play_hours = parseFloat(jQuery(this).find('.gameListRowItem .hours_played').text()),
                page_url = jQuery(this).find('.gameListRowLogo a').attr('href'),
                logo = jQuery(this).find('.gameListRowLogo a img').attr('src'),
                item = {
                    name,
                    play_hours,
                    page_url,
                    logo,
                };
                
            const link_element = this.querySelector('.gameListRowLogo a');
            link_element.click();
            await new Promise((resolve) => setTimeout(resolve, 2000));
            item.score = document.querySelector('.user_reviews_summary_row').getAttribute("data-tooltip-text");
            
            games.push(item);
        });
        */
        
        return games
    });
    
    /*for (let i = 0; i < parsedData.length; i ++) {
        let game = parsedData[i]
        if (i > 2) return false;
        console.log("Entering " + game.name + "...")
        await page.goto(game.page_url, { waitUntil: "load" });
        let additionalDetails = await page.evaluate(() => {
           return {
               score: jQuery('.user_reviews_summary_row').data('tooltip-text'),
               release: jQuery('.release_date .date').text(),
               platform_mac: jQuery('.game_area_purchase_platform .mac').length,
               platform_win: jQuery('.game_area_purchase_platform .win').length,
           }
        });
        
        console.log(additionalDetails)
        
        parsedData[i].score = additionalDetails.score
        parsedData[i].release = additionalDetails.release
        parsedData[i].platform_mac = additionalDetails.platform_mac
        parsedData[i].platform_win = additionalDetails.platform_win
        
    }*/
    
    //console.log(gamesList)
    
    for (let game_id in gamesList) {
        if (!gamesList.hasOwnProperty(game_id)) continue;
        let game = gamesList[game_id]
        
        //console.log('id: ' + game.id + ', name: ' + game.name + ', play_hours: ' + game.play_hours + ', page_url: ' + game.page_url + ', logo: ' + game.logo)
    }
    
    
    await Apify.setValue('OUTPUT', gamesList);
    
    console.log('Closing Puppeteer...');
    await browser.close();
    
    console.log('Done.');
});