2013-09-08 02:34:55 +12:00
import csv
import StringIO
2014-02-14 16:36:57 +13:00
from util import hook , http , text
2013-09-08 03:11:27 +12:00
gauge_url = " http://www.mysteamgauge.com/search?username= {} "
2013-09-08 02:34:55 +12: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-10 12:00:29 +12:00
2013-09-08 02:34:55 +12: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 :
2013-09-08 02:43:11 +12:00
yield dict ( [ ( key . lower ( ) , unicode ( value , ' utf-8 ' ) ) for key , value in row . iteritems ( ) ] )
2013-09-08 02:34:55 +12:00
@hook.command ( ' sc ' )
@hook.command
2013-09-08 03:26:34 +12:00
def steamcalc ( inp , reply = None ) :
2013-09-08 02:34:55 +12:00
""" steamcalc <username> [currency] - Gets value of steam account and
total hours played . Uses steamcommunity . com / id / < nickname > . """
2014-02-25 13:36:52 +13: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-08 02:34:55 +12:00
2014-02-25 13:36:52 +13:00
if force_reload :
2013-09-10 12:00:29 +12:00
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 13:36:52 +13: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-08 02:34:55 +12: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-08 02:34:55 +12:00
reader = unicode_dictreader ( csv_data )
2013-09-08 02:37:47 +12:00
# put the games in a list
2013-09-08 02:34:55 +12:00
games = [ ]
for row in reader :
games . append ( row )
data = { }
# basic information
steam_profile = http . get_xml ( steam_api_url . format ( name ) )
2013-09-10 12:00:29 +12:00
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-08 02:34:55 +12:00
2013-11-12 07:06:06 +01:00
online_state = online_state . replace ( " <br/> " , " : " ) # will make this pretty later
2013-09-10 19:30:40 +12:00
data [ " state " ] = text . strip_html ( online_state )
2013-09-08 02:34:55 +12:00
# work out the average metascore for all games
2013-09-08 02:43:11 +12:00
ms = [ float ( game [ " metascore " ] ) for game in games if is_number ( game [ " metascore " ] ) ]
2013-09-10 14:53:25 +12:00
metascore = float ( sum ( ms ) ) / len ( ms ) if len ( ms ) > 0 else float ( ' nan ' )
2013-09-08 02:34:55 +12:00
data [ " average_metascore " ] = " {0:.1f} " . format ( metascore )
# work out the totals
data [ " games " ] = len ( games )
2013-09-08 02:43:11 +12:00
total_value = sum ( [ float ( game [ " value " ] ) for game in games if is_number ( game [ " value " ] ) ] )
2013-09-08 02:34:55 +12:00
data [ " value " ] = str ( int ( round ( total_value ) ) )
# work out the total size
total_size = 0.0
for game in games :
2013-09-08 02:43:11 +12:00
if not is_number ( game [ " size " ] ) :
2013-09-08 02:34:55 +12:00
continue
2013-09-08 02:43:11 +12:00
if game [ " unit " ] == " GB " :
total_size + = float ( game [ " size " ] )
2013-09-08 02:34:55 +12:00
else :
2013-09-10 14:53:25 +12:00
total_size + = float ( game [ " size " ] ) / 1024
2013-09-08 02:34:55 +12: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 17:03:08 +13:00
" and a total size of {size} GB! The average metascore for these "
" games is {average_metascore} . " . format ( * * data ) )
2013-09-10 12:00:29 +12:00
if do_refresh :
refresh_data ( name )