shortener.py 820 B

123456789101112131415161718192021222324252627282930
  1. import urllib2
  2. import re
  3. class Shortener:
  4. def __init__(self, events=None, printer_handle=None, bot=None, say=None):
  5. self.events = events
  6. self.printer = printer_handle
  7. self.interests = ['__urls__']
  8. self.bot = bot
  9. self.say = say
  10. self.cmd = None
  11. self.help = "url shortening utility function"
  12. for event in events:
  13. if event._type in self.interests:
  14. # print "DEBUG: registering to ",
  15. # print event
  16. event.subscribe(self)
  17. def handle(self, event):
  18. try:
  19. target = re.search("https?://[\S]+", event.line).group(0)
  20. if len(target) > 60:
  21. url = "http://is.gd/create.php?format=simple&url=" + target
  22. response = urllib2.urlopen(url)
  23. self.printer("PRIVMSG " + event.channel + " :" + response.read() + '\n')
  24. except:
  25. pass