From cdd088aef1d28737df76083946757f50a063a852 Mon Sep 17 00:00:00 2001 From: Luke Robles Date: Sun, 8 Aug 2021 11:12:22 -0700 Subject: [PATCH] Adding battlefield 5 api stuff --- app/animals.py | 1 + app/bf5.py | 63 +++++++++++++++++++++++++++++++++++++++++++++ app/bot.py | 11 ++++++-- app/help_methods.py | 6 ++++- 4 files changed, 78 insertions(+), 3 deletions(-) create mode 100644 app/bf5.py diff --git a/app/animals.py b/app/animals.py index 5cb0c38f..16d5caf7 100644 --- a/app/animals.py +++ b/app/animals.py @@ -86,6 +86,7 @@ def random_dog_reddit(): 'tippytaps', 'vizsla', 'zoomies', + 'shiba', ] ) diff --git a/app/bf5.py b/app/bf5.py new file mode 100644 index 00000000..6c144076 --- /dev/null +++ b/app/bf5.py @@ -0,0 +1,63 @@ +import requests, json, pprint, discord +from bs4 import BeautifulSoup + +def get_player(player): + # try: + url = "https://battlefieldtracker.com/bfv/profile/origin/" + player + "/overview" + headers = { 'User-Agent' : 'Mozilla/5.0 (Windows NT 6.1; Win64; x64)' } + response = requests.get(url, headers=headers).text + soup = BeautifulSoup(response, "html.parser") + script = soup.find_all("script") + + jsonstring = str(script[1]) + jsonstring = jsonstring[33:-131] + data = json.loads(jsonstring) + + # Get Player Data + origin_name = 'bfv|origin|' + player + + base_stat_blob = data['stats-v2']['standardProfiles'][origin_name] + account_name = base_stat_blob['platformInfo']['platformUserHandle'] + avatar_url = base_stat_blob['platformInfo']['avatarUrl'] + + stats_overview = base_stat_blob['segments'][0]['stats'] + rank = stats_overview['rank'] + rank_title = rank['metadata']['label'] + rank_number = rank['displayValue'] + + # Build the embed + embed = discord.Embed(description='-------', color=15105570, type="rich") + embed.set_thumbnail(url=avatar_url) + embed.set_author(name="Battlefield V stats for %s" % account_name) + embed.add_field(name="**Rank**", value="%s, %s" % (rank_number, rank_title), inline=False) + + gameplay_stats = [ + 'scorePerMinute', + 'kdRatio', + 'assists', + 'shotsAccuracy', + 'killStreak', + 'dogtagsTaken', + 'headshots', + 'longestHeadshot', + 'heals', + 'revives', + 'revivesRecieved', + 'resupplies', + 'aceSquad', + 'wlPercentage', + ] + + for stat in gameplay_stats: + # print(stat) + embed.add_field(name="**%s**" % stat.title(), value=stats_overview[stat]['displayValue'], inline=False) + # embed.add_field(name='Raw value', value=stats_overview[stat]['displayValue'], inline=True) + # embed.add_field(name='Global Percentile', value=stats_overview[stat]['percentile'], inline=True) + return embed +# except Exception: +# return 'nope' + +# assault_stats = base_stat_blob['segments'][9] +# medic_stats = base_stat_blob['segments'][10] + +# pp.pprint(medic_stats) \ No newline at end of file diff --git a/app/bot.py b/app/bot.py index bd8dd730..7f15aa64 100755 --- a/app/bot.py +++ b/app/bot.py @@ -442,13 +442,20 @@ async def eight_ball(ctx): await ctx.send(":8ball: %s :8ball:" % result) +@bot.command(name='bf5') +async def bf5(ctx): + import bf5 + try: + await ctx.send(embed=bf5.get_player(ctx.message.content.split()[1])) + except Exception: + await ctx.send("I encountered an error while searching for that player.\nPlease check that your player name is spelled correctly") + @bot.command(name='ffxiv') async def ffxiv(ctx): import ffxiv async with ctx.message.channel.typing(): try: - ffxiv_embed = ffxiv.parse_message(ctx.message.content) - await ctx.send(embed=ffxiv_embed) + await ctx.send(embed=ffxiv.parse_message(ctx.message.content)) except Exception: await ctx.send("I encountered an error while searching for that player.\nPlease check that your player name and server are spelled correctly") diff --git a/app/help_methods.py b/app/help_methods.py index d7806bad..846c6e5b 100644 --- a/app/help_methods.py +++ b/app/help_methods.py @@ -25,6 +25,10 @@ def get_help_message(method): 'Returns a random bird photo', '\nUsage: !birb' ], + 'bf5': [ + 'Returns some API stats about a battlefield 5 character', + '\nUsage: !bf5 ' + ], 'dale': [ 'Posts a photo or video of Dale, the goodest boy.', '\nUsage: !dale' @@ -217,7 +221,7 @@ def get_help_message(method): def get_help_embed(bot): categories = { - 'fun': ['ffxiv', 'clap', 'youtube', 'excuse', 'greentext', 'lewd', 'message', 'meme', 'homepage', 'pout', 'roll', 'smug', 'wink', 'verify', 'horny', 'wasted', 'simp', 'trackdays'], + 'fun': ['bf5', 'ffxiv', 'clap', 'youtube', 'excuse', 'greentext', 'lewd', 'message', 'meme', 'homepage', 'pout', 'roll', 'smug', 'wink', 'verify', 'horny', 'wasted', 'simp', 'trackdays'], 'util': ['ask', 'corona', 'emoji', '8ball', 'decide', 'info', 'icon', 'wallpaper', 'stock', 'tts', 'issue'], 'users': ['help', 'invite', 'purge', 'roles', 'source'], 'pictures of animals': ['birb', 'redpanda,' 'dale', 'dog', 'rat', 'koala', 'panda', 'raccoon', 'fox', 'cat', 'kangaroo'],