Skip to content
Open
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
7 changes: 7 additions & 0 deletions models/Rating.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
function Rating(score, link, service) {
this.score = score;
this.link = link;
this.service = service;
}

module.exports = Rating;
2 changes: 1 addition & 1 deletion router.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ module.exports = (dir, s) => {//s is the services layer.
router.get('/manual', require(dir + 'manualRoute.js')());
router.get('/cards', require(dir + 'cardsRoute.js')(s.data));
router.get('/stats', require(dir + 'statsRoute.js')(s.data));
router.get('/retry', require(dir + 'retryRoute.js')(s.data, s.scoreUpdate));
router.get('/reset', require(dir + 'resetRoute.js')(s.data, s.scoreUpdate));
//router.post('/updategame', require(dir + 'updateScoreForGame.js')(s.data, s.metacritic));
return router;
};
7 changes: 4 additions & 3 deletions routes/cardsRoute.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

module.exports = (dataService) => {
return (req,res) => {
const ratedGames = dataService.getRatedGames().sort( (a, b) => b.score - a.score);
const unratedGames = dataService.getUnratedGames();
const games = ratedGames.concat(unratedGames);
const ratedGames = dataService.getRatedGames().sort( (a, b) => b.rating_score - a.rating_score);
const withoutScore = dataService.getGamesWithoutScore();
const unratedGames = dataService.getGamesWithoutRating();
const games = ratedGames.concat(withoutScore, unratedGames);
res.render('cards', { gamesList: games });
};
};
7 changes: 4 additions & 3 deletions routes/defaultRoute.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ const fs = require('fs');

