Added brand new steam_calc.py plugin
This commit is contained in:
parent
fac18416a8
commit
b40b4640d8
2 changed files with 80 additions and 130 deletions
80
plugins/steam_calc.py
Normal file
80
plugins/steam_calc.py
Normal file
|
@ -0,0 +1,80 @@
|
|||
from util import hook, http
|
||||
import csv
|
||||
import StringIO
|
||||
|
||||
api_url = "http://mysteamgauge.com/user/{}.csv"
|
||||
steam_api_url = "http://steamcommunity.com/id/{}/?xml=1"
|
||||
|
||||
|
||||
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, unicode(value, 'utf-8')) for key, value in row.iteritems()])
|
||||
|
||||
|
||||
@hook.command('sc')
|
||||
@hook.command
|
||||
def steamcalc(inp):
|
||||
"""steamcalc <username> [currency] - Gets value of steam account and
|
||||
total hours played. Uses steamcommunity.com/id/<nickname>. """
|
||||
|
||||
name = inp.strip()
|
||||
|
||||
try:
|
||||
request = http.get(api_url.format(name))
|
||||
except (http.HTTPError, http.URLError):
|
||||
return "Could not get data for {}!".format(name)
|
||||
|
||||
csv_data = StringIO.StringIO(request)
|
||||
reader = unicode_dictreader(csv_data)
|
||||
|
||||
games = []
|
||||
for row in reader:
|
||||
games.append(row)
|
||||
|
||||
data = {}
|
||||
|
||||
# basic information
|
||||
steam_profile = http.get_xml(steam_api_url.format(name))
|
||||
data["name"] = steam_profile.find('steamID').text
|
||||
|
||||
online_state = steam_profile.find('onlineState').text
|
||||
data["state"] = online_state # will make this pretty later
|
||||
|
||||
# work out the average metascore for all games
|
||||
ms = [float(game["Metascore"]) for game in games if is_number(game["Metascore"])]
|
||||
metascore = float(sum(ms))/len(ms) if len(ms) > 0 else float('nan')
|
||||
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"])])
|
||||
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"]):
|
||||
continue
|
||||
|
||||
if game["Unit"] == "GB":
|
||||
total_size += float(game["Size"])
|
||||
else:
|
||||
total_size += float(game["Size"])/1024
|
||||
|
||||
data["size"] = "{0:.1f}".format(total_size)
|
||||
|
||||
|
||||
return "{name} ({state}) has {games} games with a total value of ${value}" \
|
||||
" and a total size of {size}GB! The average metascore for these" \
|
||||
" games is {average_metascore}.".format(**data)
|
Reference in a new issue