import urllib2
import logging
import re
import HTMLParser

from httplib import HTTPException
from lala.util import regex, msg

_regex = re.compile("(https?://.+)\s?")
_ua = "Mozilla/5.0 (X11; Linux x86_64; rv:2.0b8) Gecko/20100101 Firefox/4.0b8"

def unescape(s):
    p = HTMLParser.HTMLParser()
    return p.unescape(s)

@regex(_regex)
def title(user, channel, text, match_obj):
    url = match_obj.groups()[0]
    req = urllib2.Request(url)
    req.add_header("User-Agent", _ua)
    try:
        content = urllib2.urlopen(req).read()
    except (urllib2.URLError, HTTPException), e:
        logging.debug("%s - %s" % (e, url))
        return

    beg = content.find("<title>")
    if beg != -1:
        title = content[beg+7:content.find("</title>")].replace("\n","")
        try:
            title = unescape(title)
        except HTMLParser.HTMLParseError, e:
            logging.info("%s -  %s" % (e.msg, url))
        msg(channel, "Title: %s" % unicode(title, "utf-8"))

@@ -61,69 +61,74 @@
 rom 
-httplib import HTTPException%0Afrom lala.util import regex, msg
+lala.util import regex, msg%0Afrom twisted.web.client import getPage
 %0A%0A_r
@@ -418,237 +418,35 @@
     
-req = urllib2.Request(url)%0A    req.add_header(%22User-Agent%22, _ua)%0A    try:%0A        content = urllib2.urlopen(req).read()%0A    except (urllib2.URLError, HTTPException), e:%0A        logging.debug(%22%25s - %25s%22 %25 (e, url))%0A        return%0A%0A
+def callback(content):%0A    
     
@@ -475,16 +475,20 @@
 itle%3E%22)%0A
+    
     if b
@@ -497,16 +497,20 @@
  != -1:%0A
+    
         
@@ -586,13 +586,21 @@
     
+  
+  
 try:%0A
+    
     
@@ -639,16 +639,20 @@
         
+ 
+   
 except H
@@ -692,16 +692,20 @@
         
+   
+ 
 logging.
@@ -736,16 +736,20 @@
 , url))%0A
+    
         
@@ -800,8 +800,185 @@
 tf-8%22))%0A
+%0A    def errback(error):%0A        msg(channel, %22Sorry, I couldn't get the title for %25s%22 %25 url)%0A        logging.info(error)%0A%0A    getPage(str(url)).addCallbacks(callback, errback)%0A

update the httptitle plugin to use twisted