From 7f02a9a0967b75ce64fa4d329d663644f36594e5 Mon Sep 17 00:00:00 2001 From: "remi.mach" Date: Sat, 11 Dec 2021 13:26:38 +0100 Subject: [PATCH] Adding a screenshot API route IGDB proxy (dirty cache) --- README.md | 6 ++++ imports/api/Handlers/server/igdb-methods.js | 2 +- imports/api/Handlers/server/publications.js | 40 ++++++++++++++++++++- 3 files changed, 46 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 4bb9b64..ef86a0f 100644 --- a/README.md +++ b/README.md @@ -67,3 +67,9 @@ Download a package from it's ID: url: /cdn/storage/packages/:package_id/original/handler-{handler_id}-v{version_of_handler}.nc?download=true httpMethod: "get" ``` + +Get IGDB screenshots for a handler: +``` +url: "api/v1/screenshots/:handler_id", +httpMethod: "get" +``` diff --git a/imports/api/Handlers/server/igdb-methods.js b/imports/api/Handlers/server/igdb-methods.js index 7025832..2e1c3f6 100644 --- a/imports/api/Handlers/server/igdb-methods.js +++ b/imports/api/Handlers/server/igdb-methods.js @@ -7,7 +7,7 @@ import axios from 'axios'; import handleMethodException from '../../../modules/handle-method-exception'; import rateLimit from '../../../modules/rate-limit'; -let bearerToken = ''; +export let bearerToken = ''; const refreshBearer = () => { HTTP.call( diff --git a/imports/api/Handlers/server/publications.js b/imports/api/Handlers/server/publications.js index 24c781f..c2f635a 100644 --- a/imports/api/Handlers/server/publications.js +++ b/imports/api/Handlers/server/publications.js @@ -4,6 +4,8 @@ import Handlers from '../Handlers'; import Comments from '../../Comments/Comments'; import escapeRegExp from '../../../modules/regexescaper'; import Packages from '../../Packages/server/ServerPackages'; +import axios from "axios"; +import { bearerToken } from "./igdb-methods"; Meteor.publish( 'handlers', @@ -73,6 +75,42 @@ Meteor.publish( }, ); + +/* This code is a PoC, its dirty */ +const screenshotsCache = {}; + +WebApp.connectHandlers.use('/api/v1/screenshots', async (req, res, next) => { + res.writeHead(200); + const handlerId = req.url.split("/")[1]; + if(handlerId.length > 0){ + const handler = await Handlers.findOne({ _id: handlerId }, {fields: {gameId: 1}}); + if(!handler?.gameId) { + res.end(JSON.stringify({error: 'Incorrect handlerId'})); + return; + } + + if(!screenshotsCache[handler.gameId]){ + const igdbApi = axios.create({ + baseURL: 'https://api.igdb.com/v4/', + timeout: 2500, + headers: { + 'Client-ID': Meteor.settings.private.IGDB_API_ID, + Authorization: `Bearer ${bearerToken}`, + 'Content-Type': 'text/plain', + Accept: 'application/json', + }, + }); + const igdbAnswer = await igdbApi.post('screenshots', `fields *;where game = ${handler.gameId};`); + screenshotsCache[handler.gameId] = igdbAnswer.data; + } + res.end(JSON.stringify({screenshots: screenshotsCache[handler.gameId]})); + }else{ + res.end(JSON.stringify({error: 'No handler ID provided'})) + } + res.end(JSON.stringify({error: 'Unknown error'})) +}) +/* End of dirty PoC */ + WebApp.connectHandlers.use('/api/v1/hubstats', async (req, res, next) => { res.writeHead(200); let downloadsSum = 0; @@ -147,4 +185,4 @@ Meteor.publish( url: 'api/v1/allhandlers', httpMethod: 'get', }, -); \ No newline at end of file +);