<s> import logging <EOL> import simplejson <EOL> import six <EOL> from . import filter <EOL> from . import magicnumbers <EOL> from . import util <EOL> from . profile import Profile <EOL> from . session import Session <EOL> log = logging . getLogger ( __name__ ) <EOL> search_filters = filter . Filters ( strict = False ) <EOL> def SearchFetchable ( session = None , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> session = session or Session . login ( ) <EOL> return util . Fetchable ( <EOL> SearchManager ( <EOL> SearchJSONFetcher ( session , ** kwargs ) , <EOL> ProfileBuilder ( session ) <EOL> ) <EOL> ) <EOL> class SearchManager ( object ) : <EOL> def __init__ ( self , search_fetchable , profile_builder ) : <EOL> self . _search_fetchable = search_fetchable <EOL> self . _profile_builder = profile_builder <EOL> self . _last_after = None <EOL> def fetch ( self , count = <NUM_LIT> ) : <EOL> last_last_after = object ( ) <EOL> while last_last_after != self . _last_after : <EOL> last_last_after = self . _last_after <EOL> for profile in self . fetch_once ( count = count ) : <EOL> yield profile <EOL> def fetch_once ( self , count = <NUM_LIT> ) : <EOL> response = self . _search_fetchable . fetch ( <EOL> after = self . _last_after , count = count <EOL> ) <EOL> try : <EOL> self . _last_after = response [ '<STR_LIT>' ] [ '<STR_LIT>' ] [ '<STR_LIT>' ] <EOL> except KeyError : <EOL> log . warning ( simplejson . dumps ( <EOL> { <EOL> '<STR_LIT>' : "<STR_LIT>" , <EOL> '<STR_LIT>' : response <EOL> } <EOL> ) ) <EOL> for profile in self . _profile_builder ( response ) : <EOL> yield profile <EOL> class SearchJSONFetcher ( object ) : <EOL> search_uri = '<STR_LIT>' <EOL> default_headers = { <EOL> '<STR_LIT:Content-Type>' : '<STR_LIT:application/json>' <EOL> } <EOL> def __init__ ( self , session = None , ** options ) : <EOL> self . _session = session or Session . login ( ) <EOL> self . _options = options <EOL> self . _parameters = search_filters . build ( session = self . _session , ** options ) <EOL> def _request_params ( self , after = None , count = None ) : <EOL> return { <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : self . _session . access_token <EOL> } , <EOL> '<STR_LIT>' : self . default_headers , <EOL> '<STR_LIT:data>' : simplejson . dumps ( self . _post_body ( after , count ) ) , <EOL> '<STR_LIT:path>' : self . search_uri <EOL> } <EOL> def _post_body ( self , after = None , count = None ) : <EOL> search_parameters = { <EOL> '<STR_LIT>' : after , <EOL> '<STR_LIT>' : count <EOL> } <EOL> search_parameters . update ( self . _parameters ) <EOL> return search_parameters <EOL> def fetch ( self , after = None , count = <NUM_LIT> ) : <EOL> request_parameters = self . _request_params ( after = after , count = count ) <EOL> log . info ( simplejson . dumps ( request_parameters ) ) <EOL> response = self . _session . okc_post ( ** request_parameters ) <EOL> try : <EOL> search_json = response . json ( ) <EOL> except : <EOL> log . warning ( simplejson . dumps ( { '<STR_LIT>' : response . content } ) ) <EOL> raise <EOL> return search_json <EOL> class ProfileBuilder ( object ) : <EOL> def __init__ ( self , session ) : <EOL> self . _session = session <EOL> def __call__ ( self , response_dictionary ) : <EOL> try : <EOL> profile_infos = response_dictionary [ '<STR_LIT:data>' ] <EOL> except KeyError : <EOL> log . warning ( simplejson . dumps ( <EOL> { <EOL> '<STR_LIT>' : "<STR_LIT>" , <EOL> '<STR_LIT>' : response_dictionary <EOL> } <EOL> ) ) <EOL> else : <EOL> for profile_info in profile_infos : <EOL> yield Profile ( self . _session , profile_info [ "<STR_LIT:username>" ] ) <EOL> class GentationFilter ( search_filters . filter_class ) : <EOL> def transform ( gentation ) : <EOL> if isinstance ( gentation , six . string_types ) : <EOL> gentations = [ gentation ] <EOL> else : <EOL> gentations = gentation <EOL> return [ <EOL> magicnumbers . gentation_to_number . get ( <EOL> a_gentation . strip ( ) . lower ( ) , gentation <EOL> ) <EOL> for a_gentation in gentations <EOL> ] <EOL> descriptions = "<STR_LIT>" <EOL> types = list <EOL> acceptable_values = magicnumbers . gentation_to_number . keys ( ) <EOL> class MinimumAgeFilter ( search_filters . filter_class ) : <EOL> keys = '<STR_LIT>' <EOL> descriptions = "<STR_LIT>" <EOL> types = int <EOL> class MaximumAgeFilter ( search_filters . filter_class ) : <EOL> keys = '<STR_LIT>' <EOL> descriptions = "<STR_LIT>" <EOL> types = int <EOL> class RadiusFilter ( search_filters . filter_class ) : <EOL> output_key = "<STR_LIT>" <EOL> descriptions = "<STR_LIT>" <EOL> types = "<STR_LIT>" <EOL> def transform ( radius ) : <EOL> return radius <EOL> class LocationActivationFilter ( search_filters . filter_class ) : <EOL> output_key = "<STR_LIT>" <EOL> types = ( int , type ( None ) ) <EOL> def transform ( radius ) : <EOL> return <NUM_LIT:1> if radius is None else <NUM_LIT:0> <EOL> class LocIdFilter ( search_filters . filter_class ) : <EOL> types = int <EOL> def transform ( locid ) : <EOL> return locid <EOL> class LocationFilter ( search_filters . filter_class ) : <EOL> output_key = "<STR_LIT>" <EOL> descriptions = ( <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" <EOL> ) <EOL> def transform ( location , location_cache ) : <EOL> return location_cache . get_locid ( location ) <EOL> def search ( session = None , count = <NUM_LIT:1> , ** kwargs ) : <EOL> return SearchFetchable ( session , count = count , ** kwargs ) [ : count ] <EOL> search_filters . add_to_docstring_of ( SearchFetchable ) </s>
