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:
parent
25f9ecfa51
commit
b9dee4e802
4 changed files with 119 additions and 79 deletions
|
@ -1,73 +1,7 @@
|
||||||
# -*- coding: utf-8 -*-
|
from util import hook, text
|
||||||
|
|
||||||
|
|
||||||
from util import hook
|
|
||||||
|
|
||||||
|
|
||||||
@hook.command
|
@hook.command
|
||||||
def munge(inp, munge_count=0):
|
def munge(inp):
|
||||||
"munge <text> -- Munges up <text>."
|
"munge <text> -- Munges up <text>."
|
||||||
reps = 0
|
return text.munge(inp)
|
||||||
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': 'Q',
|
|
||||||
'R': 'Ŗ',
|
|
||||||
'S': 'Š',
|
|
||||||
'T': 'Ţ',
|
|
||||||
'U': 'Ů',
|
|
||||||
'V': 'Ṿ',
|
|
||||||
'W': 'Ŵ',
|
|
||||||
'X': 'Χ',
|
|
||||||
'Y': 'Ỳ',
|
|
||||||
'Z': 'Ż'}
|
|
||||||
|
|
|
@ -5,17 +5,21 @@ import re
|
||||||
|
|
||||||
from util import hook, timesince
|
from util import hook, timesince
|
||||||
|
|
||||||
|
db_ready = False
|
||||||
|
|
||||||
|
|
||||||
def db_init(db):
|
def db_init(db):
|
||||||
"check to see that our db has the the seen table and return a connection."
|
"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, "
|
db.execute("create table if not exists seen_user(name, time, quote, chan, host, "
|
||||||
"primary key(name, chan))")
|
"primary key(name, chan))")
|
||||||
db.commit()
|
db.commit()
|
||||||
|
db_ready = True
|
||||||
|
|
||||||
|
|
||||||
@hook.singlethread
|
@hook.singlethread
|
||||||
@hook.event('PRIVMSG', ignorebots=False)
|
@hook.event('PRIVMSG', ignorebots=False)
|
||||||
def seen_sieve(paraml, input=None, db=None, bot=None):
|
def seen_sieve(paraml, input=None, db=None, bot=None):
|
||||||
|
if not db_ready:
|
||||||
db_init(db)
|
db_init(db)
|
||||||
# keep private messages private
|
# keep private messages private
|
||||||
if input.chan[:1] == "#":
|
if input.chan[:1] == "#":
|
||||||
|
@ -38,6 +42,7 @@ def seen(inp, nick='', chan='', db=None, input=None):
|
||||||
if not re.match("^[A-Za-z0-9_|.-\]\[]*$", inp.lower()):
|
if not re.match("^[A-Za-z0-9_|.-\]\[]*$", inp.lower()):
|
||||||
return "I can't look up that name, its impossible to use!"
|
return "I can't look up that name, its impossible to use!"
|
||||||
|
|
||||||
|
if not db_ready:
|
||||||
db_init(db)
|
db_init(db)
|
||||||
|
|
||||||
last_seen = db.execute("select name, time, quote from seen_user where name"
|
last_seen = db.execute("select name, time, quote from seen_user where name"
|
||||||
|
|
|
@ -1,34 +1,66 @@
|
||||||
from util import hook, http, web
|
from util import hook, http, web, text
|
||||||
import re
|
import re
|
||||||
|
|
||||||
|
# this is still beta code. some things will be improved later.
|
||||||
|
|
||||||
steamcalc_url = "http://steamcalculator.com/id/{}/{}"
|
steamcalc_url = "http://steamcalculator.com/id/{}/{}"
|
||||||
count_re = re.compile(r"Found (.*?) Games with a value of ")
|
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
|
@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:
|
if " " in inp:
|
||||||
return "Invalid Steam ID"
|
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:
|
try:
|
||||||
page = http.get_html(url)
|
page = http.get_html(url)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
return "Could not get Steam game listing: {}".format(e)
|
return "Could not get Steam game listing: {}".format(e)
|
||||||
|
|
||||||
|
# extract the info we need
|
||||||
try:
|
try:
|
||||||
count = page.xpath("//div[@id='rightdetail']/text()")[0]
|
count_textual = page.xpath("//div[@id='rightdetail']/text()")[0]
|
||||||
number = count_re.findall(count)[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:
|
except IndexError:
|
||||||
return "Could not get Steam game listing."
|
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:
|
try:
|
||||||
short_url = web.isgd(url)
|
short_url = web.isgd(url)
|
||||||
except web.ShortenError as e:
|
except web.ShortenError as e:
|
||||||
short_url = url
|
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))
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
|
# -*- coding: utf-8 -*-
|
||||||
""" formatting.py - handy functions for formatting text
|
""" formatting.py - handy functions for formatting text
|
||||||
this file contains code from the following URL:
|
this file contains code from the following URL:
|
||||||
<http://code.djangoproject.com/svn/django/trunk/django/utils/text.py>
|
<http://code.djangoproject.com/svn/django/trunk/django/utils/text.py>
|
||||||
|
@ -6,6 +7,74 @@
|
||||||
import re
|
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': 'Q',
|
||||||
|
'R': 'Ŗ',
|
||||||
|
'S': 'Š',
|
||||||
|
'T': 'Ţ',
|
||||||
|
'U': 'Ů',
|
||||||
|
'V': 'Ṿ',
|
||||||
|
'W': 'Ŵ',
|
||||||
|
'X': 'Χ',
|
||||||
|
'Y': 'Ỳ',
|
||||||
|
'Z': 'Ż'}
|
||||||
|
|
||||||
|
|
||||||
def capitalize_first(line):
|
def capitalize_first(line):
|
||||||
"""
|
"""
|
||||||
capitalises the first letter of words
|
capitalises the first letter of words
|
||||||
|
|
Reference in a new issue