dragon-bot/app/apex_legends.py

127 lines
4.0 KiB
Python
Executable File

import datetime
import discord
import os
import requests
def get_player(player):
# player = player.lower()
# 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"),
# }
url = (
"https://api.mozambiquehe.re/bridge?auth=%s&player=%s&platform=PC&merge=True"
% (
os.getenv("apex_api_key"),
player,
)
)
# print(url)
response = requests.get(url).json()
# Build the embed
embed = discord.Embed(description="-------", color=discord.Color.red(), type="rich")
embed.set_thumbnail(url=response["global"]["rank"]["rankImg"])
embed.set_author(name="Apex stats for %s" % response["global"]["name"])
embed.add_field(
name="**Rank**",
value="%s, %s"
% (
response["global"]["rank"]["rankName"],
response["global"]["rank"]["rankScore"],
),
inline=False,
)
embed.add_field(
name="**Current level**",
value="%s, %s%% of the way to next level"
% (
response["global"]["level"],
response["global"]["toNextLevelPercent"],
),
inline=False,
)
gameplay_stats = {
"kills": "Total kills",
"kd": "K/D",
"executions": "Executions",
"revives": "Revies",
"games_played": "Games Played",
"headshots": "Headshots",
}
embed.add_field(name="Totals", value="-------", inline=False)
for stat, readable in gameplay_stats.items():
embed.add_field(
name="**%s**" % readable,
value=response["total"][stat]["value"],
inline=True,
)
embed.add_field(
name="Stats per legend", value="---------------------", inline=False
)
legends = response["legends"]["all"]
for legend in legends:
if "data" in response["legends"]["all"][legend] and legend != "Global":
data = response["legends"]["all"][legend]["data"]
smush = {x["key"]: x for x in data}
try:
legends_kills = smush["specialEvent_kills"]["value"] or 0
legends_damage = smush["specialEvent_damage"]["value"] or 0
legends_wins = smush["specialEvent_wins"]["value"] or 0
embed.add_field(
name=legend,
value="Total kills: %s\nTotal Damage: %s\nTotal Wins: %s"
% (legends_kills, legends_damage, legends_wins),
)
except Exception as e:
print("%s threw an exception on %s" % (legend, e))
pass
# best_weapon = find_best(response["weapons"], "kills")
# embed.add_field(
# name=":trophy:**Best Weapon**:trophy:",
# value="%s\nkills: %s\naccuracy: %s\nHeadshots: %s"
# % (
# best_weapon["weaponName"],
# best_weapon["kills"],
# best_weapon["accuracy"],
# best_weapon["headshotKills"],
# ),
# inline=True,
# )
# favorite_class = find_best(response["classes"], "secondsPlayed")
# embed.add_field(
# name=":trophy:**Favorite Class**:trophy:",
# value="%s\nKDR: %s\nTime Played: %s"
# % (
# favorite_class["characterName"],
# favorite_class["killDeath"],
# str(datetime.timedelta(seconds=favorite_class["secondsPlayed"])),
# ),
# inline=True,
# )
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[key])