Skip to content

Instantly share code, notes, and snippets.

@vjeux
Created July 12, 2021 06:32
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vjeux/475b7b3a33f89c5563a8de64161ccae4 to your computer and use it in GitHub Desktop.
Save vjeux/475b7b3a33f89c5563a8de64161ccae4 to your computer and use it in GitHub Desktop.
Trackmania fetch difficulty
/*
>> node difficulty.js qDkVXW6pEqrwH0WP86jilQW4uZ8
{ rankAT: 629, rankGold: 4332, rankSilver: 7256, rankBronze: 8158 }
>> node difficulty.js qDkVXW6pEqrwH0WP86jilQW4uZ8
$i$o$s$c30Vashj'ir { rankAT: 629, rankGold: 4332, rankSilver: 7256, rankBronze: 8158 }
>> node difficulty.js 5yg9Ga2Kh11Ow2b4E9f4_viMo_h
$o$s$n$fffT$0ffhe $fffB$0ffank $fffR$0ffobbery { rankAT: 423, rankGold: 5425, rankSilver: 7641, rankBronze: 8913 }
>> node difficulty.js xUxShKB4PKec7pICAsQfj3Gn33k
#zoomies { rankAT: 1175, rankGold: 6860, rankSilver: 8233, rankBronze: 8843 }
>> node difficulty.js ogZoUILPyiBb7xknMQJVnSaKBa
Perfecto Bonko Mapo { rankAT: 5495, rankGold: 5694, rankSilver: 5940, rankBronze: 6326 }
*/
const fetch = require('node-fetch');
async function fetchJSON(url) {
const response = await fetch(url);
const json = await response.json();
return json;
}
async function fetchRankFromTime(trackID, time) {
const json = await fetchJSON(
'https://trackmania.io/api/leaderboard/map/' + trackID + '?from=' + time
);
return json.tops[0].position;
}
async function fetchRanks(trackID) {
try {
const map = await fetchJSON('https://trackmania.io/api/map/' + trackID);
const [rankAT, rankGold, rankSilver, rankBronze] = await Promise.all([
fetchRankFromTime(trackID, map.authorScore),
fetchRankFromTime(trackID, map.goldScore),
fetchRankFromTime(trackID, map.silverScore),
fetchRankFromTime(trackID, map.bronzeScore),
]);
console.log(map.name, {rankAT, rankGold, rankSilver, rankBronze});
} catch (e) {
console.error(e)
}
}
fetchRanks(process.argv[2] || 'qDkVXW6pEqrwH0WP86jilQW4uZ8').then();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment