Closed MadeByIToncek closed 2 years ago
Code that saved it. Store somewhere safe :)
const express = require('express')
const app = express()
const port = 3000
const fetch = require('node-fetch');
function biggestNumberInArray (arr) {
// The largest number at first should be the first element or null for empty array
var largest = arr[0] || null;
// Current number, handled by the loop
var number = null;
for (var i = 0; i < arr.length; i++) {
// Update current number
number = arr[i];
// Compares stored largest number with current number, stores the largest one
largest = Math.max(largest, number);
}
return largest;
}
app.get('/', (req, res) => {
res.send('Hello World!')
})
app.get('/api/v1/:project/', (req, res) => {
let project = req.params.project
fetch( 'https://papermc.io/api/v2/projects/' + project )
.then( response => response.json() )
.then( response => {
res.json(response)
} );
})
app.get('/api/v1/:project/:version/latest/', (req, res) => {
let project = req.params.project
let version = req.params.version
let lastestbuild;
fetch( 'https://papermc.io/api/v2/projects/' + project + "/versions/" + version )
.then( response => response.json() )
.then( response => lastestbuild = biggestNumberInArray(response.builds))
.then( response => {
res.send(lastestbuild.toString())
});
})
app.get('/api/v1/:project/:version/latest/download/', (req, res) => {
let project = req.params.project
let version = req.params.version
let lastestbuild;
fetch( 'https://papermc.io/api/v2/projects/' + project + "/versions/" + version )
.then( response => response.json() )
.then( response => lastestbuild = biggestNumberInArray(response.builds))
.then( response => {
res.redirect("https://papermc.io/api/v2/projects/" + project + "/versions/" + version + "/builds/" + lastestbuild + "/downloads/" + project + "-" + version + "-" + lastestbuild + ".jar")
});
})
app.listen(port, () => {
console.log(`Example app listening at http://localhost:${port}`)
})```
Is it possible to somehow not rely on third party URLs and use paper API directly?
I know, it's all spagheti code but it works.