<s> """<STR_LIT>""" <EOL> __author__ = '<STR_LIT>' <EOL> import glob <EOL> import operator <EOL> import os <EOL> import os . path <EOL> import re <EOL> import sys <EOL> import time <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> sys . path . append ( '<STR_LIT:..>' ) <EOL> try : <EOL> import third_party <EOL> except ImportError : <EOL> pass <EOL> import httplib2 <EOL> CACHE_DIR = os . getenv ( '<STR_LIT>' ) + '<STR_LIT>' <EOL> CACHE_EXPIRATION = <NUM_LIT> * <NUM_LIT> <EOL> SLEEPY_TIME = <NUM_LIT:15> <EOL> MAX_ATTEMPTS = <NUM_LIT:5> <EOL> NAKED_DOMAINS = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> def FetchUrl ( url , attempts = <NUM_LIT:0> ) : <EOL> attempts += <NUM_LIT:1> <EOL> print >> sys . stderr , "<STR_LIT>" % ( url , attempts ) <EOL> h = httplib2 . Http ( CACHE_DIR , timeout = <NUM_LIT:10> ) <EOL> try : <EOL> resp , content = h . request ( url , '<STR_LIT:GET>' , headers = { '<STR_LIT>' : '<STR_LIT>' % CACHE_EXPIRATION } ) <EOL> except : <EOL> if attempts < MAX_ATTEMPTS : <EOL> print >> sys . stderr , "<STR_LIT>" <EOL> time . sleep ( SLEEPY_TIME ) <EOL> return FetchUrl ( url , attempts = attempts ) <EOL> time . sleep ( SLEEPY_TIME ) <EOL> return content <EOL> def FetchCachedAlexaPage ( domain ) : <EOL> url_path = '<STR_LIT>' % domain <EOL> cache_path = '<STR_LIT>' % ( CACHE_DIR , url_path . replace ( '<STR_LIT:/>' , '<STR_LIT:U+002C>' ) ) <EOL> for file in glob . glob ( "<STR_LIT>" % cache_path ) : <EOL> f = open ( file ) <EOL> return f . read ( ) <EOL> return FetchUrl ( "<STR_LIT>" % url_path ) <EOL> def ParseAlexaSubdomains ( content ) : <EOL> return re . findall ( '<STR_LIT>' , content , re . M | re . S ) <EOL> def GetHostsForDomain ( domain ) : <EOL> content = FetchCachedAlexaPage ( domain ) <EOL> return ParseAlexaSubdomains ( content ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> index = <NUM_LIT:0> <EOL> results = { } <EOL> for domain in sys . stdin : <EOL> index += <NUM_LIT:1> <EOL> domain = domain . rstrip ( ) <EOL> for host , percentage in GetHostsForDomain ( domain ) : <EOL> if host == domain and domain not in NAKED_DOMAINS : <EOL> host = '<STR_LIT:.>' . join ( ( '<STR_LIT>' , domain ) ) <EOL> if percentage == '<STR_LIT>' : <EOL> continue <EOL> score = index / ( float ( percentage ) / <NUM_LIT:100> ) <EOL> if host not in results : <EOL> results [ host ] = score <EOL> print >> sys . stderr , "<STR_LIT>" % ( score , host , percentage ) <EOL> for host , score in sorted ( results . items ( ) , key = operator . itemgetter ( <NUM_LIT:1> ) ) : <EOL> print "<STR_LIT>" % host </s>
