<s> import requests <EOL> class GeolocationExcetion ( Exception ) : <EOL> pass <EOL> def geolocate ( address , bounds = None , country = None , administrative_area = None , sensor = False ) : <EOL> """<STR_LIT>""" <EOL> params = { <EOL> '<STR_LIT:address>' : address , <EOL> '<STR_LIT>' : str ( sensor ) . lower ( ) <EOL> } <EOL> components = [ ] <EOL> if country : <EOL> components . append ( '<STR_LIT>' + country ) <EOL> if administrative_area : <EOL> components . append ( '<STR_LIT>' + administrative_area ) <EOL> if bounds : <EOL> params [ '<STR_LIT>' ] = '<STR_LIT:|>' . join ( [ '<STR_LIT>' . format ( x . latitude , x . longitude ) for x in bounds ] ) <EOL> if components : <EOL> params [ '<STR_LIT>' ] = '<STR_LIT:|>' . join ( components ) <EOL> response = requests . get ( '<STR_LIT>' , params = params ) <EOL> if not response . ok : <EOL> raise GeolocationExcetion ( response . text ) <EOL> data = response . json ( ) <EOL> if data [ '<STR_LIT:status>' ] not in [ '<STR_LIT:OK>' , '<STR_LIT>' ] : <EOL> raise GeolocationExcetion ( data ) <EOL> all_results = data . get ( '<STR_LIT>' , [ ] ) <EOL> for result in all_results : <EOL> coords = result . get ( '<STR_LIT>' , { } ) . get ( '<STR_LIT:location>' ) <EOL> if coords : <EOL> result [ '<STR_LIT>' ] = coords [ '<STR_LIT>' ] <EOL> result [ '<STR_LIT>' ] = coords [ '<STR_LIT>' ] <EOL> return all_results </s>
