This repository has been archived on 2023-04-13. You can view files and clone it, but cannot push or open issues or pull requests.
CloudBot/plugins/steam_calc.py

121 lines
3.5 KiB
Python
Raw Normal View History

2013-09-07 16:34:55 +02:00
import csv
import StringIO
2014-02-14 04:36:57 +01:00
from util import hook, http, text
gauge_url = "http://www.mysteamgauge.com/search?username={}"
2013-09-07 16:34:55 +02:00
api_url = "http://mysteamgauge.com/user/{}.csv"
steam_api_url = "http://steamcommunity.com/id/{}/?xml=1"
2013-11-12 07:06:06 +01:00
def refresh_data(name):
http.get(gauge_url.format(name), timeout=25, get_method='HEAD')
def get_data(name):
return http.get(api_url.format(name))
2013-09-07 16:34:55 +02:00
def is_number(s):
try:
float(s)
return True
except ValueError:
return False
def unicode_dictreader(utf8_data, **kwargs):
csv_reader = csv.DictReader(utf8_data, **kwargs)
for row in csv_reader:
yield dict([(key.lower(), unicode(value, 'utf-8')) for key, value in row.iteritems()])
2013-09-07 16:34:55 +02:00
@hook.command('sc')
@hook.command
def steamcalc(inp, reply=None):
2013-09-07 16:34:55 +02:00
"""steamcalc <username> [currency] - Gets value of steam account and
total hours played. Uses steamcommunity.com/id/<nickname>. """
2014-02-25 01:36:52 +01:00
# check if the user asked us to force reload
force_reload = inp.endswith(" forcereload")
if force_reload:
name = inp[:-12].strip().lower()
else:
name = inp.strip()
2013-09-07 16:34:55 +02:00
2014-02-25 01:36:52 +01:00
if force_reload:
try:
reply("Collecting data, this may take a while.")
refresh_data(name)
request = get_data(name)
do_refresh = False
except (http.HTTPError, http.URLError):
return "Could not get data for this user."
2014-02-25 01:36:52 +01:00
else:
try:
request = get_data(name)
do_refresh = True
except (http.HTTPError, http.URLError):
try:
reply("Collecting data, this may take a while.")
refresh_data(name)
request = get_data(name)
do_refresh = False
except (http.HTTPError, http.URLError):
return "Could not get data for this user."
2013-09-07 16:34:55 +02:00
2013-11-12 07:06:06 +01:00
csv_data = StringIO.StringIO(request) # we use StringIO because CSV can't read a string
2013-09-07 16:34:55 +02:00
reader = unicode_dictreader(csv_data)
2013-09-07 16:37:47 +02:00
# put the games in a list
2013-09-07 16:34:55 +02:00
games = []
for row in reader:
games.append(row)
data = {}
# basic information
steam_profile = http.get_xml(steam_api_url.format(name))
try:
data["name"] = steam_profile.find('steamID').text
online_state = steam_profile.find('stateMessage').text
except AttributeError:
return "Could not get data for this user."
2013-09-07 16:34:55 +02:00
2013-11-12 07:06:06 +01:00
online_state = online_state.replace("<br/>", ": ") # will make this pretty later
2013-09-10 09:30:40 +02:00
data["state"] = text.strip_html(online_state)
2013-09-07 16:34:55 +02:00
# work out the average metascore for all games
ms = [float(game["metascore"]) for game in games if is_number(game["metascore"])]
2013-09-10 04:53:25 +02:00
metascore = float(sum(ms)) / len(ms) if len(ms) > 0 else float('nan')
2013-09-07 16:34:55 +02:00
data["average_metascore"] = "{0:.1f}".format(metascore)
# work out the totals
data["games"] = len(games)
total_value = sum([float(game["value"]) for game in games if is_number(game["value"])])
2013-09-07 16:34:55 +02:00
data["value"] = str(int(round(total_value)))
# work out the total size
total_size = 0.0
for game in games:
if not is_number(game["size"]):
2013-09-07 16:34:55 +02:00
continue
if game["unit"] == "GB":
total_size += float(game["size"])
2013-09-07 16:34:55 +02:00
else:
2013-09-10 04:53:25 +02:00
total_size += float(game["size"]) / 1024
2013-09-07 16:34:55 +02:00
data["size"] = "{0:.1f}".format(total_size)
2013-11-12 07:06:06 +01:00
reply("{name} ({state}) has {games} games with a total value of ${value}"
2014-02-14 05:03:08 +01:00
" and a total size of {size}GB! The average metascore for these"
" games is {average_metascore}.".format(**data))
if do_refresh:
refresh_data(name)