Fix issue with incomplete Steam profiles, fix to pep8 standards (except line length)

This commit is contained in:
Steven Smith 2013-07-23 02:53:17 +08:00
parent 21d608763b
commit 963f512d1e

View file

@ -1,32 +1,20 @@
from util import hook, http, web, text from util import hook, http, web, text, timesince
from urllib import urlencode from urllib import urlencode
from datetime import datetime from datetime import datetime
import re import re
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()
def timesince(dt, default="just now"):
now = datetime.utcnow()
diff = now - dt
periods = (
(diff.days / 365, "year", "years"),
(diff.days / 30, "month", "months"),
(diff.days / 7, "week", "weeks"),
(diff.days, "day", "days"),
(diff.seconds / 3600, "hour", "hours"),
(diff.seconds / 60, "minute", "minutes"),
(diff.seconds, "second", "seconds"),
)
for period, singular, plural in periods:
if period:
return "%d %s ago" % (period, singular if period == 1 else plural)
return default
@hook.command('sc', autohelp=False) @hook.command('sc', autohelp=False)
@hook.command(autohelp=False) @hook.command(autohelp=False)
"""steamcalc [username] [currency] - Gets value of steam account and total hours played. Uses steamcommunity.com/id/<nickname>. Uses IRC nickname if none provided. """ 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. """
currencies = {'USD': 'us', 'euro1': "de", 'euro2': 'no', currencies = {'USD': 'us', 'euro1': "de", 'euro2': 'no',
'pound': 'uk', 'rubles': 'ru', 'real': 'br', 'pound': 'uk', 'rubles': 'ru', 'real': 'br',
'yen': 'jp', 'dollars': 'us', 'german': 'de', 'yen': 'jp', 'dollars': 'us', 'german': 'de',
@ -37,48 +25,66 @@ def timesince(dt, default="just now"):
currency = None currency = None
dontsave = False dontsave = False
if not inp: if not inp:
user = db.execute("select acc from steam where nick=lower(?)", (nick,)).fetchone() user = db.execute("select acc from steam where nick=lower(?)", (nick,)).fetchone()
if not user: if not user:
inp = nick inp = nick
else:
inp = user[0]
dontsave = True
else:
if len(inp.split(" ")) > 1:
if inp.split(" ")[1] in currencies:
currency = currencies[inp.split(" ")[1]]
else: else:
return "Invalid currency!" inp = user[0]
inp = inp.split(" ")[0] dontsave = True
dontsave = False else:
if len(inp.split(" ")) > 1:
if inp.split(" ")[1] in currencies:
currency = currencies[inp.split(" ")[1]]
else:
return "Invalid currency!"
inp = inp.split(" ")[0]
dontsave = False
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:
name = soup.findAll('h1', {'class': 'header-title'})[1].text name = soup.findAll('h1', {'class': 'header-title'})[1].text
status = soup.findAll('td')[7].text status = soup.findAll('td')[7].text
if status == "Online": except Exception as e:
print e
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))
if status == "Online":
status = "\x033\x02Online\x02\x0f" status = "\x033\x02Online\x02\x0f"
elif status == "Offline": elif status == "Offline":
status = "\x034\x02Offline\x02\x0f" status = "\x034\x02Offline\x02\x0f"
elif status == "Away": elif status == "Away":
status = "\x038\x02Away\x02\x0f" status = "\x038\x02Away\x02\x0f"
elif status == "Busy": elif status == "Busy":
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
twdata = soup.find('h1', {'class': 'header-title pull-right'}).find('a')['data-text'].split(", ") worth = None
money = twdata[0].split("My #Steam account is worth ")[1] timeonsteam = None
time = twdata[1].split("and I spent ")[1].split(" playing games!")[0] gamesplayed = None
timeonsteam = soup.findAll('i')[1].text[1:-1].split(" ") try:
timestamp = datetime.strptime(timeonsteam[0]+" "+timeonsteam[1]+" "+timeonsteam[2] + " - " + timeonsteam[4]+" "+timeonsteam[5], "%B %d, %Y - %H:%M:%S UTC") twdata = soup.find('h1', {'class': 'header-title pull-right'}).find('a')['data-text'].split(", ")
timeonsteam = timesince(timestamp) money = twdata[0].split("My #Steam account is worth ")[1]
totalgames = soup.find('b').text time = twdata[1].split("and I spent ")[1].split(" playing games!")[0]
notplayed = soup.findAll('b')[1].text worth = "This Steam account is worth \x02%s\x02, and they've spent \x02%s\x02 playing games! " % (money, time)
nppercent = soup.findAll('i')[3].text[1:-1] except:
if not dontsave: pass
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:
pass
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:
pass
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()
return u"%s (%s): This Steam account is worth \x02%s\x02, and they've spent \x02%s\x02 playing games! Their Steam account was created %s! They have \x02%s games in their Steam library\x02, but \x02%s of them haven't been touched\x02! That's %s! %s" % (name, status, money, time, timeonsteam, totalgames, notplayed, nppercent, web.isgd("http://steamdb.info/calculator/?"+ urldata)) if not worth and not timeonsteam and not gamesplayed:
except Exception as e: return "I couldn't read the information for that user. %s" % web.isgd("http://steamdb.info/calculator/?" + urldata)
print e 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"\x02Unable to retrieve info for %s!\x02 Check that it's a valid Steam profile username (steamcommunity.com/id/<USERNAME>), check if your profile is private, or go here to search: %s" % (inp, web.isgd("http://steamdb.info/calculator/?"+ urldata))