module.exports = (dataService, fetchService) => {
return (req,res) => {
const ratedGames = dataService.getRatedGames().sort( (a, b) => b.score - a.score);
const unratedGames = dataService.getUnratedGames();
const games = ratedGames.concat(unratedGames);
const ratedGames = dataService.getRatedGames().sort( (a, b) => b.rating_score - a.rating_score);
const withoutScore = dataService.getGamesWithoutScore();
const unratedGames = dataService.getGamesWithoutRating();
const games = ratedGames.concat(withoutScore, unratedGames);
const lastUpdate = fetchService.getLastFetchDate();
const versionInfo = Settings.buildVersion || Settings.buildVersion || 'local build';
res.render('games', { title: games.length + ' Games on Sale', gamesList: games, lastFetch: lastUpdate, version: versionInfo });
Expand Down
4 changes: 2 additions & 2 deletions routes/retryRoute.js → routes/resetRoute.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@

module.exports = (gameService, scoreUpdateService) => {
return async (req, res) => {
gameService.retry();
scoreUpdateService.checkAndUpdateScores();
gameService.resetRatingForGamesWithoutScore();
await scoreUpdateService.checkAndUpdateScores();
res.sendStatus(202);
};
};
6 changes: 5 additions & 1 deletion servicelayer.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,21 @@
module.exports = (dir) => {
const nintendo = require(dir + '/nintendoShopService')();
const metacritic = require(dir + '/metacriticService')();
const opencritic = require(dir + '/opencriticService')();
const data = require(dir + '/gameService')();
const rating = require(dir + '/ratingService')(opencritic, metacritic, data);
const saleHistory = require(dir + '/saleHistoryService')();
const fetch = require(dir + '/fetchService')(data, nintendo, saleHistory);
const scoreUpdate = require(dir + '/scoreUpdateService')(data, metacritic);
const scoreUpdate = require(dir + '/scoreUpdateService')(data, rating);
const cronjobFetch = require(dir + '/fetchCronjob')(fetch);
const cronjobScore = require(dir + '/scoreCronjob')(scoreUpdate);
return {
data,
saleHistory,
nintendo,
metacritic,
opencritic,
rating,
fetch,
scoreUpdate,
cronjobFetch,
Expand Down
4 changes: 3 additions & 1 deletion services/fetchService.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ function Game(game) {
this.imageUrl = 'https:' + game.image_url_sq_s;
this.priceRegular = game.price_regular_f;
this.nintendoUrl = game.url;
this.rating_available = false;
this.rating_hasScore = false;
}

function Sleep(milliseconds) {
Expand Down Expand Up @@ -42,7 +44,7 @@ module.exports = (dataService, nintendoService, saleService) => {
if (gamesToAdd != undefined && gamesToAdd.length >= 0) {
const priceInfos = await nintendoService.getPriceInfoForGames(gamesToAdd.map(game => game.nsId));
gamesToAddWithSaleDetails = gamesToAdd
.filter(game => priceInfos.get(game.nsId) != undefined) // remove games without price discount
.filter(game => typeof priceInfos.get(game.nsId) !== 'undefined') // remove games without price discount
.map(game => {
const info = priceInfos.get(game.nsId);
const historyEntry = saleService.addSale(game.nsId, new Sale(info.price, info.start, info.end));
Expand Down
64 changes: 38 additions & 26 deletions services/gameService.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,55 +23,67 @@ module.exports = () => {
},

getRatedGames: () => {
return db.games.find().filter(g => (g.score != undefined));
return db.games.find({rating_available: true, rating_hasScore: true});
},

getUnratedGames: () => {
return db.games.find().filter(g => (g.score == undefined));
// rating website link exists, but rating is not available yet
getGamesWithoutScore: () => {
return db.games.find({rating_available: true, rating_hasScore: false});
},

// could not be found at any rating website
getGamesWithoutRating: () => {
return db.games.find({rating_available: false});
},

getStats: () => {
const tbd = db.games.find().filter(g => (g.score === 0)).length;
const notFound = db.games.find().filter(g => (g.score === -1)).length;
return { tbd: tbd, notFound: notFound }
const gameOnSale = db.games.count();
const gamesWithRating = module.exports().getRatedGames().length;
const tbd = module.exports().getGamesWithoutScore().length;
const notFound = module.exports().getGamesWithoutRating().length;
return { notFound: notFound, unrated: tbd, rated: gamesWithRating, total: gameOnSale};
},

saveGame: (game) => {
db.games.save(game);
},

retry: () => {

// FIXME - resets everything. Seems like using a query with more than one parameter might be broken?
resetRatingForGamesWithoutScore: () => {
const options = {
multi: true
}

const queryTba = {
score: 0
};

const queryNotFound = {
score: -1
const queryWithoutScore = {
//rating_score: undefined
//not working as expected
rating_available: true,
rating_hasScore: false
};

const update = {
score: undefined
}

db.games.update(queryTba, update, options);
db.games.update(queryNotFound, update, options);
rating_providers: undefined,
rating_available: false,
rating_hasScore: false,
rating_score: undefined
};
db.games.update(queryWithoutScore, update, options);
},

setMetacritInfo: (id, rating, url) => {
const score = (rating === 'tbd') ? 0 : rating;
getRatingsFromProviders: (id) => {
return db.games.findOne({ _id: id }).rating_providers;
},

setRating: (id, score, ratings) => {
const hasScore = typeof score !== 'undefined';
const hasRating = (typeof ratings !== 'undefined' && ratings.length > 0);
let query = {
_id: id
};
let update = {
score: score,
metacriticUrl: url
}
rating_available: hasRating,
rating_hasScore: hasScore,
rating_providers: ratings,
rating_score: score
};
db.games.update(query, update);
}
}
Expand Down
6 changes: 3 additions & 3 deletions services/metacriticScrapeService.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ var request = require('request')
, cheerio = require('cheerio')
, extend = require('extend');

var url = 'https://www.metacritic.com/'
, urlSearchAll = 'search/{0}/{1}/results'
, urlSearchAllByPlatform = 'search/{0}/{1}/results?search_type=advanced&plats[{2}]=1'
var url = 'https://www.metacritic.com'
, urlSearchAll = '/search/{0}/{1}/results'
, urlSearchAllByPlatform = '/search/{0}/{1}/results?search_type=advanced&plats[{2}]=1'
, urlPage = '?page={0}'
, currentPage = 0;

Expand Down
50 changes: 45 additions & 5 deletions services/metacriticService.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,36 @@
'use strict';
const Settings = require('../settings');
const Rating = require('../models/Rating');
const service = 'metacritic';
const metacriticScrape = require('./metacriticScrapeService');

const { promisify } = require('util');
const levenshtein = require('fast-levenshtein');
const platformIdSwitch = 268409;
const categoryGame = 'game';

const searchSwitchGame = async (title, cb) => {
const asyncScrapeSearch = promisify(metacriticScrape.Search);

async function searchSwitchGame(title) {
const searchTitle = getSearchTitle(title);
console.log(`${title} = ${searchTitle}`);

const options = { text: searchTitle, category: categoryGame, platformId: platformIdSwitch };
return asyncScrapeSearch(options);
}

const getBestMatchTitle = (title, titlesFromMC) => {
var lowestScore = 99;
var bestMatch = titlesFromMC[0];

metacriticScrape.Search(options, cb);
titlesFromMC.forEach(game => {
const score = levenshtein.get(title, game.title);
if (score < lowestScore) {
lowestScore = score;
bestMatch = game;
}
});

console.log(`Found best match for "${title}" with a score of ${lowestScore}: "${bestMatch.title}"`);
return bestMatch;
}

function getSearchTitle(title) {
Expand All @@ -24,8 +43,29 @@ function getSearchTitle(title) {
.trim();
}

async function getRatingFor(title) {
try {
const list = await searchSwitchGame(title);
const bestMatch = getBestMatchTitle(title, list);
console.debug(`${title}: ${bestMatch.metascore}, ${bestMatch.link}`);
let score = (bestMatch.metascore !== 'tbd') ? parseInt(bestMatch.metascore) : undefined;
return new Rating(score, bestMatch.link, service);
}
catch (err) {
console.error(`failed to fetch score for "${title}": ${err}`);
if (err === 'No results') {
return new Rating(undefined, undefined, service);
}
else {
// error but no results... what should we do here?
return new Rating(undefined, undefined, service);
}
}
}

module.exports = () => {
return {
searchSwitchGame
searchSwitchGame: searchSwitchGame,
getRatingFor: getRatingFor
};
}
67 changes: 67 additions & 0 deletions services/opencriticService.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
'use strict';
const axios = require('axios').default;
const Rating = require('../models/Rating');
const Settings = require('../settings');
const service = 'opencritic';

async function searchGame(title) {
try {
const titleEncoded = encodeURIComponent(title);
const response = await axios({
method: 'get',
url: Settings.opencriticBase + '/meta/search',
params: {
criteria: titleEncoded
},
});

let first = response.data[0];
// check dist and discard if bigger than 0.59
if (first.dist <= 0.59) {
return {error: false, id: first.id, name: first.name};
}
console.log('[info] opencritic - ' + title + ' - not found - distance too high');
return {error: false, id: undefined, name: undefined};
}
catch (e) {
console.log('[error] opencritic - ' + title + ' - search request failed - criteria: ' + titleEncoded);
throw Error('opencritic - ' + title + ' - search request failed - criteria: ' + titleEncoded);
}
}

async function getScore(id) {
try {
const response = await axios({
method: 'get',
url: Settings.opencriticBase + '/game/' + id,
});
let score = (response.data.medianScore > -1) ? response.data.medianScore : undefined;
return {error: false, score: score};
}
catch (e) {
console.log('[error] opencritic - score request failed - id: ' + id);
throw Error('opencritic - score request failed - id: ' + id);
}
}

async function getRatingFor(title) {
let link = undefined;
try {
const searchResult = await searchGame(title);
if (typeof searchResult.id === 'undefined') {
return new Rating(undefined, undefined, service);
}
link = 'https://opencritic.com/game/' + searchResult.id + '/' + searchResult.name.replace(/ /g, "-");
let rating = await getScore(searchResult.id);
return new Rating(rating.score, link, service);
}
catch (e) {
return new Rating(undefined, link, service);
}
}

module.exports = () => {
return {
getRatingFor: getRatingFor
}
};
Loading