Moved munge to the text module, added steamcalc plugin with DB logging of values, added steamtop command to show top values.

This commit is contained in:
Luke Rogers 2012-11-11 04:47:12 +13:00
parent 25f9ecfa51
commit b9dee4e802
4 changed files with 119 additions and 79 deletions

View file

@ -1,34 +1,66 @@
from util import hook, http, web
from util import hook, http, web, text
import re
# this is still beta code. some things will be improved later.
steamcalc_url = "http://steamcalculator.com/id/{}/{}"
count_re = re.compile(r"Found (.*?) Games with a value of ")
region = "us" # can be "us", "eu" or "uk"
region = "us" # if you change this shit breaks
def db_init(db):
"check to see that our db has the the top steam users table."
db.execute("create table if not exists steam_rankings(id, value, count, "
"primary key(id))")
db.commit()
@hook.command
def steamcalc(inp):
def steamcalc(inp, db=None):
"steamcalc <user> -- Check the value of <user>s steam account."
db_init(db)
if " " in inp:
return "Invalid Steam ID"
url = steamcalc_url.format(http.quote_plus(inp), region)
uid = inp.strip().lower()
url = steamcalc_url.format(http.quote_plus(uid), region)
# get the web page
try:
page = http.get_html(url)
except Exception as e:
return "Could not get Steam game listing: {}".format(e)
# extract the info we need
try:
count = page.xpath("//div[@id='rightdetail']/text()")[0]
number = count_re.findall(count)[0]
count_textual = page.xpath("//div[@id='rightdetail']/text()")[0]
count = int(count_re.findall(count_textual)[0])
value = page.xpath("//div[@id='rightdetail']/h1/text()")[0]
value_textual = page.xpath("//div[@id='rightdetail']/h1/text()")[0]
value = float(value_textual[1:][:-4]) # todo: make this less shit
except IndexError:
return "Could not get Steam game listing."
# save the info in the DB for steam rankings
db.execute("insert or replace into steam_rankings(id, value, count)"
"values(?,?,?)", (uid, value, count))
db.commit()
# shorten the URL
try:
short_url = web.isgd(url)
except web.ShortenError as e:
short_url = url
return u"Found {} games with a value of {}! - {}".format(number, value, short_url)
return u"Found {} games with a total value of ${} USD! - {}".format(count, value, short_url)
@hook.command
def steamtop(inp, db=None):
"steamtop -- Shows the top five users from steamcalc."
rows = []
for row in db.execute("SELECT id, value, count FROM steam_rankings ORDER BY value DESC LIMIT 5"):
rows.append(u"{} - \x02${}\x02 ({} games)".format(text.munge(row[0], 1), row[1], row[2]))
return u"Top Steam Users: {}".format(", ".join(rows))