Merge branch 'develop' of github.com:ClouDev/CloudBot into develop

This commit is contained in:
Luke Rogers 2012-03-08 16:21:51 +13:00
commit 02d2d82d38
3 changed files with 17 additions and 24 deletions

View file

@ -12,15 +12,9 @@ def format_quote(q, num, n_quotes):
def create_table_if_not_exists(db): def create_table_if_not_exists(db):
"Creates an empty quote table if one does not already exist" "Creates an empty quote table if one does not already exist"
db.execute('''CREATE TABLE IF NOT EXISTS quote ( db.execute("create table if not exists quote"
chan, "(chan, nick, add_nick, msg, time real, deleted default 0, "
nick, "primary key (chan, nick, msg))")
add_nick,
msg,
time real,
deleted default 0,
PRIMARY KEY (chan, nick, msg)
)''')
db.commit() db.commit()
def add_quote(db, chan, nick, add_nick, msg): def add_quote(db, chan, nick, add_nick, msg):
@ -37,15 +31,12 @@ def add_quote(db, chan, nick, add_nick, msg):
def del_quote(db, chan, nick, add_nick, msg): def del_quote(db, chan, nick, add_nick, msg):
"Deletes a quote from a nick" "Deletes a quote from a nick"
db.execute('''UPDATE quote db.execute('''UPDATE quote SET deleted = 1 WHERE
SET deleted = 1 chan=? AND lower(nick)=lower(?) AND msg=msg''')
WHERE chan=?
AND lower(nick)=lower(?)
AND msg=msg''')
db.commit() db.commit()
def get_quote_num(num, count, name): def get_quote_num(num, count, name):
"Returns the quote number desired from the database" "Returns the quote number to fetch from the DB"
if num: # Make sure num is a number if it isn't false if num: # Make sure num is a number if it isn't false
num = int(num) num = int(num)
if count == 0: # If there are no quotes in the database, raise an Exception. if count == 0: # If there are no quotes in the database, raise an Exception.
@ -62,9 +53,7 @@ def get_quote_num(num, count, name):
def get_quote_by_nick(db, nick, num=False): def get_quote_by_nick(db, nick, num=False):
"Returns a formatted quote from a nick, random or selected by number" "Returns a formatted quote from a nick, random or selected by number"
count = db.execute('''SELECT COUNT(*) count = db.execute('''SELECT COUNT(*) FROM quote WHERE deleted != 1
FROM quote
WHERE deleted != 1
AND lower(nick) = lower(?)''', [nick]).fetchall()[0][0] AND lower(nick) = lower(?)''', [nick]).fetchall()[0][0]
try: try:

View file

@ -12,28 +12,32 @@ class ShortenError(Exception):
def __str__(self): def __str__(self):
return repr(self.value) return repr(self.value)
def bitly(url, user, apikey): def bitly(url, user, apikey):
try: try:
if url[:8] == "https://":
pass
elif url[:7] != "http://":
url = "http://" + url
params = urlencode({'longUrl': url, 'login': user, 'apiKey': apikey, 'format': 'json'}) params = urlencode({'longUrl': url, 'login': user, 'apiKey': apikey, 'format': 'json'})
j = http.get_json("http://api.bit.ly/v3/shorten?%s" % params) j = http.get_json("http://api.bit.ly/v3/shorten?%s" % params)
if j['status_code'] == 200: if j['status_code'] == 200:
return j['data']['url'] return j['data']['url']
raise ShortenError('%s'%j['status_txt']) raise ShortenError('%s' % j['status_txt'])
except (HTTPError, ShortenError): except (HTTPError, ShortenError):
return "Could not shorten %s!" % url return "Could not shorten %s!" % url
@hook.command @hook.command
def shorten(inp, bot=None): def shorten(inp, bot = None):
".shorten <url> - Makes an j.mp/bit.ly shortlink to the url provided." ".shorten <url> - Makes an j.mp/bit.ly shortlink to the url provided."
api_user = bot.config.get("api_keys", {}).get("bitly_user", None) api_user = bot.config.get("api_keys", {}).get("bitly_user", None)
api_key = bot.config.get("api_keys", {}).get("bitly_api", None) api_key = bot.config.get("api_keys", {}).get("bitly_api", None)
if api_key is None: if api_key is None:
return "error: no api key set" return "error: no api key set"
return bitly(inp, api_user, api_key) return bitly(inp, api_user, api_key)
@hook.command @hook.command
def expand(inp, bot=None): def expand(inp, bot = None):
".expand <url> - Gets the original URL from a shortened link." ".expand <url> - Gets the original URL from a shortened link."
try: try:
url = http.get_url(inp) url = http.get_url(inp)

View file

@ -240,7 +240,7 @@ def timecommand(inp, say=None):
try: try:
return get_time(inp) return get_time(inp)
except ValueError: except ValueError:
return "Could not get time for " + inp + "!" return "Could not get the time for " + inp + "!"