Skip to content

Commit

Permalink
Get title from sidebar
Browse files Browse the repository at this point in the history
  • Loading branch information
KevinNovak committed May 19, 2020
1 parent 52bb0f0 commit 7142e0f
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 55 deletions.
92 changes: 38 additions & 54 deletions services/steam-scraper.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ const _regexUtils = require("../utils/regex-utils");
const _stringUtils = require("../utils/string-utils");
const { CustomException } = require("../models/exceptions");

const TITLE_REMOVE = ["Buy", "Play", "Download", "Install", "Pre-Purchase"];

function getAppPageData(appPageHtml) {
let firstGame = getMainGameElement(appPageHtml);
if (!firstGame) {
Expand All @@ -14,18 +12,20 @@ function getAppPageData(appPageHtml) {
);
}

let title = getTitle(appPageHtml);
let gameData = getGameDataFromGameElement(firstGame);
let countdown = getCountdownFromGameElement(firstGame);
let vrSupport = getVrSupportFromGameElement(firstGame);
let headsets = getHeadsets(appPageHtml);
let reviews = getReviews(appPageHtml);

return {
title,
...gameData,
...reviews,
countdown,
vrSupport,
headsets
headsets,
};
}

Expand Down Expand Up @@ -62,7 +62,7 @@ function getSearchAppPageData(appPageHtml) {
return {
countdown,
vrSupport,
headsets
headsets,
};
}

Expand All @@ -81,6 +81,25 @@ function getMainGameElement(appPageHtml) {
return gameElements[0];
}

function getTitle(appPageHtml) {
let $ = _cheerio.load(appPageHtml);

let title = "";

let gameDetailsElement = $(
'.game_details .details_block:contains("Title:")'
)
.first()
?.html()
.trim();

if (gameDetailsElement) {
title = _regexUtils.extractTitle(gameDetailsElement);
}

return title;
}

function getHeadsets(appPageHtml) {
let $ = _cheerio.load(appPageHtml);

Expand All @@ -94,9 +113,7 @@ function getHeadsets(appPageHtml) {
let headsets = [];

for (let headsetElement of headsetElements) {
let headsetName = $(".name", headsetElement)
.text()
.trim();
let headsetName = $(".name", headsetElement).text().trim();
if (headsetName) {
headsets.push(headsetName);
}
Expand All @@ -110,7 +127,7 @@ function getReviews(appPageHtml) {

let reviewData = {
reviewsPercent: "",
reviewsCount: ""
reviewsCount: "",
};

let reviewsTooltip = $(
Expand Down Expand Up @@ -138,12 +155,10 @@ function getGameDataFromSearchResult(searchResult) {
originalPrice: "",
percentOff: "",
reviewsPercent: "",
reviewsCount: ""
reviewsCount: "",
};

let title = $("div.search_name > span.title")
.text()
.trim();
let title = $("div.search_name > span.title").text().trim();
if (title) {
gameData.title = title;
}
Expand All @@ -170,17 +185,13 @@ function getGameDataFromSearchResult(searchResult) {
gameData.price = price;
}

let originalPrice = $("div.search_price > span > strike")
.text()
.trim();
let originalPrice = $("div.search_price > span > strike").text().trim();
if (originalPrice) {
gameData.originalPrice = originalPrice;
}

let percentOff = _regexUtils.extractPercent(
$("div.search_discount > span")
.text()
.trim()
$("div.search_discount > span").text().trim()
);
if (percentOff) {
gameData.percentOff = percentOff;
Expand Down Expand Up @@ -208,7 +219,7 @@ function getGameDataFromSearchResult(searchResult) {
function extractReviewDataFromTooltip(reviewsTooltip) {
let reviewData = {
reviewsPercent: "",
reviewsCount: ""
reviewsCount: "",
};

let reviewsPercent = _regexUtils.extractPercent(reviewsTooltip);
Expand All @@ -227,51 +238,26 @@ function getGameDataFromGameElement(gameElement) {
let $ = _cheerio.load(gameElement);

let gameData = {
title: "",
price: "",
originalPrice: "",
percentOff: ""
percentOff: "",
};

let title = $(".game_area_purchase_game > h1")
.children()
.remove()
.end()
.text()
.trim();
for (let removeKeyword of TITLE_REMOVE) {
if (title.startsWith(removeKeyword)) {
title = title.substr(removeKeyword.length).trim();
}
}

if (title) {
gameData.title = title;
}

let originalPrice = $(".discount_original_price")
.text()
.trim();
let originalPrice = $(".discount_original_price").text().trim();
if (originalPrice) {
gameData.originalPrice = originalPrice;
}

let percentOff = _regexUtils.extractPercent(
$(".discount_pct")
.text()
.trim()
$(".discount_pct").text().trim()
);
if (percentOff) {
gameData.percentOff = percentOff;
}

let price = gameData.originalPrice
? $(".discount_final_price")
.text()
.trim()
: $(".game_purchase_price")
.text()
.trim();
? $(".discount_final_price").text().trim()
: $(".game_purchase_price").text().trim();
if (price) {
gameData.price = price;
}
Expand All @@ -284,13 +270,11 @@ function getCountdownFromGameElement(gameElement) {

let countdownData = {
text: "",
time: 0
time: 0,
};

try {
let text = $(".game_purchase_discount_countdown")
.text()
.trim();
let text = $(".game_purchase_discount_countdown").text().trim();
if (text) {
countdownData.text = text;
}
Expand Down Expand Up @@ -328,5 +312,5 @@ function getVrSupportFromGameElement(gameElement) {
module.exports = {
getAppPageData,
getSearchAppPageData,
getSearchPageData
getSearchPageData,
};
11 changes: 10 additions & 1 deletion utils/regex-utils.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
const TITLE_REGEX = /<b>Title:<\/b>(.*)<br>/;
const PERCENT_REGEX = /(\d+%)/;
const REVIEWS_COUNT_REGEX = /([\d,]+) user review/;
const DISCOUNT_COUNTDOWN_REGEX = /DiscountCountdown,[ ]*([\d]{7,})/;

function extractTitle(input) {
let match = TITLE_REGEX.exec(input);
if (match) {
return match[1].trim();
}
}

function extractPercent(input) {
let match = PERCENT_REGEX.exec(input);
if (match) {
Expand All @@ -24,7 +32,8 @@ function extractDiscountCountdown(input) {
}

module.exports = {
extractTitle,
extractPercent,
extractReviewsCount,
extractDiscountCountdown
extractDiscountCountdown,
};

0 comments on commit 7142e0f

Please sign in to comment.