From 92ea836c509c26ab00bc174a1a8029ff745556b8 Mon Sep 17 00:00:00 2001 From: "sekkusu@server.danieljennings.net" Date: Fri, 22 May 2009 00:12:35 +0000 Subject: [PATCH] Adding TinyUrl parser thingie. --- bot.py | 4 ++-- plugins/tinyurl.py | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 2 deletions(-) create mode 100644 plugins/tinyurl.py diff --git a/bot.py b/bot.py index b9de4cc..55c2f23 100755 --- a/bot.py +++ b/bot.py @@ -1,8 +1,8 @@ #!/usr/bin/python network = "irc.synirc.net" -nick = "skybot" -channel = "#cobol" +nick = "UggBot" +channel = "#uggbot" import sys import os diff --git a/plugins/tinyurl.py b/plugins/tinyurl.py new file mode 100644 index 0000000..c62403d --- /dev/null +++ b/plugins/tinyurl.py @@ -0,0 +1,35 @@ +import socket +import re +import locale + +import hook + +locale.setlocale(locale.LC_ALL, "") + +def recv_basic(the_socket): + total_data=[] + while True: + data = the_socket.recv(8192) + if not data: break + total_data.append(data) + return ''.join(total_data) + + +def tinyurlparse(url): + id = url[(url.rfind("/",8)+1):] + ts = socket.socket() + ts.connect(("tinyurl.com", 80)) + ts.send("GET /redirect.php?num=%s HTTP/1.1\r\n" % id) + ts.send("Host: tinyurl.com\r\n\r\n"); + tresult = recv_basic(ts); + for tline in tresult.split("\n"): + if tline[:10] == "Location: ": + return tline[10:] + +tinyurl_re = re.compile(r'http://(www\.)?tinyurl.com/([A-Za-z0-9\-]+)', flags=re.IGNORECASE) + +@hook.command(hook=r'(.*)', prefix=False) +def tinyurl(inp): + tumatch = tinyurl_re.search(inp) + if (tumatch is not None): + return tinyurlparse(tumatch.group())