aboutsummaryrefslogtreecommitdiff
path: root/api.py
diff options
context:
space:
mode:
authoralex <[email protected]>2026-07-12 10:19:24 +0200
committeralex <[email protected]>2026-07-12 10:19:24 +0200
commitb63c2933b244feebeb1c26fd31b02e5655a867e1 (patch)
treedcd84844a0ccd0ed6c41cb94dbc02e8473c7cec0 /api.py
downloadleague-discord-widget-main.tar.xz
league-discord-widget-main.zip
Diffstat (limited to 'api.py')
-rw-r--r--api.py112
1 files changed, 112 insertions, 0 deletions
diff --git a/api.py b/api.py
new file mode 100644
index 0000000..f1ccfad
--- /dev/null
+++ b/api.py
@@ -0,0 +1,112 @@
+import os
+import requests
+import sys
+from models import User
+
+def get_latest_ddragon_version():
+ url = "https://ddragon.leagueoflegends.com/api/versions.json"
+ response = requests.get(url)
+ return response.json()[0]
+
+def get_champion_mappings(version):
+ url = f"https://ddragon.leagueoflegends.com/cdn/{version}/data/en_US/champion.json"
+ champs_data = requests.get(url).json()["data"]
+
+ mapping = {}
+ for champ_string_id, info in champs_data.items():
+ mapping[int(info["key"])] = {
+ "name": info["name"],
+ "image_key": info["id"]
+ }
+ return mapping
+
+def get_player_puuid(user):
+ headers = {"X-Riot-Token": os.getenv("RIOT_API_KEY")}
+ region = user.region.lower()
+ name = user.game_name
+ tag = user.tag_line
+
+ url = f"https://{region}.api.riotgames.com/riot/account/v1/accounts/by-riot-id/{name}/{tag}"
+ response = requests.get(url, headers=headers)
+ if response.status_code != 200:
+ print(f"Failed to find Riot ID {name}#{tag}. Code: {response.status_code}")
+ sys.exit(1)
+ return response.json()["puuid"]
+
+def fetch_top_champions(user):
+ headers = {"X-Riot-Token": os.getenv("RIOT_API_KEY")}
+
+ url = f"https://{user.platform}.api.riotgames.com/lol/champion-mastery/v4/champion-masteries/by-puuid/{user.puuid}/top?count=4"
+ response = requests.get(url, headers=headers)
+ if response.status_code != 200:
+ print(f"Failed pulling masteries. Code: {response.status_code}")
+ sys.exit(1)
+ return response.json()
+
+def fetch_player_rank(user):
+ headers = {"X-Riot-Token": os.getenv("RIOT_API_KEY")}
+ platform = user.platform
+
+ url = f"https://{user.platform}.api.riotgames.com/lol/league/v4/entries/by-puuid/{user.puuid}"
+ response = requests.get(url, headers=headers)
+
+ if response.status_code != 200:
+ print(f"Failed pulling rank data. Code: {response.status_code}")
+ return "Unranked", "unranked"
+
+ entries = response.json()
+ target_queue = None
+
+ for entry in entries:
+ if entry.get("queueType") == "RANKED_SOLO_5x5":
+ target_queue = entry
+ break
+ if not target_queue and entries:
+ target_queue = entries[0]
+
+ if target_queue:
+ tier = target_queue["tier"].capitalize()
+ division = target_queue["rank"]
+ return f"{tier} {division}", tier.lower()
+
+ return "Unranked", "unranked"
+
+def update_discord_widget(dynamic_data, user):
+ app_id = os.getenv("DISCORD_APP_ID")
+ identity_id = os.getenv("DISCORD_IDENTITY_ID")
+ bot_token = os.getenv("DISCORD_BOT_TOKEN")
+
+ url = f"https://discord.com/api/v9/applications/{app_id}/users/{user.discord_id}/identities/{identity_id}/profile"
+
+ headers = {
+ "Content-Type": "application/json",
+ "Authorization": f"Bot {bot_token}",
+ "User-Agent": "DiscordBot (https://github.com/discord/discord-api-docs, 1.0.0)"
+ }
+
+ payload = {"data": {"dynamic": dynamic_data}}
+ response = requests.patch(url, headers=headers, json=payload)
+
+ if response.status_code in [200, 204]:
+ print("Success! Your Discord Profile Widget has been dynamically updated.")
+ else:
+ print(f"Discord API Rejected: {response.status_code} - {response.text}")
+
+def get_region_from_platform(platform: str) -> str:
+ mapping = {
+ "NA1": "AMERICAS",
+ "EUW1": "EUROPE",
+ "EUN1": "EUROPE",
+ "KR": "ASIA",
+ "JP1": "ASIA",
+ "BR1": "AMERICAS",
+ "LA1": "AMERICAS",
+ "LA2": "AMERICAS",
+ "OC1": "SEA",
+ "PH2": "SEA",
+ "SG2": "SEA",
+ "TH2": "SEA",
+ "TW2": "SEA",
+ "VN2": "SEA"
+ }
+ return mapping.get(platform.upper(), "EUROPE") \ No newline at end of file