<s> from facebookads . exceptions import FacebookBadObjectError <EOL> import hashlib <EOL> import six <EOL> import re <EOL> class CustomAudienceMixin : <EOL> class Schema ( object ) : <EOL> uid = '<STR_LIT>' <EOL> email_hash = '<STR_LIT>' <EOL> phone_hash = '<STR_LIT>' <EOL> mobile_advertiser_id = '<STR_LIT>' <EOL> class MultiKeySchema ( object ) : <EOL> extern_id = '<STR_LIT>' <EOL> email = '<STR_LIT>' <EOL> phone = '<STR_LIT>' <EOL> gen = '<STR_LIT>' <EOL> doby = '<STR_LIT>' <EOL> dobm = '<STR_LIT>' <EOL> dobd = '<STR_LIT>' <EOL> ln = '<STR_LIT>' <EOL> fn = '<STR_LIT>' <EOL> fi = '<STR_LIT>' <EOL> ct = '<STR_LIT>' <EOL> st = '<STR_LIT>' <EOL> zip = '<STR_LIT>' <EOL> madid = '<STR_LIT>' <EOL> country = '<STR_LIT>' <EOL> @ classmethod <EOL> def format_params ( cls , <EOL> schema , <EOL> users , <EOL> is_raw = False , <EOL> app_ids = None , <EOL> pre_hashed = None ) : <EOL> hashed_users = [ ] <EOL> if schema in ( cls . Schema . phone_hash , <EOL> cls . Schema . email_hash , <EOL> cls . Schema . mobile_advertiser_id ) : <EOL> for user in users : <EOL> if schema == cls . Schema . email_hash : <EOL> user = user . strip ( "<STR_LIT>" ) . lower ( ) <EOL> if isinstance ( user , six . text_type ) and not ( pre_hashed ) : <EOL> user = user . encode ( '<STR_LIT:utf8>' ) <EOL> if pre_hashed : <EOL> hashed_users . append ( user ) <EOL> else : <EOL> hashed_users . append ( hashlib . sha256 ( user ) . hexdigest ( ) ) <EOL> elif isinstance ( schema , list ) : <EOL> if not is_raw : <EOL> raise FacebookBadObjectError ( <EOL> "<STR_LIT>" + <EOL> "<STR_LIT>" , <EOL> ) <EOL> for user in users : <EOL> if len ( schema ) != len ( user ) : <EOL> raise FacebookBadObjectError ( <EOL> "<STR_LIT>" + <EOL> "<STR_LIT>" , <EOL> ) <EOL> break <EOL> if pre_hashed : <EOL> hashed_users . append ( user ) <EOL> else : <EOL> counter = <NUM_LIT:0> <EOL> hashed_user = [ ] <EOL> for key in user : <EOL> key = key . strip ( "<STR_LIT>" ) . lower ( ) <EOL> key = cls . normalize_key ( schema [ counter ] , <EOL> str ( key ) ) <EOL> if schema [ counter ] != cls . Schema . MultiKeySchema . extern_id : <EOL> if isinstance ( key , six . text_type ) : <EOL> key = key . encode ( '<STR_LIT:utf8>' ) <EOL> key = hashlib . sha256 ( key ) . hexdigest ( ) <EOL> counter = counter + <NUM_LIT:1> <EOL> hashed_user . append ( key ) <EOL> hashed_users . append ( hashed_user ) <EOL> payload = { <EOL> '<STR_LIT>' : schema , <EOL> '<STR_LIT>' : is_raw , <EOL> '<STR_LIT:data>' : hashed_users or users , <EOL> } <EOL> if schema == cls . Schema . uid : <EOL> if not app_ids : <EOL> raise FacebookBadObjectError ( <EOL> "<STR_LIT>" + cls . Schema . uid + <EOL> "<STR_LIT>" , <EOL> ) <EOL> payload [ '<STR_LIT>' ] = app_ids <EOL> return { <EOL> '<STR_LIT>' : payload , <EOL> } <EOL> @ classmethod <EOL> def normalize_key ( cls , key_name , key_value = None ) : <EOL> """<STR_LIT>""" <EOL> if key_value is None : <EOL> return key_value <EOL> if ( key_name == cls . Schema . MultiKeySchema . extern_id or <EOL> key_name == cls . Schema . MultiKeySchema . email or <EOL> key_name == cls . Schema . MultiKeySchema . madid ) : <EOL> return key_value <EOL> if ( key_name == cls . Schema . MultiKeySchema . phone ) : <EOL> key_value = re . sub ( r'<STR_LIT>' , '<STR_LIT>' , key_value ) <EOL> return key_value <EOL> if ( key_name == cls . Schema . MultiKeySchema . gen ) : <EOL> key_value = key_value . strip ( ) [ : <NUM_LIT:1> ] <EOL> return key_value <EOL> if ( key_name == cls . Schema . MultiKeySchema . doby ) : <EOL> key_value = re . sub ( r'<STR_LIT>' , '<STR_LIT>' , key_value ) <EOL> return key_value <EOL> if ( key_name == cls . Schema . MultiKeySchema . dobm or <EOL> key_name == cls . Schema . MultiKeySchema . dobd ) : <EOL> key_value = re . sub ( r'<STR_LIT>' , '<STR_LIT>' , key_value ) <EOL> if len ( key_value ) == <NUM_LIT:1> : <EOL> key_value = '<STR_LIT:0>' + key_value <EOL> return key_value <EOL> if ( key_name == cls . Schema . MultiKeySchema . ln or <EOL> key_name == cls . Schema . MultiKeySchema . fn or <EOL> key_name == cls . Schema . MultiKeySchema . ct or <EOL> key_name == cls . Schema . MultiKeySchema . fi or <EOL> key_name == cls . Schema . MultiKeySchema . st ) : <EOL> key_value = re . sub ( r'<STR_LIT>' , '<STR_LIT>' , key_value ) <EOL> return key_value <EOL> if ( key_name == cls . Schema . MultiKeySchema . zip ) : <EOL> key_value = re . split ( '<STR_LIT:->' , key_value ) [ <NUM_LIT:0> ] <EOL> return key_value <EOL> if ( key_name == cls . Schema . MultiKeySchema . country ) : <EOL> key_value = re . sub ( r'<STR_LIT>' , '<STR_LIT>' , key_value ) [ : <NUM_LIT:2> ] <EOL> return key_value <EOL> def add_users ( self , <EOL> schema , <EOL> users , <EOL> is_raw = False , <EOL> app_ids = None , <EOL> pre_hashed = None ) : <EOL> """<STR_LIT>""" <EOL> return self . get_api_assured ( ) . call ( <EOL> '<STR_LIT:POST>' , <EOL> ( self . get_id_assured ( ) , '<STR_LIT>' ) , <EOL> params = self . format_params ( schema , <EOL> users , <EOL> is_raw , <EOL> app_ids , <EOL> pre_hashed , <EOL> ) , <EOL> ) <EOL> def remove_users ( self , <EOL> schema , <EOL> users , <EOL> is_raw = False , <EOL> app_ids = None , <EOL> pre_hashed = None ) : <EOL> """<STR_LIT>""" <EOL> return self . get_api_assured ( ) . call ( <EOL> '<STR_LIT>' , <EOL> ( self . get_id_assured ( ) , '<STR_LIT>' ) , <EOL> params = self . format_params ( <EOL> schema , <EOL> users , <EOL> is_raw , <EOL> app_ids , <EOL> pre_hashed , <EOL> ) , <EOL> ) <EOL> def share_audience ( self , account_ids ) : <EOL> """<STR_LIT>""" <EOL> return self . get_api_assured ( ) . call ( <EOL> '<STR_LIT:POST>' , <EOL> ( self . get_id_assured ( ) , '<STR_LIT>' ) , <EOL> params = { '<STR_LIT>' : account_ids } , <EOL> ) <EOL> def unshare_audience ( self , account_ids ) : <EOL> """<STR_LIT>""" <EOL> return self . get_api_assured ( ) . call ( <EOL> '<STR_LIT>' , <EOL> ( self . get_id_assured ( ) , '<STR_LIT>' ) , <EOL> params = { '<STR_LIT>' : account_ids } , <EOL> ) </s>
