<s> import requests <EOL> from hashlib import md5 <EOL> from search_client import * <EOL> from api_credentials import * <EOL> GOOGLE_OLD_API_ENTRY = '<STR_LIT>' <EOL> GOOGLE_OLD_API_FUNC = '<STR_LIT>' <EOL> class GoogleOldAPISearch ( requests . Session , SearchClient ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , async_query = True , timeout = <NUM_LIT> , ** kwargs ) : <EOL> super ( GoogleOldAPISearch , self ) . __init__ ( ) <EOL> if not GOOGLE_OLD_API_KEY : <EOL> raise NoAPICredentials ( '<STR_LIT>' ) <EOL> self . headers . update ( kwargs ) <EOL> self . timeout = timeout <EOL> self . _results_per_req = <NUM_LIT:8> <EOL> self . _supported_sizes_map = { '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' } <EOL> self . _supported_styles_map = { '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' } <EOL> self . async_query = async_query <EOL> def _fetch_results_from_offset ( self , query , result_offset , <EOL> aux_params = { } , headers = { } , <EOL> num_results = - <NUM_LIT:1> ) : <EOL> if num_results == - <NUM_LIT:1> : <EOL> num_results = self . _results_per_req <EOL> try : <EOL> req_result_count = min ( self . _results_per_req , num_results - result_offset ) <EOL> aux_params [ '<STR_LIT:q>' ] = query <EOL> aux_params [ '<STR_LIT:start>' ] = result_offset <EOL> aux_params [ '<STR_LIT>' ] = req_result_count <EOL> resp = self . get ( GOOGLE_OLD_API_ENTRY + GOOGLE_OLD_API_FUNC , <EOL> params = aux_params , headers = headers ) <EOL> result_dict = resp . json ( ) <EOL> return result_dict [ '<STR_LIT>' ] [ '<STR_LIT>' ] [ : ( num_results - result_offset ) ] <EOL> except requests . exceptions . RequestException : <EOL> return [ ] <EOL> def __google_results_to_results ( self , results ) : <EOL> return [ { '<STR_LIT:url>' : item [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : md5 ( item [ '<STR_LIT>' ] ) . hexdigest ( ) , <EOL> '<STR_LIT:title>' : item [ '<STR_LIT>' ] } for item in results ] <EOL> def query ( self , query , size = '<STR_LIT>' , style = '<STR_LIT>' , num_results = <NUM_LIT:64> ) : <EOL> if num_results > <NUM_LIT:64> : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> size = self . _size_to_native_size ( size ) <EOL> style = self . _style_to_native_style ( style ) <EOL> aux_params = { '<STR_LIT:v>' : '<STR_LIT:1.0>' , <EOL> '<STR_LIT:key>' : GOOGLE_OLD_API_KEY } <EOL> if size : <EOL> aux_params [ '<STR_LIT>' ] = size <EOL> if style : <EOL> aux_params [ '<STR_LIT>' ] = style <EOL> results = [ ] <EOL> results = self . _fetch_results ( query , <EOL> num_results , <EOL> aux_params = aux_params ) <EOL> return self . __google_results_to_results ( results ) </s>
