Skip to content

Commit

Permalink
Adding a screenshot API route IGDB proxy (dirty cache)
Browse files Browse the repository at this point in the history
  • Loading branch information
r-mach committed Dec 11, 2021
1 parent ad3bffc commit 7f02a9a
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 2 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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"
```
2 changes: 1 addition & 1 deletion imports/api/Handlers/server/igdb-methods.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
40 changes: 39 additions & 1 deletion imports/api/Handlers/server/publications.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -147,4 +185,4 @@ Meteor.publish(
url: 'api/v1/allhandlers',
httpMethod: 'get',
},
);
);

0 comments on commit 7f02a9a

Please sign in to comment.