Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move control of individual app scraping to UI #12

Merged
merged 5 commits into from
Nov 2, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ function main() {
res.json(gamePageData);
});

_app.post('/api/headset-scrape', async (req, res) => {
let searchUrl = req.body.url;
let searchPageData = await _steamScraper.getHeadsetsFromAppPage(searchUrl);
res.json(searchPageData);
});

_app.post('/api/search-scrape', async (req, res) => {
let searchUrl = req.body.url;
let searchPageData = await _steamScraper.getSearchPageData(searchUrl);
Expand Down
22 changes: 11 additions & 11 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"request-promise": "4.2.4"
},
"devDependencies": {
"nodemon": "^1.19.2"
"nodemon": "^1.19.4"
},
"homepage": "https://github.com/KevinNovak/vrgamedeals-Tools#readme",
"repository": {
Expand Down
37 changes: 25 additions & 12 deletions public/scripts/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const PRICE_NUMBER_REGEX = /\$(\d+\.\d{2})/;
const PERCENT_NUMBER_REGEX = /(\d+)%/;

const NEW_LINE = '&#10';
const MAX_PAGES = 20;
const MAX_PAGES = 100;

const headsetAliases = {
'Valve Index': {
Expand Down Expand Up @@ -105,21 +105,15 @@ async function retrieveSteamSearchTable() {
let searchData = [];
if (searchAllPages) {
for (let i = 1; i <= MAX_PAGES; i++) {
let content = {
url: `${steamSearchUrl}&page=${i}`
};
searchResultsDiv.innerHTML = `Retrieving page ${i}...`;
let searchPageData = await post('./api/search-scrape', content);
if (searchPageData.length < 1) {
break;
let searchPageData = await retrieveSearchPageData(searchResultsDiv, steamSearchUrl, i);
if (searchPageData.length < 1) {
break;
}
searchData.push(...searchPageData);
}
} else {
let content = {
url: steamSearchUrl
};
searchData = await post('./api/search-scrape', content);
let searchPageData = await retrieveSearchPageData(searchResultsDiv, steamSearchUrl);
searchData.push(...searchPageData);
}

if (!searchData || searchData.length < 1) {
Expand All @@ -145,6 +139,25 @@ async function retrieveSteamSearchTable() {
retrieveSearchButton.disabled = false;
}

async function retrieveSearchPageData(searchResultsDiv, steamSearchUrl, pageNumber = 1) {
let content = {
url: `${steamSearchUrl}&page=${pageNumber}`
};
let searchPageData = await post('./api/search-scrape', content);
for (let [index, app] of searchPageData.entries()) {
let itemNumber = index + 1;
searchResultsDiv.innerHTML = `Retrieving page ${pageNumber}, result ${itemNumber} of ${searchPageData.length}...`;
app.headsets = [];
if (app.type == "APP") {
let content = {
url: app.link
};
app.headsets = await post('./api/headset-scrape', content);
}
}
return searchPageData;
}

function createMarkdownTable(searchData) {
let header = '| Platform | Title | Price (USD) | Discount (%) | Rating (%) | Review Count |';
let divider = '| :- | :- | -: | -: | -: | -: |';
Expand Down
7 changes: 2 additions & 5 deletions services/steam-scraper.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ function getHeadsets($) {
return headsets;
}

async function getHeadsetsFromLink(link) {
async function getHeadsetsFromAppPage(link) {
let pageHtml = await _rp({ url: link });
let $ = _cheerio.load(pageHtml);
return getHeadsets($);
Expand All @@ -98,7 +98,6 @@ async function getGameDataFromSearchResult(searchResult) {
let percentOff = "";
let reviewsPercent = "";
let reviewsCount = "";
let headsets = [];

title = $('div.search_name > span.title').text().trim();
link = stripQueryString(searchResult.attribs.href);
Expand All @@ -125,8 +124,6 @@ async function getGameDataFromSearchResult(searchResult) {
reviewsPercent = extractPercent(reviewsSummary);
reviewsCount = extractReviewsCount(reviewsSummary).replace(/,/g, '');
}

headsets = await getHeadsetsFromLink(link);
}

return {
Expand All @@ -139,7 +136,6 @@ async function getGameDataFromSearchResult(searchResult) {
percentOff,
reviewsPercent,
reviewsCount,
headsets
};
}

Expand Down Expand Up @@ -179,5 +175,6 @@ function getGameDataFromGameElement(gameElement) {

module.exports = {
getAppPageData,
getHeadsetsFromAppPage,
getSearchPageData
};