/
fbPushEmails.py
188 lines (166 loc) · 6.73 KB
/
fbPushEmails.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
__author__ = 'assafdekel'
from facebookads.api import FacebookAdsApi
# from facebookads import objects
from facebookads.objects import AdUser, AdAccount, CustomAudience
# import requests
#Initialize a new Session and instanciate an Api object
AUDIENCE_DEFAULT_NAME = "Salesforce Audience"
AUDIENCE_DEFAULT_DESCRIPTION = "Salesforce CRM based audience. Generated by 3Targeting"
my_app_id = '540991286064496'
my_app_secret = '5b4d044338217ad32a29852f194b09ad'
my_access_token = 'CAAHsB1lutXABAFOM3J65M3rC1Rc4pWqnxaIZAbaRpIORGZAhLH3uwArocOGiVFov8qSi6oP1vsksZBRZCIFdLn0JXKTHEKQh7k9DZBh6tu5TVKCSuYqjxpmHxfWcvIZC5TcjWZBM1RRfgFCK7AxLtuEX7qFULqtzcpJk11GGb9ujjNAzF7nQz7qknSucUKZCIPGOLBD2D1m48E0X6qTQafjd' # User access token
def facebookMain(fbCred, emails_list):
act_id = fbCred['fb_user_id']
user_access_token = fbCred['user_token']
# user_access_token = 'CAAHsB1lutXABADNT3ppqATXZBdUwlfgEViIVlaHl5A14CpZBzoCvWi3qycaVdZAtLPnVFH5QY7EKY1UWYuYzvJptzrFaNcE8OXIOOYVo1hX33h8Ml2M9n5yyd7UJ3PHwOtixi6VgJNJY7sZAYY6b4CwqwCwZAjXir8aKAzmu6GMWZAutDKdSQ482SnawqJRIA7XVgR3K7HGIx1ZA4rOEFra'
# Login
FacebookAdsApi.init(my_app_id, my_app_secret, user_access_token)
ad_account = AdAccount(act_id)
### use this to login as yourself (if loged in on this computer)
# me = AdUser(fbid='me')
# my_account = me.get_ad_accounts()[0]
# Push emails to 3Targeting default list
audiences_list = getCustomAudiences(ad_account)
my_audiences = findAudienceByName(AUDIENCE_DEFAULT_NAME, audiences_list)
if my_audiences:
retVal = LoadCustomAudience(my_audiences[0], f=emails_list, datatype='email')
else:
retVal = CreateCustomAudience(ad_account, AUDIENCE_DEFAULT_NAME, description=AUDIENCE_DEFAULT_DESCRIPTION, f=emails_list, datatype='email')
return retVal
# finds audience with specific name
def findAudienceByName(name, audiences_list):
result = [audience for audience in audiences_list if name in audience['name']]
return result
# returns a list of the custom audiences in an account
def getCustomAudiences(my_account):
ad_account = AdAccount("{AD_ACCOUNT_ID}".format(AD_ACCOUNT_ID = my_account['id']))
custom_audiences = ad_account.get_custom_audiences(fields=[CustomAudience.Field.name])
# audiences = my_account.get_custom_audiences(fields=[
# CustomAudience.Field.name,
# CustomAudience.Field.description])
return custom_audiences
# deletes audience from account
def DeleteCustomAudience(audience_id):
audience = CustomAudience(audience_id)
print('Deleting audience id ' + audience[CustomAudience.Field.id])
return audience.remote_delete()
# just creates audience
def CreateCustomAudience(ad_account, name, description=None, f=None, datatype='email'):
audience = CustomAudience(parent_id=ad_account.get_id_assured())
audience.update({
CustomAudience.Field.name: name,
CustomAudience.Field.subtype: CustomAudience.Subtype.custom,
})
if description:
audience.update({CustomAudience.Field.description: description})
audience.remote_create()
print('Created custom audience id ' + audience[CustomAudience.Field.id])
if f and datatype:
return LoadCustomAudience(audience, f, datatype)
return "No data was uploaded to audience. Only created custom audience id ' + audience[CustomAudience.Field.id]"
# add data to audience
def LoadCustomAudience(audience, f, datatype, schema=None, app_ids=None):
# File format is one type per file (ie email), and one entry per line
if datatype == 'email':
schema = CustomAudience.Schema.email_hash
elif datatype == 'phone':
schema = CustomAudience.Schema.phone_hash
elif datatype == 'mobile_id':
schema = CustomAudience.Schema.mobile_advertiser_id
elif datatype == 'uid':
schema = CustomAudience.Schema.uid
else:
return "[ERROR] invalid datatype " + datatype
#sys.exit("[ERROR] invalid datatype " + datatype)
print('Adding users to audience using ' + str(schema))
data = [line.strip().lower() for line in f]
r = audience.add_users(schema, data, app_ids=app_ids)
return r._body
#pp(r._body)
# def sendHashedEmails(emailsList, hashed_emails):
# # Init
# FacebookAdsApi.init(my_app_id, my_app_secret, my_access_token)
#
# me = AdUser(fbid='me')
# my_account = me.get_ad_accounts()[0]
#
# # Get custom audiences
# custom_audiences = getCustomAudiences(my_account)
#
# audience_id = custom_audiences[0]['id']
# audience = CustomAudience(audience_id)
# audience.add_users(CustomAudience.Schema.email_hash, emailsList)
# audience.remote_update()
#
# my_custom_audience = custom_audiences[0]
# #my_custom_audience[objects.CustomAudience.Field.name]
# my_custom_audience.remoteupdate()
#
#
# # Add emails to audience
# URL = 'https://graph.facebook.com/v2.5/{CUSTOM_AUDIENCE_ID}/users?access_token={ACCESS_TOKEN}'.format(CUSTOM_AUDIENCE_ID = audience_id, ACCESS_TOKEN = my_access_token)
# payload = {
# 'schema' : 'EMAIL_SHA256',
# 'data' : hashed_emails
# }
# r = requests.post(URL, data=payload)
# return r
#
# # for custom_audience in custom_audiences:
# # print(custom_audience[CustomAudience.Field.name])
#
#
# # # Create new audience
# # import requests
# # url = "https://graph.facebook.com/v2.5/act_{AD_ACCOUNT_ID}/customaudiences?access_token={ACCESS_TOKEN}".format(AD_ACCOUNT_ID = my_account['account_id'], ACCESS_TOKEN = my_access_token)
# # r = requests.get(url)
# # r = r.json()
# #
# # print r
#
# # def createNewAudience(audience_name, description, account_id, access_token):
# # # Create new custom audience on facebook ads account
# # URL = 'https://graph.facebook.com/v2.5/act_{AD_ACCOUNT_ID}/customaudiences'.format(AD_ACCOUNT_ID = account_id)
# # payload = {
# # 'name' : audience_name,
# # 'subtype' : 'CUSTOM',
# # 'description' : description, # example: 'People who bought from my website'
# # 'access_token' : access_token
# # }
# #
# # session = requests.session()
# # r = requests.post(URL, data=payload)
#
# """ get emails, normalize, hash, get audinces, push to audience """
#
#
# from facebookads import FacebookAdsApi
# from facebookads.objects import (
# AdAccount,
# CustomAudience,
# )
#
# import json
# import os
# import pprint
# import sys
#
# pp = pprint.pprint
#
# this_dir = os.path.dirname(__file__)
# config_filename = os.path.join(this_dir, 'config.json')
#
# ### Setup session and api objects
# config_file = open(config_filename)
# config = json.load(config_file)
# config_file.close()
#
# auth_info = (
# config['app_id'],
# config['app_secret'],
# config['access_token'])
#
# FacebookAdsApi.init(*auth_info)
#
# ### Get account from config file
# my_account = AdAccount(config['act_id'])