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,73 +1,7 @@
# -*- coding: utf-8 -*-
from util import hook
from util import hook, text
@hook.command
def munge(inp, munge_count=0):
def munge(inp):
"munge <text> -- Munges up <text>."
reps = 0
for n in xrange(len(inp)):
rep = character_replacements.get(inp[n])
if rep:
inp = inp[:n] + rep.decode('utf8') + inp[n + 1:]
reps += 1
if reps == munge_count:
break
return inp
character_replacements = {
'a': 'ä',
'b': 'Б',
'c': 'ċ',
'd': 'đ',
'e': 'ë',
'f': 'ƒ',
'g': 'ġ',
'h': 'ħ',
'i': 'í',
'j': 'ĵ',
'k': 'ķ',
'l': 'ĺ',
'm': '',
'n': 'ñ',
'o': 'ö',
'p': 'ρ',
'q': 'ʠ',
'r': 'ŗ',
's': 'š',
't': 'ţ',
'u': 'ü',
'v': '',
'w': 'ω',
'x': 'χ',
'y': 'ÿ',
'z': 'ź',
'A': 'Å',
'B': 'Β',
'C': 'Ç',
'D': 'Ď',
'E': 'Ē',
'F': '',
'G': 'Ġ',
'H': 'Ħ',
'I': 'Í',
'J': 'Ĵ',
'K': 'Ķ',
'L': 'Ĺ',
'M': 'Μ',
'N': 'Ν',
'O': 'Ö',
'P': 'Р',
'Q': '',
'R': 'Ŗ',
'S': 'Š',
'T': 'Ţ',
'U': 'Ů',
'V': '',
'W': 'Ŵ',
'X': 'Χ',
'Y': '',
'Z': 'Ż'}
return text.munge(inp)

View file

@ -5,18 +5,22 @@ import re
from util import hook, timesince
db_ready = False
def db_init(db):
"check to see that our db has the the seen table and return a connection."
db.execute("create table if not exists seen_user(name, time, quote, chan, host, "
"primary key(name, chan))")
db.commit()
db_ready = True
@hook.singlethread
@hook.event('PRIVMSG', ignorebots=False)
def seen_sieve(paraml, input=None, db=None, bot=None):
db_init(db)
if not db_ready:
db_init(db)
# keep private messages private
if input.chan[:1] == "#":
db.execute("insert or replace into seen_user(name, time, quote, chan, host)"
@ -38,7 +42,8 @@ def seen(inp, nick='', chan='', db=None, input=None):
if not re.match("^[A-Za-z0-9_|.-\]\[]*$", inp.lower()):
return "I can't look up that name, its impossible to use!"
db_init(db)
if not db_ready:
db_init(db)
last_seen = db.execute("select name, time, quote from seen_user where name"
" like ? and chan = ?", (inp, chan)).fetchone()

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))

View file

@ -1,3 +1,4 @@
# -*- coding: utf-8 -*-
""" formatting.py - handy functions for formatting text
this file contains code from the following URL:
<http://code.djangoproject.com/svn/django/trunk/django/utils/text.py>
@ -6,6 +7,74 @@
import re
def munge(text, munge_count=0):
"munges up text."
reps = 0
for n in xrange(len(text)):
rep = character_replacements.get(text[n])
if rep:
text = text[:n] + rep.decode('utf8') + text[n + 1:]
reps += 1
if reps == munge_count:
break
return text
character_replacements = {
'a': 'ä',
'b': 'Б',
'c': 'ċ',
'd': 'đ',
'e': 'ë',
'f': 'ƒ',
'g': 'ġ',
'h': 'ħ',
'i': 'í',
'j': 'ĵ',
'k': 'ķ',
'l': 'ĺ',
'm': '',
'n': 'ñ',
'o': 'ö',
'p': 'ρ',
'q': 'ʠ',
'r': 'ŗ',
's': 'š',
't': 'ţ',
'u': 'ü',
'v': '',
'w': 'ω',
'x': 'χ',
'y': 'ÿ',
'z': 'ź',
'A': 'Å',
'B': 'Β',
'C': 'Ç',
'D': 'Ď',
'E': 'Ē',
'F': '',
'G': 'Ġ',
'H': 'Ħ',
'I': 'Í',
'J': 'Ĵ',
'K': 'Ķ',
'L': 'Ĺ',
'M': 'Μ',
'N': 'Ν',
'O': 'Ö',
'P': 'Р',
'Q': '',
'R': 'Ŗ',
'S': 'Š',
'T': 'Ţ',
'U': 'Ů',
'V': '',
'W': 'Ŵ',
'X': 'Χ',
'Y': '',
'Z': 'Ż'}
def capitalize_first(line):
"""
capitalises the first letter of words