Last active
March 18, 2024 15:41
-
-
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)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
I sincerely thank you
Based, thank you for sharing!
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
So helpful! <3