mς
`KoGc           @   sΰ   d  k  Z  d  k Z d  k Z d  k Z d  k Z d  k Z d  k Z d  k Z d d g Z d e i	 f d     YZ
 e
   e _ d  d  d  d  d  Z d   Z d   Z d   Z d	   Z d
   Z d   Z e d j o e   n d  S(   Ns   http://s   https://t   AppURLopenerc           B   s   t  Z d Z RS(   Ns   Av_IRC_Bot/1.0(   t   __name__t
   __module__t   version(    (    (    t=   /home/avleen/devel/python/googlebot2/modules/googlebot_url.pyR       s   c   
      C   sά   t  i d  } t  i d |  } t |  } t |  } t
 i   } | o t |  }	 n d  }	 |	 o | o d |	 | f } nB |	 o d |	 } n- | o | o d | | f } n d | } t
 i | | |  d  S(   Nt   commons   %s : %ss   %s(   t   impt   find_modulet   module_locationt   load_modulet   modulet
   ExtractURLt   msgt   urlt   GetPageTitlet   titleR   t   SetupDBt   cursort   MatchURLt   resultt   Nonet   DeliverMessaget   irct   channel(
   t   keywordR   R   R   R   R   R   R
   R   R   (    (    R   t   Gather   s     
c         C   s   t  i   } d |  } | i |  t | i  } | d j o | i	   } | d } n' d |  } | i |  | i i   } | S(   Ns+   SELECT id FROM urlredirect WHERE url = "%s"i    s+   INSERT INTO urlredirect (url) VALUES ("%s")(   R   R   R   R   t	   selectsqlt   executet   intt   rowcountt   numrowst   fetchonet   rowt   short_url_idt	   insertsqlt
   connectiont	   insert_id(   R   R   R   R"   R   R!   R    (    (    R   t   Shorten(   s    

c         C   sk   t  i   } d |  } | i |  t | i  } | d j o$ | i	   } | d i   } | Sn d  Sd  S(   Ns+   SELECT url FROM urlredirect WHERE id = "%s"i    (   R   R   R   t   idR   R   R   R   R   R   R    t   tostringR   R   (   R&   R   R   R   R   R    (    (    R   t   Expand7   s    
c         C   s?   y t  i d |   i d  } Wn t j
 o d  } n X| S(   Ns   .*(http?\S+)i   (   t   ret   matchR   t   groupR   t   AttributeErrorR   (   R   R   (    (    R   R   D   s
    c         C   sλ   t  i    } | i t d   | i d d  } y# t i d | |   i	 d  } Wn t j
 o n3 Xt |  } | d  j o
 d } n d | } | St |   d j o5 t |   } | p d	 |  } qη d
 | | f } n d  S| S(   Ns   config/url_shorten.confR   t   url_paths   .*%s([\d]+)i   s   No such shortened URL!s   URL expanded: %si   s   Unable to open URL: %ss   URL shortened: %s%s(   t   ConfigParsert
   url_configt   readfpt   opent   getR-   R)   R*   R   R+   R&   R,   R(   t   long_urlR   t   long_msgt   lenR%   t	   short_urlt	   short_msg(   R   R4   R7   R3   R/   R6   R-   R&   (    (    R   R   K   s&    #

c         C   sγ   y t  i |   i d  } Wn t j
 o d	 Sn Xd } t i	 | t i
 t i B } y8 d | i |  i d  } d i t i |   } Wn t j
 o d	 } n X| o, d | j p d | j o
 d } n | Sn d	 S(
   sa   Get the <title> of the submitted web page

  Args:
    url: str
  Returns:
    title_text: str
  i (  s   .*<title>(.*?)</title>s   %si   t    t   403t   404s   No titleN(   t   urllibt   urlopenR   t   readt   htmlt   IOErrorR   t   patternR)   t   compilet   DOTALLt
   IGNORECASEt   title_pR*   R+   t
   title_textt   joint   stringt   splitfieldsR,   (   R   R@   R>   RE   RD   (    (    R   R   h   s      	
c          C   sy   y t  i d } Wn  t j
 o t  i d  n Xy t  i d }  Wn  t j
 o t  i d  n Xt | |   d  S(   Ni   i   (   t   syst   argvR   t
   IndexErrort   exitt   argumentR   (   RM   R   (    (    R   t   main   s    t   __main__(   R   R.   t
   HTMLParserR   R)   RG   RI   R;   t   module_find_triggerst   FancyURLopenerR    t
   _urlopenerR   R   R%   R(   R   R   R   RN   R   (   RN   RG   RQ   R)   R   R   R%   R;   R   RI   R   RP   R   R    R   R(   R.   (    (    R   t   ?   s&   														
