aboutsummaryrefslogtreecommitdiff
path: root/api.py
blob: f1ccfad9bf495c13294ab5278cf4f99934cfc5c3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
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")