2013-07-22 20:53:17 +02:00
from util import hook , http , web , text , timesince
2013-07-22 16:01:47 +02:00
from urllib import urlencode
from datetime import datetime
2012-11-08 09:56:56 +01:00
import re
2013-07-22 20:53:17 +02:00
2013-07-22 21:07:25 +02:00
def shorten ( inp ) :
try :
url = web . isgd ( inp )
except ( web . ShortenError , http . HTTPError ) :
url = inp
return url
2012-11-10 16:47:12 +01:00
def db_init ( db ) :
2013-07-22 16:01:47 +02:00
db . execute ( " create table if not exists steam(nick primary key, acc) " )
2012-11-10 16:47:12 +01:00
db . commit ( )
2012-11-08 09:56:56 +01:00
2013-07-22 16:01:47 +02:00
@hook.command ( ' sc ' , autohelp = False )
@hook.command ( autohelp = False )
2013-07-22 20:53:17 +02:00
def steamcalc ( inp , nick = ' ' , db = None ) :
""" steamcalc <username> [currency] - Gets value of steam account and
total hours played . Uses steamcommunity . com / id / < nickname > . Uses
IRC nickname if none provided . """
2013-07-22 16:01:47 +02:00
currencies = { ' USD ' : ' us ' , ' euro1 ' : " de " , ' euro2 ' : ' no ' ,
' pound ' : ' uk ' , ' rubles ' : ' ru ' , ' real ' : ' br ' ,
' yen ' : ' jp ' , ' dollars ' : ' us ' , ' german ' : ' de ' ,
' pounds ' : ' uk ' , ' russian ' : ' ru ' , ' brazil ' : ' br ' ,
' japan ' : ' jp ' , ' us ' : ' us ' , ' de ' : ' de ' , ' no ' : ' no ' ,
' uk ' : ' uk ' , ' ru ' : ' ru ' , ' br ' : ' br ' , ' jp ' : ' jp ' }
2012-11-10 16:47:12 +01:00
db_init ( db )
2013-07-22 16:01:47 +02:00
currency = None
dontsave = False
if not inp :
2013-07-22 20:53:17 +02:00
user = db . execute ( " select acc from steam where nick=lower(?) " , ( nick , ) ) . fetchone ( )
if not user :
inp = nick
2013-07-22 16:01:47 +02:00
else :
2013-07-22 20:53:17 +02:00
inp = user [ 0 ]
dontsave = True
else :
if len ( inp . split ( " " ) ) > 1 :
if inp . split ( " " ) [ 1 ] in currencies :
currency = currencies [ inp . split ( " " ) [ 1 ] ]
2013-07-22 21:07:25 +02:00
dontsave = False
elif inp . split ( " " ) [ 1 ] == " dontsave " :
dontsave = True
2013-07-22 20:53:17 +02:00
else :
return " Invalid currency! "
inp = inp . split ( " " ) [ 0 ]
2013-07-22 21:07:25 +02:00
if len ( inp . split ( " " ) ) > 2 :
if inp . split ( " " ) [ 2 ] == " dontsave " :
dontsave = True
2013-07-22 16:01:47 +02:00
urldata = urlencode ( { " player " : inp , " currency " : currency if currency else " us " } )
soup = http . get_soup ( " http://steamdb.info/calculator/? " + urldata )
2012-11-08 09:56:56 +01:00
try :
2013-07-22 20:53:17 +02:00
name = soup . findAll ( ' h1 ' , { ' class ' : ' header-title ' } ) [ 1 ] . text
status = soup . findAll ( ' td ' ) [ 7 ] . text
except Exception as e :
print e
return u " \x02 Unable to retrieve info for %s ! \x02 Is it a valid SteamCommunity profile username ( %s )? " \
2013-07-22 21:07:25 +02:00
" Check if your profile is private, or go here to search: %s " % ( inp , shorten ( " http://steamcommunity.com/id/ %s " % inp ) , shorten ( " http://steamdb.info/calculator/? " + urldata ) )
2013-07-22 20:53:17 +02:00
if status == " Online " :
2013-07-22 16:01:47 +02:00
status = " \x03 3 \x02 Online \x02 \x0f "
2013-07-22 20:53:17 +02:00
elif status == " Offline " :
2013-07-22 16:01:47 +02:00
status = " \x03 4 \x02 Offline \x02 \x0f "
2013-07-22 20:53:17 +02:00
elif status == " Away " :
2013-07-22 16:01:47 +02:00
status = " \x03 8 \x02 Away \x02 \x0f "
2013-07-22 20:53:17 +02:00
elif status == " Busy " :
2013-07-22 16:01:47 +02:00
status = " \x03 5 \x02 Busy \x02 \x0f "
2013-07-22 20:53:17 +02:00
elif " Looking to " in status :
2013-07-22 16:01:47 +02:00
status = " \x03 6 \x02 %s \x02 \x0f " % status
2013-07-22 20:53:17 +02:00
try :
twdata = soup . find ( ' h1 ' , { ' class ' : ' header-title pull-right ' } ) . find ( ' a ' ) [ ' data-text ' ] . split ( " , " )
money = twdata [ 0 ] . split ( " My #Steam account is worth " ) [ 1 ]
time = twdata [ 1 ] . split ( " and I spent " ) [ 1 ] . split ( " playing games! " ) [ 0 ]
worth = " This Steam account is worth \x02 %s \x02 , and they ' ve spent \x02 %s \x02 playing games! " % ( money , time )
except :
2013-07-22 21:07:25 +02:00
worth = " "
2013-07-22 20:53:17 +02:00
try :
timeonsteam = soup . findAll ( ' i ' ) [ 1 ] . text [ 1 : - 1 ] . split ( " " )
timestamp = datetime . strptime ( timeonsteam [ 0 ] + " " + timeonsteam [ 1 ] + " " + timeonsteam [ 2 ] + " - " + timeonsteam [ 4 ] + " " + timeonsteam [ 5 ] , " % B %d , % Y - % H: % M: % S UTC " )
timeonsteam = timesince . timesince ( timestamp )
timeonsteam = " Their Steam account was created %s ago! " % timeonsteam
except :
2013-07-22 21:07:25 +02:00
timeonsteam = " "
2013-07-22 20:53:17 +02:00
try :
totalgames = soup . find ( ' b ' ) . text
notplayed = soup . findAll ( ' b ' ) [ 1 ] . text
nppercent = soup . findAll ( ' i ' ) [ 3 ] . text [ 1 : - 1 ]
gamesplayed = " They have \x02 %s games in their Steam library \x02 , but \x02 %s of them haven ' t been touched \x02 ! That ' s \x02 %s \x02 ! " % ( totalgames , notplayed , nppercent )
except :
2013-07-22 21:07:25 +02:00
gamesplayed = " "
2013-07-22 20:53:17 +02:00
if not dontsave :
2013-07-22 16:01:47 +02:00
db . execute ( " insert or replace into steam(nick, acc) values (?,?) " , ( nick . lower ( ) , inp ) )
db . commit ( )
2013-07-22 20:53:17 +02:00
if not worth and not timeonsteam and not gamesplayed :
2013-07-22 21:07:25 +02:00
return " I couldn ' t read the information for that user. %s " % shorten ( " http://steamdb.info/calculator/? " + urldata )
return u " %s ( %s ): %s %s %s %s " % ( name , status , worth , timeonsteam , gamesplayed , shorten ( " http://steamdb.info/calculator/? " + urldata ) )