Skip to content

Instantly share code, notes, and snippets.

@guilouro
Created April 28, 2020 22:09
Show Gist options
  • Save guilouro/96cbaa536ca2f1e33376b1b00b7b992d to your computer and use it in GitHub Desktop.
Save guilouro/96cbaa536ca2f1e33376b1b00b7b992d to your computer and use it in GitHub Desktop.
const puppeteer = require('puppeteer');
const fs = require("fs");
const HEADLESS = true
const getGameData = async (url) => {
const browser = await puppeteer.launch({ headless: HEADLESS });
const page = await browser.newPage();
await page.goto(url, { waitUntil:"networkidle2" });
const item = await page.evaluate(() => {
return {
id: '',
title: '',
price: '',
image: '',
releaseDate: '',
publisher: '',
developer: '',
platform: '',
description: ''
}
})
browser.close();
return item;
};
const getUrlsByTag = async (tagsId) => {
const linksPromises=[];
const links = [];
await puppeteer.launch({ headless: HEADLESS }).then(async browser => {
tagsId.forEach(id => {
linksPromises.push(browser.newPage().then(async page => {
await page.setViewport({ width: 1366, height: 768 })
await page.goto(`https://store.steampowered.com/search/?&tags=${id}`, { waitUntil:"networkidle2" });
links.push(...await page.evaluate(() => {
return Array.from(document.querySelectorAll('#search_resultsRows > a'))
.filter(item => item.querySelector("[data-price-final]").dataset.priceFinal > 0)
.map(item => item.getAttribute('href'));
}));
await page.close()
}));
});
await Promise.all(linksPromises)
await browser.close();
});
return links;
};
const saveData = (links, data) => {
fs.writeFile("links.json", JSON.stringify(links), "utf8", console.log);
fs.writeFile("data.json", JSON.stringify(data), "utf8", console.log);
}
(async () => {
const tagsId = ['492','9','597','599','122','701','493'];
// const tagsId = ['492'];
const links = await getUrlsByTag(tagsId)
await Promise.all(tagsId.map(async tag => {
const arr = await getUrlsByTag(tag)
links.push(...arr)
}))
console.log({ links, len: links.length })
let data = []
await Promise.all(links.forEach(async link => {
const item = await getGameData(link);
data.push(item)
}))
// console.log({ data })
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment