Skip to content

Instantly share code, notes, and snippets.

@Querijn
Last active March 18, 2024 15:41
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Querijn/5fe4b9f65a1236b98ead0bc39ab155ad to your computer and use it in GitHub Desktop.
Save Querijn/5fe4b9f65a1236b98ead0bc39ab155ad to your computer and use it in GitHub Desktop.
Get Champion by key or by ID with the latest DDragon version (Javascript)
let championByIdCache = {};
let championJson = {};
async function getLatestChampionDDragon(language = "en_US") {
if (championJson[language])
return championJson[language];
let response;
let versionIndex = 0;
do { // I loop over versions because 9.22.1 is broken
const version = (await fetch("http://ddragon.leagueoflegends.com/api/versions.json").then(async(r) => await r.json()))[versionIndex++];
response = await fetch(`https://ddragon.leagueoflegends.com/cdn/${version}/data/${language}/champion.json`);
}
while (!response.ok)
championJson[language] = await response.json();
return championJson[language];
}
async function getChampionByKey(key, language = "en_US") {
// Setup cache
if (!championByIdCache[language]) {
let json = await getLatestChampionDDragon(language);
championByIdCache[language] = {};
for (var championName in json.data) {
if (!json.data.hasOwnProperty(championName))
continue;
const champInfo = json.data[championName];
championByIdCache[language][champInfo.key] = champInfo;
}
}
return championByIdCache[language][key];
}
// NOTE: IN DDRAGON THE ID IS THE CLEAN NAME!!! It's also super-inconsistent, and broken at times.
// Cho'gath => Chogath, Wukong => Monkeyking, Fiddlesticks => Fiddlesticks/FiddleSticks (depending on what mood DDragon is in this patch)
async function getChampionByID(name, language = "en_US") {
return await getLatestChampionDDragon(language)[name];
}
async function main() {
const annie = await getChampionByKey(1, "en_US");
const leona = await getChampionByKey(89, "es_ES");
const brand = await getChampionByID("brand");
console.log(annie);
console.log(leona);
console.log(brand);
}
main();
@iEloyB
Copy link

iEloyB commented Mar 12, 2023

So helpful! <3

@jungleologyphong
Copy link

I sincerely thank you

@gcisneros310
Copy link

Based, thank you for sharing!

@stinooo
Copy link

stinooo commented Mar 18, 2024

python way to get from the championID to championNAME

import json

with open("champion.json", "r", encoding="utf-8") as file:
champion_data = json.load(file)

champion_name_key_map = {}

for champion_name, champion_info in champion_data["data"].items():
champion_key = champion_info["key"]
champion_name_key_map[champion_key] = champion_name

for key, name in champion_name_key_map.items():
print(f"Champion Key: {key}, Champion Name: {name}")

with open("champion_name_key_map.json", "w", encoding="utf-8") as outfile:
json.dump(champion_name_key_map, outfile, indent=4)

print("Champion name and key mapping has been written to 'champion_name_key_map.json'")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment