<s> """<STR_LIT>""" <EOL> import sys <EOL> import webbrowser <EOL> import argparse <EOL> import duckduckgo <EOL> def main ( ) : <EOL> """<STR_LIT>""" <EOL> '<STR_LIT>' <EOL> parser = argparse . ArgumentParser ( <EOL> description = '<STR_LIT>' <EOL> ) <EOL> parser . add_argument ( '<STR_LIT>' , nargs = '<STR_LIT:*>' , help = '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , '<STR_LIT>' , action = '<STR_LIT:store_true>' , <EOL> help = '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , '<STR_LIT>' , action = '<STR_LIT:store_true>' , <EOL> help = '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , '<STR_LIT>' , action = '<STR_LIT:store_true>' , <EOL> help = '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , '<STR_LIT>' , action = '<STR_LIT:store_true>' , <EOL> help = '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , '<STR_LIT>' , action = '<STR_LIT:store_true>' , <EOL> help = '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , '<STR_LIT>' , action = '<STR_LIT:store_true>' , <EOL> help = '<STR_LIT>' ) <EOL> args = parser . parse_args ( ) <EOL> '<STR_LIT>' <EOL> if args . query : <EOL> queries = [ '<STR_LIT:U+0020>' . join ( args . query ) ] <EOL> elif not sys . stdin . isatty ( ) : <EOL> queries = sys . stdin . read ( ) . splitlines ( ) <EOL> else : <EOL> parser . print_help ( ) <EOL> return <EOL> '<STR_LIT>' <EOL> prefix = '<STR_LIT>' if args . search else '<STR_LIT:!>' if args . bang else '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> for query in queries : <EOL> '<STR_LIT>' <EOL> query = prefix + query <EOL> '<STR_LIT>' <EOL> results = duckduckgo . search ( query ) <EOL> '<STR_LIT>' <EOL> if not results : <EOL> continue <EOL> '<STR_LIT>' <EOL> if args . json : <EOL> print_result ( results . json ) <EOL> continue <EOL> '<STR_LIT>' <EOL> results_priority = get_results_priority ( args ) <EOL> '<STR_LIT>' <EOL> var = get_text_or_url ( args ) <EOL> '<STR_LIT>' <EOL> action = get_action ( args ) <EOL> '<STR_LIT>' <EOL> failed_to_find_answer = True <EOL> for r in results_priority : <EOL> result = getattr ( getattr ( results , r ) , var ) <EOL> if result : <EOL> action ( result ) <EOL> failed_to_find_answer = False <EOL> break <EOL> '<STR_LIT>' <EOL> if failed_to_find_answer : <EOL> if results . type == '<STR_LIT>' : <EOL> print '<STR_LIT>' <EOL> else : <EOL> print '<STR_LIT>' <EOL> def get_results_priority ( args ) : <EOL> """<STR_LIT>""" <EOL> redirect_mode = args . bang or args . search or args . lucky <EOL> if redirect_mode : <EOL> results_priority = [ '<STR_LIT>' , '<STR_LIT:result>' , '<STR_LIT>' ] <EOL> else : <EOL> results_priority = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:result>' ] <EOL> insert_pos = <NUM_LIT:0> if args . define else len ( results_priority ) <EOL> results_priority . insert ( insert_pos , '<STR_LIT>' ) <EOL> return results_priority <EOL> def get_action ( args ) : <EOL> """<STR_LIT>""" <EOL> redirect_mode = args . bang or args . search or args . lucky <EOL> if redirect_mode and not args . url : <EOL> return webbrowser . open_new_tab <EOL> else : <EOL> return print_result <EOL> def get_text_or_url ( args ) : <EOL> """<STR_LIT>""" <EOL> redirect_mode = args . bang or args . search or args . lucky <EOL> if redirect_mode or args . url : <EOL> return '<STR_LIT:url>' <EOL> else : <EOL> return '<STR_LIT:text>' <EOL> def print_result ( result ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> print result <EOL> except UnicodeEncodeError : <EOL> if sys . stdout . encoding : <EOL> print result . encode ( sys . stdout . encoding , '<STR_LIT:replace>' ) <EOL> else : <EOL> print result . encode ( '<STR_LIT:utf8>' ) <EOL> except : <EOL> print "<STR_LIT>" <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> main ( ) </s>
