Skip to content
This repository has been archived by the owner on Oct 23, 2022. It is now read-only.

Commit

Permalink
Merge pull request #22 from ExtraRandom/master
Browse files Browse the repository at this point in the history
Overwatch update
  • Loading branch information
David Jetelina authored and David Jetelina committed Jun 5, 2016
2 parents c63bf66 + e08256f commit c118004
Showing 1 changed file with 52 additions and 29 deletions.
81 changes: 52 additions & 29 deletions cogs/overwatch.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

log = logging.getLogger(__name__)


class Overwatch:
def __init__(self, bot):
"""
Expand All @@ -18,12 +19,6 @@ def __init__(self, bot):

@commands.command(description=desc.ow, brief=desc.owb)
async def overwatch(self, region: str, battletag: str):
games_played = 0
games_won = 0
time_played = 0
count = 0
stat_count = 0

msg = await self.bot.say("Fetching Stats for {}".format(battletag))

user = battletag.replace("#", "-")
Expand Down Expand Up @@ -58,33 +53,15 @@ async def overwatch(self, region: str, battletag: str):
doc = bs4.BeautifulSoup(res.text, "html.parser")
page = doc.select('div')

# eof = len(page) - 1 # eof = end of file - left in as you could use it for the for loop "stuff in page" maybe

most_played = page[82].select('div')[2].getText()
most_games = page[82].select('div')[3].getText()

for stuff in page:
try:
if str(page[count].select('div')[0].select('td')[0].getText()) == "Games Won":
try:
stats = page[count].select('div')[0].select('td')

for _ in stats:
if stats[stat_count].getText() == "Games Won":
games_won = int(stats[stat_count + 1].getText())
if stats[stat_count].getText() == "Games Played":
games_played = int(stats[stat_count + 1].getText())
elif stats[stat_count].getText() == "Time Played":
time_played = stats[stat_count + 1].getText()
stat_count += 1

except Exception as e:
log.exception("Parsing HTML")
stats = doc.find_all('td')
# print(stats)

except Exception:
log.exception("ExtraRandom overwatch exception")

count += 1
games_won = find_value(stats, "Games Won")
games_played = find_value(stats, "Games Played")
time_played = find_value(stats, "Time Played")

games_lost = int(games_played) - int(games_won)
won_lost = "{}/{}".format(games_won, games_lost)
Expand All @@ -98,5 +75,51 @@ async def overwatch(self, region: str, battletag: str):
won_lost, most_played, most_games))


def find_value(stats, name):
"""
:param stats: stats list
:param name: name of value to find (i.e. Games Won)
:return: largest value for name (there are multiple game won's but the biggest will be the overall games won
rather than a character specific games won
"""

tagged_name = "<td>{}</td>".format(name)

things = []
is_time = False

hour = []
mins = []

for item in stats:
if name == "Time Played":
is_time = True

if str(item) == tagged_name:
time = item.next_sibling.getText().split()
if time[1] == "minutes" or time[1] == "minute":
mins.append(int(time[0]))
elif time[1] == "hours" or time[1] == "hour": # needed instead of else as it can also be in seconds
hour.append(int(time[0]))

elif str(item) == tagged_name:
things.append(int(item.next_sibling.getText()))

if is_time:
if not hour:
# meaning max play time is in minutes
return "{} minutes".format(max(mins))
# doesnt accommodate for 1 min playtime but seriously why would you even have that short a play time

elif hour:
max_time = max(hour)
sfx = "hours"
if max_time == 1:
sfx = "hour"
return "{} {}".format(max_time, sfx)

return max(things)


def setup(bot):
bot.add_cog(Overwatch(bot))

0 comments on commit c118004

Please sign in to comment.