From 6c6c10854936c0780152bf18dd846ee38ffea167 Mon Sep 17 00:00:00 2001 From: Steven Smith Date: Tue, 4 Feb 2014 03:37:27 +0800 Subject: [PATCH 1/7] Created plugin to parse Google urls to normal urls Example Google url: http://www.google.nl/url?sa=i&rct=j&q=&esrc=s&source=images&cd=&cad=rja&docid=kzd-kV9_31JsiM&tbnid=7Sl4EuqcSGY2wM:&ved=0CAIQjBw&url=http%3A%2F%2F4sysops.com%2Fwp-content%2Fuploads%2F2011%2F02%2FFTP.client.for_.the_.Windows.command.line_.FTPUse.gif&ei=2ujvUta0BKrV0QWdj4DoDg&bvm=bv.60444564,d.d2k&psig=AFQjCNEI_OCuyO1vWor1SoK_i5QGQYaY-Q&ust=1391540811442407 Example output url: http://4sysops.com/wp-content/uploads/2011/02/FTP.client.for_.the_.Windows.command.line_.FTPUse.gif --- plugins/googleurlparse.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 plugins/googleurlparse.py diff --git a/plugins/googleurlparse.py b/plugins/googleurlparse.py new file mode 100644 index 0000000..099fa4e --- /dev/null +++ b/plugins/googleurlparse.py @@ -0,0 +1,16 @@ +from util import hook +from urllib import unquote + +@hook.command(autohelp=False) +def googleurl(inp, db=None, input=None): + """googleurl [nickname] - Converts Google urls (google.com/url) to normal urls where possible, in the specified nickname's last message. If nickname isn't provided, action will be performed on user's last message""" + if not inp: + inp = input.nick + last_message = db.execute("select name, quote from seen_user where name" + " like ? and chan = ?", (inp.lower(), input.chan.lower())).fetchone() + if last_message: + msg = last_message[1] + out = ", ".join([(unquote(a[4:]) if a[:4] == "url=" else "") for a in msg.split("&")]).replace(", ,", "").strip() + return out if out else "No matches in your last message." + else: + return "You haven't said anything in this channel yet!" if inp == input.nick else "That user hasn't said anything in this channel yet!" From b6558ccdd53e75ea9045202bd99be1b576d2f957 Mon Sep 17 00:00:00 2001 From: Luke Rogers Date: Fri, 14 Feb 2014 19:43:36 +1300 Subject: [PATCH 2/7] I've been doing this all wrong for a long time --- plugins/encrypt.py | 13 +++++++------ plugins/factoids.py | 11 ++++++++--- plugins/horoscope.py | 12 ++++++------ plugins/notes.py | 8 ++++---- plugins/regex_chans.py | 8 ++++---- plugins/seen.py | 10 ++++++---- plugins/tell.py | 12 ++++++++---- 7 files changed, 43 insertions(+), 31 deletions(-) diff --git a/plugins/encrypt.py b/plugins/encrypt.py index 23d6a6f..e391a04 100644 --- a/plugins/encrypt.py +++ b/plugins/encrypt.py @@ -25,10 +25,12 @@ db_ready = False def db_init(db): """check to see that our db has the the encryption table.""" - db.execute("create table if not exists encryption(encrypted, iv, " - "primary key(encrypted))") - db.commit() - db_ready = True + global db_ready + if not db_ready: + db.execute("create table if not exists encryption(encrypted, iv, " + "primary key(encrypted))") + db.commit() + db_ready = True def get_salt(bot): @@ -42,8 +44,7 @@ def get_salt(bot): @hook.command def encrypt(inp, bot=None, db=None, notice=None): """encrypt -- Encrypts with . ( can only be decrypted using this bot)""" - if not db_ready: - db_init(db) + db_init(db) split = inp.split(" ") diff --git a/plugins/factoids.py b/plugins/factoids.py index 7468b66..403e6f5 100644 --- a/plugins/factoids.py +++ b/plugins/factoids.py @@ -7,6 +7,8 @@ from util import hook, http, text, pyexec re_lineends = re.compile(r'[\r\n]*') +db_ready = False + # some simple "shortcodes" for formatting purposes shortcodes = { '[b]': '\x02', @@ -18,9 +20,12 @@ shortcodes = { def db_init(db): - db.execute("create table if not exists mem(word, data, nick," - " primary key(word))") - db.commit() + global db_ready + if not db_ready: + db.execute("create table if not exists mem(word, data, nick," + " primary key(word))") + db.commit() + db_ready = True def get_memory(db, word): diff --git a/plugins/horoscope.py b/plugins/horoscope.py index 74b12e0..7145711 100644 --- a/plugins/horoscope.py +++ b/plugins/horoscope.py @@ -7,17 +7,17 @@ db_ready = False def db_init(db): """check to see that our db has the horoscope table and return a connection.""" - db.execute("create table if not exists horoscope(nick primary key, sign)") - db.commit() - db_ready = True + global db_ready + if not db_ready: + db.execute("create table if not exists horoscope(nick primary key, sign)") + db.commit() + db_ready = True @hook.command(autohelp=False) def horoscope(inp, db=None, notice=None, nick=None): """horoscope -- Get your horoscope.""" - - if not db_ready: - db_init(db) + db_init(db) # check if the user asked us not to save his details dontsave = inp.endswith(" dontsave") diff --git a/plugins/notes.py b/plugins/notes.py index 129b7d5..070a9fb 100644 --- a/plugins/notes.py +++ b/plugins/notes.py @@ -3,7 +3,7 @@ import re from util import hook -db_inited = False +db_ready = False def clean_sql(sql): @@ -11,8 +11,8 @@ def clean_sql(sql): def db_init(db): - global db_inited - if db_inited: + global db_ready + if db_ready: return exists = db.execute(""" @@ -32,7 +32,7 @@ def db_init(db): db.commit() - db_inited = True + db_ready = True def db_getall(db, nick, limit=-1): diff --git a/plugins/regex_chans.py b/plugins/regex_chans.py index f49411a..c16c250 100644 --- a/plugins/regex_chans.py +++ b/plugins/regex_chans.py @@ -6,15 +6,15 @@ from util import hook # If False, all channels without a setting will have regex disabled default_enabled = True -db_already_initiated = False +db_ready = False def db_init(db): - global db_already_initiated - if not db_already_initiated: - db_already_initiated = True + global db_ready + if not db_ready: db.execute("CREATE TABLE IF NOT EXISTS regexchans(channel PRIMARY KEY, status)") db.commit() + db_ready = True def get_status(db, channel): diff --git a/plugins/seen.py b/plugins/seen.py index 85d433a..65bfecb 100644 --- a/plugins/seen.py +++ b/plugins/seen.py @@ -11,10 +11,12 @@ 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 + global db_ready + if not db_ready: + 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 diff --git a/plugins/tell.py b/plugins/tell.py index 67f7ec3..b29cf5b 100644 --- a/plugins/tell.py +++ b/plugins/tell.py @@ -6,13 +6,17 @@ import re from util import hook, timesince +db_ready = False def db_init(db): """check to see that our db has the tell table and return a dbection.""" - db.execute("create table if not exists tell" - "(user_to, user_from, message, chan, time," - "primary key(user_to, message))") - db.commit() + global db_ready + if not db_ready: + db.execute("create table if not exists tell" + "(user_to, user_from, message, chan, time," + "primary key(user_to, message))") + db.commit() + db_ready = True return db From 2c6e01a41d410eed1b7bfcf1973b2db53f68c292 Mon Sep 17 00:00:00 2001 From: Luke Rogers Date: Fri, 14 Feb 2014 19:56:11 +1300 Subject: [PATCH 3/7] Fixed up old religion.py plugin, added to disabled_stuff --- disabled_stuff/religion.py | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 disabled_stuff/religion.py diff --git a/disabled_stuff/religion.py b/disabled_stuff/religion.py new file mode 100644 index 0000000..552b23f --- /dev/null +++ b/disabled_stuff/religion.py @@ -0,0 +1,38 @@ +from util import hook, http + + +@hook.command('god') +@hook.command +def bible(inp): + """.bible -- gets from the Bible (ESV)""" + + base_url = ('http://www.esvapi.org/v2/rest/passageQuery?key=IP&' + 'output-format=plain-text&include-heading-horizontal-lines&' + 'include-headings=false&include-passage-horizontal-lines=false&' + 'include-passage-references=false&include-short-copyright=false&' + 'include-footnotes=false&line-length=0&' + 'include-heading-horizontal-lines=false') + + text = http.get(base_url, passage=inp) + + text = ' '.join(text.split()) + + if len(text) > 400: + text = text[:text.rfind(' ', 0, 400)] + '...' + + return text + + +@hook.command('allah') +@hook.command +def koran(inp): # Koran look-up plugin by Ghetto Wizard + """.koran -- gets from the Koran""" + + url = 'http://quod.lib.umich.edu/cgi/k/koran/koran-idx?type=simple' + + results = http.get_html(url, q1=inp).xpath('//li') + + if not results: + return 'No results for ' + inp + + return results[0].text_content() From f7af1b0726fad19a958937f2c4d2bfb0bce2d2ee Mon Sep 17 00:00:00 2001 From: Luke Rogers Date: Fri, 14 Feb 2014 23:44:05 +1300 Subject: [PATCH 4/7] quote_plus() --- plugins/xkcd.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/plugins/xkcd.py b/plugins/xkcd.py index 77c33f0..fd632fb 100644 --- a/plugins/xkcd.py +++ b/plugins/xkcd.py @@ -17,13 +17,15 @@ def xkcd_info(xkcd_id, url=False): return "xkcd: \x02%s\x02 (%s)%s" % (data['title'], date, url if url else "") -def xkcd_search(inp): +def xkcd_search(term): + search_term = http.quote_plus(term) soup = http.get_soup("http://www.ohnorobot.com/index.pl?s={}&Search=Search&" - "comic=56&e=0&n=0&b=0&m=0&d=0&t=0".format(inp)) + "comic=56&e=0&n=0&b=0&m=0&d=0&t=0".format(search_term)) result = soup.find('li') if result: url = result.find('div', {'class': 'tinylink'}).text xkcd_id = url[:-1].split("/")[-1] + print xkcd_id return xkcd_info(xkcd_id, url=True) else: return "No results found!" From b0c6815085faf47955a0375d9931a12f3ad408bf Mon Sep 17 00:00:00 2001 From: Luke Rogers Date: Sat, 15 Feb 2014 00:49:48 +1300 Subject: [PATCH 5/7] fixed horoscope --- plugins/horoscope.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/plugins/horoscope.py b/plugins/horoscope.py index 7145711..e4404cf 100644 --- a/plugins/horoscope.py +++ b/plugins/horoscope.py @@ -32,16 +32,16 @@ def horoscope(inp, db=None, notice=None, nick=None): sign = db.execute("select sign from horoscope where nick=lower(?)", (nick,)).fetchone() if not sign: - notice(horoscope.__doc__) + notice("horoscope -- Get your horoscope") return sign = sign[0] - url = "http://my.horoscope.com/astrology/free-daily-horoscope-%s.html" % sign + url = "http://my.horoscope.com/astrology/free-daily-horoscope-{}.html".format(sign) soup = http.get_soup(url) title = soup.find_all('h1', {'class': 'h1b'})[1] - horoscope = soup.find('div', {'class': 'fontdef1'}) - result = "\x02%s\x02 %s" % (title, horoscope) + horoscope_text = soup.find('div', {'class': 'fontdef1'}) + result = u"\x02%s\x02 %s" % (title, horoscope_text) result = text.strip_html(result) #result = unicode(result, "utf8").replace('flight ','') From 694cbbe81ffe1552232bb81c462622c3093de397 Mon Sep 17 00:00:00 2001 From: xxyy Date: Sat, 15 Feb 2014 00:16:03 +0100 Subject: [PATCH 6/7] Fix xkcd comics made in January throwing an error Previously, any commit written in January would throw an error because the array key for January was falsely '1' instead of the integer 1. This would lead to the moth name not being found and the error occuring. This PR fixes that problem by changing the key of January to the integer 1. **How to reproduce:** Ask your CloudBot to `xkcd Regular Expressions`, an error will be shown in console. --- plugins/xkcd.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/xkcd.py b/plugins/xkcd.py index fd632fb..d7fad59 100644 --- a/plugins/xkcd.py +++ b/plugins/xkcd.py @@ -4,7 +4,7 @@ from util import hook, http xkcd_re = (r'(.*:)//(www.xkcd.com|xkcd.com)(.*)', re.I) -months = {'1': 'January', 2: 'February', 3: 'March', 4: 'April', 5: 'May', 6: 'June', 7: 'July', 8: 'August', +months = {1: 'January', 2: 'February', 3: 'March', 4: 'April', 5: 'May', 6: 'June', 7: 'July', 8: 'August', 9: 'September', 10: 'October', 11: 'November', 12: 'December'} From 160785e92c8fa02ac663edd99eba3e0a83248b49 Mon Sep 17 00:00:00 2001 From: Luke Rogers Date: Sat, 15 Feb 2014 14:19:12 +1300 Subject: [PATCH 7/7] pep --- plugins/googleurlparse.py | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/plugins/googleurlparse.py b/plugins/googleurlparse.py index 099fa4e..cbea897 100644 --- a/plugins/googleurlparse.py +++ b/plugins/googleurlparse.py @@ -2,15 +2,21 @@ from util import hook from urllib import unquote @hook.command(autohelp=False) -def googleurl(inp, db=None, input=None): - """googleurl [nickname] - Converts Google urls (google.com/url) to normal urls where possible, in the specified nickname's last message. If nickname isn't provided, action will be performed on user's last message""" +def googleurl(inp, db=None, nick=None): + """googleurl [nickname] - Converts Google urls (google.com/url) to normal urls + where possible, in the specified nickname's last message. If nickname isn't provided, + action will be performed on user's last message""" if not inp: - inp = input.nick + inp = nick last_message = db.execute("select name, quote from seen_user where name" " like ? and chan = ?", (inp.lower(), input.chan.lower())).fetchone() if last_message: msg = last_message[1] - out = ", ".join([(unquote(a[4:]) if a[:4] == "url=" else "") for a in msg.split("&")]).replace(", ,", "").strip() + out = ", ".join([(unquote(a[4:]) if a[:4] == "url=" else "") for a in msg.split("&")])\ + .replace(", ,", "").strip() return out if out else "No matches in your last message." else: - return "You haven't said anything in this channel yet!" if inp == input.nick else "That user hasn't said anything in this channel yet!" + if inp == nick: + return "You haven't said anything in this channel yet!" + else: + return "That user hasn't said anything in this channel yet!"