<s> import logging <EOL> import re <EOL> import sys <EOL> import urllib <EOL> import urlparse <EOL> import xml . sax . saxutils <EOL> try : <EOL> from urlparse import parse_qsl <EOL> except : <EOL> from cgi import parse_qsl <EOL> import base . handlers <EOL> LARGE_THUMBNAIL = '<STR_LIT>' <EOL> SMALL_THUMBNAIL = '<STR_LIT>' <EOL> _YFROG_PATH_RE = re . compile ( r'<STR_LIT>' ) <EOL> _INSTAGRAM_PATH_RE = re . compile ( r'<STR_LIT>' ) <EOL> _FLICKR_SHORT_PATH_RE = re . compile ( r'<STR_LIT>' ) <EOL> _FLICKR_LONG_HOSTNAME_RE = re . compile ( r'<STR_LIT>' ) <EOL> _FLICKR_LONG_PATH_RE = re . compile ( r'<STR_LIT>' ) <EOL> _FLICKR_PHOTO_PAGE_PATH_RE = re . compile ( r'<STR_LIT>' ) <EOL> _FLICKR_SHORT_ID_ALPHABET = r'<STR_LIT>' <EOL> _IMGUR_PATH_RE = re . compile ( r'<STR_LIT>' ) <EOL> _IMGUR_GALLERY_PATH_RE = re . compile ( r'<STR_LIT>' ) <EOL> _TWITPIC_PATH_RE = re . compile ( r'<STR_LIT>' ) <EOL> _LOCKERZ_PATH_RE = re . compile ( r'<STR_LIT>' ) <EOL> _IMGLY_FULL_PATH_RE = re . compile ( r'<STR_LIT>' ) <EOL> _IMGLY_SHORT_PATH_RE = re . compile ( r'<STR_LIT>' ) <EOL> _OWLY_PATH_RE = re . compile ( r'<STR_LIT>' ) <EOL> _VINE_PATH_RE = re . compile ( r'<STR_LIT>' ) <EOL> def _get_short_flickr_photo_id ( photo_id ) : <EOL> result = '<STR_LIT>' <EOL> base = len ( _FLICKR_SHORT_ID_ALPHABET ) <EOL> while photo_id : <EOL> div , mod = divmod ( photo_id , base ) <EOL> result = _FLICKR_SHORT_ID_ALPHABET [ mod ] + result <EOL> photo_id = div <EOL> return result <EOL> def get_thumbnail_info ( url , size ) : <EOL> def get_thumb_url_for_short_photo_id ( short_photo_id ) : <EOL> return '<STR_LIT>' % ( <EOL> short_photo_id , size == SMALL_THUMBNAIL and '<STR_LIT:t>' or '<STR_LIT:m>' ) <EOL> def get_youtube_thumb_url ( video_id ) : <EOL> return '<STR_LIT>' % ( <EOL> video_id , need_small and '<STR_LIT:default>' or '<STR_LIT>' ) <EOL> thumb_url = None <EOL> thumb_width = None <EOL> thumb_height = None <EOL> need_small = size == SMALL_THUMBNAIL <EOL> parsed_url = urlparse . urlparse ( url ) <EOL> hostname = parsed_url . netloc <EOL> path = parsed_url . path <EOL> query = dict ( parse_qsl ( parsed_url . query ) ) <EOL> if hostname == '<STR_LIT>' : <EOL> match = _YFROG_PATH_RE . match ( path ) <EOL> if match : <EOL> thumb_url = '<STR_LIT>' % match . group ( <NUM_LIT:1> ) <EOL> if need_small : <EOL> thumb_url += '<STR_LIT>' <EOL> else : <EOL> thumb_url += '<STR_LIT>' <EOL> elif hostname in ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) : <EOL> match = _INSTAGRAM_PATH_RE . match ( path ) <EOL> if match : <EOL> thumb_url = '<STR_LIT>' % match . group ( <NUM_LIT:1> ) <EOL> if need_small : <EOL> thumb_url += '<STR_LIT>' <EOL> thumb_width = <NUM_LIT> <EOL> thumb_height = <NUM_LIT> <EOL> else : <EOL> thumb_width = <NUM_LIT> <EOL> thumb_height = <NUM_LIT> <EOL> elif hostname == '<STR_LIT>' : <EOL> match = _FLICKR_SHORT_PATH_RE . match ( path ) <EOL> if match : <EOL> thumb_url = get_thumb_url_for_short_photo_id ( match . group ( <NUM_LIT:1> ) ) <EOL> elif _FLICKR_LONG_HOSTNAME_RE . match ( hostname ) : <EOL> match = _FLICKR_LONG_PATH_RE . match ( path ) <EOL> if match : <EOL> path_prefix = match . group ( <NUM_LIT:1> ) <EOL> size_suffix = match . group ( <NUM_LIT:2> ) <EOL> extension = match . group ( <NUM_LIT:3> ) <EOL> if size_suffix == '<STR_LIT>' : <EOL> thumb_url = url <EOL> else : <EOL> thumb_url = '<STR_LIT>' % ( parsed_url . scheme , hostname , path_prefix ) <EOL> if need_small : <EOL> thumb_url += '<STR_LIT>' <EOL> thumb_url += extension <EOL> elif hostname == '<STR_LIT>' : <EOL> match = _FLICKR_PHOTO_PAGE_PATH_RE . match ( path ) <EOL> if match : <EOL> photo_id = int ( match . group ( <NUM_LIT:1> ) ) <EOL> short_photo_id = _get_short_flickr_photo_id ( photo_id ) <EOL> thumb_url = get_thumb_url_for_short_photo_id ( short_photo_id ) <EOL> elif hostname . startswith ( '<STR_LIT>' ) and hostname . endswith ( '<STR_LIT>' ) : <EOL> match = _IMGUR_PATH_RE . match ( path ) <EOL> if match : <EOL> thumb_url = '<STR_LIT>' % ( <EOL> parsed_url . scheme , hostname , match . group ( <NUM_LIT:1> ) , <EOL> ( need_small and '<STR_LIT:s>' or '<STR_LIT:l>' ) , match . group ( <NUM_LIT:2> ) ) <EOL> elif hostname == '<STR_LIT>' : <EOL> match = _IMGUR_GALLERY_PATH_RE . match ( path ) <EOL> if match : <EOL> thumb_url = '<STR_LIT>' % ( <EOL> match . group ( <NUM_LIT:2> ) , ( need_small and '<STR_LIT:s>' or '<STR_LIT:l>' ) ) <EOL> elif hostname == '<STR_LIT>' : <EOL> match = _TWITPIC_PATH_RE . match ( path ) <EOL> if match : <EOL> thumb_url = '<STR_LIT>' % ( <EOL> need_small and '<STR_LIT>' or '<STR_LIT>' , <EOL> match . group ( <NUM_LIT:1> ) ) <EOL> if need_small : <EOL> thumb_width = <NUM_LIT> <EOL> thumb_height = <NUM_LIT> <EOL> elif hostname == '<STR_LIT>' : <EOL> match = _LOCKERZ_PATH_RE . match ( path ) <EOL> if match : <EOL> thumb_url = '<STR_LIT>' % ( <EOL> urllib . quote ( url ) , <EOL> need_small and '<STR_LIT>' or '<STR_LIT>' ) <EOL> if need_small : <EOL> thumb_width = <NUM_LIT> <EOL> thumb_height = <NUM_LIT> <EOL> elif hostname == '<STR_LIT>' : <EOL> thumb_url = '<STR_LIT>' % path <EOL> elif hostname == '<STR_LIT>' or hostname == '<STR_LIT>' : <EOL> if path == '<STR_LIT>' and '<STR_LIT:v>' in query : <EOL> thumb_url = get_youtube_thumb_url ( query [ '<STR_LIT:v>' ] ) <EOL> elif hostname == '<STR_LIT>' : <EOL> thumb_url = get_youtube_thumb_url ( path [ <NUM_LIT:1> : ] ) <EOL> elif hostname == '<STR_LIT>' : <EOL> match = _IMGLY_FULL_PATH_RE . match ( path ) <EOL> if match : <EOL> thumb_url = '<STR_LIT>' % ( <EOL> match . group ( <NUM_LIT:1> ) , <EOL> need_small and '<STR_LIT>' or '<STR_LIT>' ) <EOL> else : <EOL> match = _IMGLY_SHORT_PATH_RE . match ( path ) <EOL> if match : <EOL> thumb_url = '<STR_LIT>' % ( <EOL> need_small and '<STR_LIT>' or '<STR_LIT>' , <EOL> match . group ( <NUM_LIT:1> ) ) <EOL> if need_small : <EOL> thumb_width = <NUM_LIT> <EOL> thumb_height = <NUM_LIT> <EOL> elif hostname == '<STR_LIT>' : <EOL> match = _OWLY_PATH_RE . match ( path ) <EOL> if match : <EOL> thumb_url = '<STR_LIT>' % ( <EOL> need_small and '<STR_LIT>' or '<STR_LIT>' , <EOL> match . group ( <NUM_LIT:1> ) ) <EOL> if need_small : <EOL> thumb_width = <NUM_LIT:100> <EOL> thumb_height = <NUM_LIT:100> <EOL> return thumb_url , thumb_width , thumb_height <EOL> def get_iframe_info ( url ) : <EOL> iframe_url = None <EOL> iframe_width = None <EOL> iframe_height = None <EOL> parsed_url = urlparse . urlparse ( url ) <EOL> hostname = parsed_url . netloc <EOL> path = parsed_url . path <EOL> query = dict ( parse_qsl ( parsed_url . query ) ) <EOL> if hostname == '<STR_LIT>' or hostname == '<STR_LIT>' : <EOL> if path == '<STR_LIT>' and '<STR_LIT:v>' in query : <EOL> iframe_url = '<STR_LIT>' % query [ '<STR_LIT:v>' ] <EOL> iframe_width = <NUM_LIT> <EOL> iframe_height = <NUM_LIT> <EOL> elif hostname == '<STR_LIT>' : <EOL> iframe_url = '<STR_LIT>' % path [ <NUM_LIT:1> : ] <EOL> iframe_width = <NUM_LIT> <EOL> iframe_height = <NUM_LIT> <EOL> elif hostname == '<STR_LIT>' : <EOL> iframe_url = '<STR_LIT>' % path [ <NUM_LIT:1> : ] <EOL> iframe_width = <NUM_LIT> <EOL> iframe_height = <NUM_LIT> <EOL> elif hostname == '<STR_LIT>' : <EOL> match = _VINE_PATH_RE . match ( path ) <EOL> if match : <EOL> iframe_url = '<STR_LIT>' % match . group ( <NUM_LIT:1> ) <EOL> iframe_width = <NUM_LIT> <EOL> iframe_height = <NUM_LIT> <EOL> return iframe_url , iframe_width , iframe_height <EOL> class TestHandler ( base . handlers . BaseHandler ) : <EOL> def get ( self ) : <EOL> url = self . request . get ( '<STR_LIT:url>' ) <EOL> self . response . headers [ '<STR_LIT:Content-Type>' ] = '<STR_LIT>' <EOL> def add_field ( label , value ) : <EOL> self . response . out . write ( '<STR_LIT>' % ( label , value ) ) <EOL> iframe_url , iframe_width , iframe_height = get_iframe_info ( url ) <EOL> if iframe_url : <EOL> add_field ( '<STR_LIT>' , iframe_url ) <EOL> add_field ( '<STR_LIT>' , iframe_width ) <EOL> add_field ( '<STR_LIT>' , iframe_height ) <EOL> iframe_attributes = '<STR_LIT>' <EOL> if iframe_width and iframe_height : <EOL> iframe_attributes = '<STR_LIT>' % ( <EOL> iframe_width , iframe_height ) <EOL> add_field ( '<STR_LIT>' , <EOL> '<STR_LIT>' <EOL> % ( xml . sax . saxutils . escape ( iframe_url ) , iframe_attributes ) ) <EOL> def add_thumbnail ( size ) : <EOL> thumb_url , thumb_width , thumb_height = get_thumbnail_info ( url , size ) <EOL> if thumb_url : <EOL> add_field ( '<STR_LIT>' % size , thumb_url ) <EOL> add_field ( '<STR_LIT>' % size , thumb_width ) <EOL> add_field ( '<STR_LIT>' % size , thumb_height ) <EOL> img_attributes = '<STR_LIT>' <EOL> if thumb_width and thumb_height : <EOL> img_attributes = '<STR_LIT>' % ( <EOL> thumb_width , thumb_height ) <EOL> add_field ( '<STR_LIT>' , <EOL> '<STR_LIT>' <EOL> % ( xml . sax . saxutils . escape ( thumb_url ) , img_attributes ) ) <EOL> add_thumbnail ( LARGE_THUMBNAIL ) <EOL> add_thumbnail ( SMALL_THUMBNAIL ) </s>
