-
Notifications
You must be signed in to change notification settings - Fork 0
/
Audience_tools.py
174 lines (116 loc) · 5.42 KB
/
Audience_tools.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
import pandas as pd
import os
from facebookads import FacebookAdsApi
from facebookads.adobjects.customaudience import CustomAudience
from facebookads.adobjects.adaccount import AdAccount
from facebookads.adobjects.adset import AdSet
import logging
import hashlib
logger = logging.getLogger(__name__)
class Audiences():
"""
A class for the manipulation of custom audiences for facebook
"""
def __init__(self,account_id):
self.account_id = account_id
try:
app_id = os.environ["APPID"]
app_secret = os.environ['APPSECRET']
access_token = os.environ['ACCESSTOKEN']
FacebookAdsApi.init(app_id, app_secret, access_token)
logger.info('FB INIT')
except KeyError as ke:
logger.error('You need to set the following environ variables:\n'
'APPID, APPSECRET, ACCESSTOKEN {}'.format(ke)
)
except ValueError as ve:
logger.error(ve)
def hashing(self,data):
"""
Hashing data for further digesting lateron
"""
return [hashlib.sha224(k.encode('utf-8')).hexdigest() for k in data]
def creating_custom_audience(self, **kwarg):
try:
audience = CustomAudience(parent_id=kwarg['account_id'])
audience[CustomAudience.Field.subtype] = CustomAudience.Subtype.custom
audience[CustomAudience.Field.name] = kwarg['ca_name']
audience[CustomAudience.Field.description] = kwarg['ca_description']
audience_id = audience.remote_create()
except KeyError as ke:
logger.error('You need to supply the following keyword variables\n'
'account_id,ca_name,ca_descriptio {}'.format(ke))
return
return audience_id
def add_users(self, users, audience_id):
chunks = []
for i in range(0,len(users),3000):
chunks.append(users[i:i+3000])
audience = CustomAudience(audience_id['id'])
for group in chunks:
audience.add_users(CustomAudience.Schema.email_hash, group)
return
def add_users_phone(self, users, audience_id):
chunks = []
for i in range(0,len(users),500):
chunks.append(users[i:i+500])
audience = CustomAudience(audience_id['id'])
for group in chunks:
audience.add_users(CustomAudience.Schema.phone_hash, group)
return
def creating_look_alike(self,**kwarg):
try:
audience = CustomAudience(parent_id=kwarg['account_id'])
audience[CustomAudience.Field.subtype] = CustomAudience.Subtype.lookalike
audience[CustomAudience.Field.name] = kwarg['ca_name']
audience[CustomAudience.Field.description] = kwarg['ca_description']
audience['origin_audience_id'] = kwarg['oa_id']
audience[CustomAudience.Field.lookalike_spec] = {'type':'similarity','country':kwarg['country']}
audience_id = audience.remote_create()
except KeyError as ke:
logger.error('You need to supply the following keyword variables\n'
'account_id,ca_name,ca_description,ca_id,country {}'.format(ke))
return
return audence_id
def sharing_audience(self, **kwarg):
audience = CustomAudience(parent_id = kwarg['account_id'])
audience.share_audience(account_ids)
return
def custom_audiences_by_account(self, **kwarg):
audience = CustomAudience(parent_id = kwarg['account_id'])
return audience
def read_custom_audiences_by_account(self, **kwarg):
account = AdAccount(kwarg['account_id'])
ca = account.get_custom_audiences(fields =[CustomAudience.Field.name])
return ca
def match_custom_audience(self, **kwargs):
fb_api_init()
out = []
words = [x.lower() for x in kwargs['words']]
account = AdAccount(kwargs['account_id'])
ca = account.get_custom_audiences(fields =[CustomAudience.Field.name])
for audience in ca:
if any(word in [x.lower() for x in audience['name'].split(' ')] for word in words):
out.append(audience)
return out
if __name__ == '__main__':
#data = pd.read_csv('/Users/aaronmeagher/Work/Facebook_experiments/Paddy_experiments/alpha_websummit/ad_input_data/tito.csv')
#temp = data['Ticket Email']
#temp = temp.append(data['Order Email'])
#temp.dropna(inplace = True)
#users = temp.unique()
fb_api_init()
#audience_id = creating_custom_audience(account_id ='act_851394958259883', ca_name = 'Tito_sep_15',\
# ca_description = 'The export from DK from Tito in sep 2015 ' )
# Experiments = read_custom_audiences_by_account(account_id = 'act_872554132810632')
#WS = read_custom_audiences_by_account(account_id = 'act_54708236')
#add_users(users,audience_id)
Test = match_custom_audience(account_id = 'act_851394958259883',
words = ['thank', 'collision'])
# look_alike = creating_look_alike(account_id ='act_851394958259883', ca_name = 'Look_alike_audience_api_test', \
# ca_description = 'This is a test of the look-alike audience creation tool ', oa_id = audience_id['id'],\
# country='US')
# audiences = []
# while Experiments.load_next_page() == True:
# print(Experiments)
# audiences.append(Experiments)