dragon-bot/app/apex_legends.py

90 lines
3.0 KiB
Python
Executable File

import discord
import os
import requests
def get_player(player):
url = "https://public-api.tracker.gg/v2/apex/standard/profile/origin/" + player
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 6.1; Win64; x64)",
"accept": "application/json",
"TRN-Api-Key": os.getenv("tracker_network_token"),
}
response = requests.get(url, headers=headers).json()["data"]
# Build the embed
embed = discord.Embed(description="-------", color=discord.Color.red(), type="rich")
embed.set_thumbnail(url=response["platformInfo"]["avatarUrl"])
embed.set_author(
name="Apex stats for %s" % response["platformInfo"]["platformUserId"],
icon_url=response["segments"][0]["stats"]["peakRankScore"]["metadata"][
"iconUrl"
],
)
embed.add_field(
name="**Current Rank**",
value=response["segments"][0]["stats"]["peakRankScore"]["displayName"],
inline=True,
)
embed.add_field(name="Account Wide stats", value="-----", inline=False)
stats_we_care_about = ["wins", "level", "kills", "damage", "headshots", "revives"]
for stat in stats_we_care_about:
embed.add_field(
name="**%s**" % response["segments"][0]["stats"][stat]["displayName"],
value=response["segments"][0]["stats"][stat]["displayValue"],
inline=True,
)
all_legends = response["segments"][1:]
# Calculate their most effective legends
embed.add_field(name="Bests", value="-----", inline=False)
embed.add_field(
name="**Legend with most kills**",
value="%s: %s"
% (
find_best(all_legends, key="kills")["metadata"]["name"],
find_best(all_legends, key="kills")["stats"]["kills"]["displayValue"],
),
)
embed.add_field(
name="**Legend with most wins**",
value="%s: %s"
% (
find_best(all_legends, key="wins")["metadata"]["name"],
find_best(all_legends, key="wins")["stats"]["wins"]["displayValue"],
),
)
embed.add_field(
name="**Legend with most damage**",
value="%s: %s"
% (
find_best(all_legends, key="damage")["metadata"]["name"],
find_best(all_legends, key="damage")["stats"]["damage"]["displayValue"],
),
)
embed.add_field(
name="**Legend with most revives**",
value="%s: %s"
% (
find_best(all_legends, key="revives")["metadata"]["name"],
find_best(all_legends, key="revives")["stats"]["revives"]["displayValue"],
),
)
return embed
def find_best(blob, key):
"""
find_best(blob, key)
blob should be the list within the request you want to find the best of,
must be one of [weapopns, vehicles, classes, gamemodes, maps, gadgets], eg. response["weapons"],
and the key to use to make that distinction, for example, for weapons, you could use kills
"""
return max(blob, key=lambda x: x["stats"][key]["value"] if key in x else 0)