Update steam.py

This commit is contained in:
Steven Smith 2013-07-23 03:07:25 +08:00
parent 963f512d1e
commit 0d35422d87

View file

@ -4,6 +4,13 @@ from datetime import datetime
import re import re
def shorten(inp):
try:
url = web.isgd(inp)
except (web.ShortenError, http.HTTPError):
url = inp
return url
def db_init(db): def db_init(db):
db.execute("create table if not exists steam(nick primary key, acc)") db.execute("create table if not exists steam(nick primary key, acc)")
db.commit() db.commit()
@ -35,10 +42,15 @@ def steamcalc(inp, nick='', db=None):
if len(inp.split(" ")) > 1: if len(inp.split(" ")) > 1:
if inp.split(" ")[1] in currencies: if inp.split(" ")[1] in currencies:
currency = currencies[inp.split(" ")[1]] currency = currencies[inp.split(" ")[1]]
dontsave = False
elif inp.split(" ")[1] == "dontsave":
dontsave = True
else: else:
return "Invalid currency!" return "Invalid currency!"
inp = inp.split(" ")[0] inp = inp.split(" ")[0]
dontsave = False if len(inp.split(" ")) > 2:
if inp.split(" ")[2] == "dontsave":
dontsave = True
urldata = urlencode({"player": inp, "currency": currency if currency else "us"}) urldata = urlencode({"player": inp, "currency": currency if currency else "us"})
soup = http.get_soup("http://steamdb.info/calculator/?" + urldata) soup = http.get_soup("http://steamdb.info/calculator/?" + urldata)
try: try:
@ -47,7 +59,7 @@ def steamcalc(inp, nick='', db=None):
except Exception as e: except Exception as e:
print e print e
return u"\x02Unable to retrieve info for %s!\x02 Is it a valid SteamCommunity profile username (%s)? " \ return u"\x02Unable to retrieve info for %s!\x02 Is it a valid SteamCommunity profile username (%s)? " \
"Check if your profile is private, or go here to search: %s" % (inp, web.isgd("http://steamcommunity.com/id/%s" % inp), web.isgd("http://steamdb.info/calculator/?" + urldata)) "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))
if status == "Online": if status == "Online":
status = "\x033\x02Online\x02\x0f" status = "\x033\x02Online\x02\x0f"
elif status == "Offline": elif status == "Offline":
@ -58,33 +70,30 @@ def steamcalc(inp, nick='', db=None):
status = "\x035\x02Busy\x02\x0f" status = "\x035\x02Busy\x02\x0f"
elif "Looking to" in status: elif "Looking to" in status:
status = "\x036\x02%s\x02\x0f" % status status = "\x036\x02%s\x02\x0f" % status
worth = None
timeonsteam = None
gamesplayed = None
try: try:
twdata = soup.find('h1', {'class': 'header-title pull-right'}).find('a')['data-text'].split(", ") twdata = soup.find('h1', {'class': 'header-title pull-right'}).find('a')['data-text'].split(", ")
money = twdata[0].split("My #Steam account is worth ")[1] money = twdata[0].split("My #Steam account is worth ")[1]
time = twdata[1].split("and I spent ")[1].split(" playing games!")[0] 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) worth = "This Steam account is worth \x02%s\x02, and they've spent \x02%s\x02 playing games! " % (money, time)
except: except:
pass worth = ""
try: try:
timeonsteam = soup.findAll('i')[1].text[1:-1].split(" ") 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") 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 = timesince.timesince(timestamp)
timeonsteam = "Their Steam account was created %s ago! " % timeonsteam timeonsteam = "Their Steam account was created %s ago! " % timeonsteam
except: except:
pass timeonsteam = ""
try: try:
totalgames = soup.find('b').text totalgames = soup.find('b').text
notplayed = soup.findAll('b')[1].text notplayed = soup.findAll('b')[1].text
nppercent = soup.findAll('i')[3].text[1:-1] 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) 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: except:
pass gamesplayed = ""
if not dontsave: if not dontsave:
db.execute("insert or replace into steam(nick, acc) values (?,?)", (nick.lower(), inp)) db.execute("insert or replace into steam(nick, acc) values (?,?)", (nick.lower(), inp))
db.commit() db.commit()
if not worth and not timeonsteam and not gamesplayed: if not worth and not timeonsteam and not gamesplayed:
return "I couldn't read the information for that user. %s" % web.isgd("http://steamdb.info/calculator/?" + urldata) 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 if worth else "", timeonsteam if timeonsteam else "", gamesplayed if gamesplayed else "", web.isgd("http://steamdb.info/calculator/?" + urldata)) return u"%s (%s): %s%s%s%s" % (name, status, worth, timeonsteam, gamesplayed, shorten("http://steamdb.info/calculator/?" + urldata))