|
| 1 | +import IGDB_Helper from "./src/igdb/igdb_api.js"; |
| 2 | +import TwitchAPI from "./src/twitch_oauth/twitch_oauth.js"; |
| 3 | +import fs from 'fs'; |
| 4 | + |
| 5 | +function getCredentials() { |
| 6 | + console.log("Retrieving client ID and client secret"); |
| 7 | + const secrets = fs.readFileSync("./secrets.json"); |
| 8 | + const secretsJSON = JSON.parse(secrets); |
| 9 | + return secretsJSON; |
| 10 | +} |
| 11 | + |
| 12 | +async function getAPIToken(clientId, clientSecret) { |
| 13 | + console.log("Requesting API token from Twitch"); |
| 14 | + const twichApi = new TwitchAPI(); |
| 15 | + const apiTokenResult = await twichApi.requestOauthToken(clientId, clientSecret); |
| 16 | + return apiTokenResult.access_token; |
| 17 | +} |
| 18 | + |
| 19 | +async function getGameData(clientId, apiToken) { |
| 20 | + console.log("Pulling game data from IGDB"); |
| 21 | + const igdb = new IGDB_Helper(); |
| 22 | + const igdbRequest = igdb.createRequest(clientId, apiToken); |
| 23 | + |
| 24 | + igdbRequest |
| 25 | + .fields("name, release_dates") |
| 26 | + .limit(1); |
| 27 | + |
| 28 | + const responseData = await igdbRequest.request("/games") |
| 29 | + .then(async (result) => { |
| 30 | + if (result.status !== 200) { |
| 31 | + console.log("Unexpected response from IGDB! Received: " + await result.text()); |
| 32 | + throw new Error("Received bad result from IGDB API!"); |
| 33 | + } |
| 34 | + |
| 35 | + return result.data; |
| 36 | + }) |
| 37 | + .then((data) => { |
| 38 | + console.log("Successfully retrieved data from IGDB API."); |
| 39 | + return data; |
| 40 | + }); |
| 41 | + |
| 42 | + return responseData; |
| 43 | +} |
| 44 | + |
| 45 | +const credentials = getCredentials(); |
| 46 | +const token = await getAPIToken(credentials.clientId, credentials.clientSecret); |
| 47 | +const gameData = await getGameData(credentials.clientId, token); |
| 48 | + |
| 49 | +const firstGame = gameData[0]; |
| 50 | +const firstGameName = firstGame.name; |
| 51 | +let firstGameRelease = new Date(firstGame.release_dates[0]); |
| 52 | +firstGameRelease = firstGameRelease.toLocaleDateString(); |
| 53 | +console.log(`Retrieved game ${firstGameName} released at ${firstGameRelease}`); |
0 commit comments