Skip to content

Commit

Permalink
updated IGDB calls with new API, realized I already created lists I w…
Browse files Browse the repository at this point in the history
…anted, realized repo needs serious updating
  • Loading branch information
jgdigitaljedi committed Aug 1, 2021
1 parent 2c45223 commit 973aec0
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 29 deletions.
45 changes: 45 additions & 0 deletions package-lock.json

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

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
"cors": "^2.8.5",
"express": "^4.17.1",
"glob": "^7.1.6",
"igdb-api-node": "^5.0.1",
"lodash": "^4.17.15",
"moment": "^2.25.1",
"request": "^2.88.2",
Expand Down
77 changes: 48 additions & 29 deletions server/igdb.controller.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
const express = require('express');
const router = express.Router();
const apicalypse = require('apicalypse').default;
const moment = require('moment');
const _cloneDeep = require('lodash/cloneDeep');
const axios = require('axios');
const igdb = require('igdb-api-node').default;

let client;
let appKey;
let appKeyTimestamp;
const twitchClientId = process.env.TWITCH_CLIENT_ID;
const twitchSecretToken = process.env.TWITCH_SECRET_TOKEN;

/* IGDB Search */

Expand All @@ -16,27 +23,24 @@ const esrbData = {
'12': 'AO'
};

const requestOptions = {
method: 'POST',
baseURL: 'https://api-v3.igdb.com',
headers: {
Accept: 'application/json',
'user-key': process.env.IGDBV3KEY
}
};

const platformOptions = {
method: 'POST',
baseURL: 'https://api-v3.igdb.com',
headers: {
Accept: 'application/json',
'user-key': process.env.IGDBV3KEY
}
};

const fullGameQueryString =
'age_ratings.rating,total_rating,total_rating_count,first_release_date,genres.name,name,game_modes';

async function getAppAccessToken() {
return axios.post(
`https://id.twitch.tv/oauth2/token?client_id=${twitchClientId}&client_secret=${twitchSecretToken}&grant_type=client_credentials`
);
}

async function refreshAppKey() {
const appKeyRes = await getAppAccessToken();
appKey = appKeyRes.data;
appKeyTimestamp = moment().add(appKey.expires_in - 60, 'seconds');
console.log('clientId', twitchClientId);
console.log('token', appKey.access_token);
return igdb(twitchClientId, appKey.access_token);
}

function parseFullDataResult(data) {
return data.map(item => {
if (item.first_release_date) {
Expand All @@ -61,8 +65,11 @@ function parseFullDataResult(data) {
});
}

function apiFullSearch(name, platform) {
const request = apicalypse(requestOptions)
async function apiFullSearch(name, platform) {
if (!client || !appKey || moment().isAfter(appKeyTimestamp)) {
client = await refreshAppKey();
}
const request = client
.fields(fullGameQueryString)
.search(name)
.where(`platforms = [${platform}]`)
Expand All @@ -80,16 +87,22 @@ function apiFullSearch(name, platform) {
});
}

function apiSearch(name, platform) {
return apicalypse(requestOptions)
async function apiSearch(name, platform) {
if (!client || !appKey || moment().isAfter(appKeyTimestamp)) {
client = await refreshAppKey();
}
return client
.fields(`name,id`)
.search(name)
.where(`platforms = [${platform}]`)
.request('/games');
}

function fuzzyFullApiSearch(name) {
const request = apicalypse(requestOptions)
async function fuzzyFullApiSearch(name) {
if (!client || !appKey || moment().isAfter(appKeyTimestamp)) {
client = await refreshAppKey();
}
const request = client
.fields(fullGameQueryString)
.search(name)
.request('/games');
Expand All @@ -106,15 +119,21 @@ function fuzzyFullApiSearch(name) {
});
}

function fuzzyApiSearch(name) {
return apicalypse(requestOptions)
async function fuzzyApiSearch(name) {
if (!client || !appKey || moment().isAfter(appKeyTimestamp)) {
client = await refreshAppKey();
}
return client
.fields(`name,id`)
.search(name)
.request('/games');
}

function platformSearch(platform) {
return apicalypse(platformOptions)
async function platformSearch(platform) {
if (!client || !appKey || moment().isAfter(appKeyTimestamp)) {
client = await refreshAppKey();
}
return client
.fields(`name,id`)
.search(platform)
.request('/platforms');
Expand Down

0 comments on commit 973aec0

Please sign in to comment.