-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsearch.py
129 lines (112 loc) · 3.7 KB
/
search.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
import requests
import csv
import json
import argparse
import sys
import os
import dateparser
from urllib.parse import urlencode
parser = argparse.ArgumentParser(prog='FB Ad Archive API')
parser.add_argument('-p', '--page')
parser.add_argument('-d', '--date-limit')
parser.add_argument('-t', '--token')
parser.add_argument('-b', '--byline')
parser.add_argument('query')
args = parser.parse_args()
if not args.query:
sys.exit("A query term is required")
api_token = args.token or os.environ['FB_API_TOKEN']
if not api_token:
sys.exit("A Facebook API token must be set via --token or the FB_API_TOKEN environment variable")
request_args = {
'ad_reached_countries': "['US']",
'access_token': api_token,
'ad_type': 'POLITICAL_AND_ISSUE_ADS',
'ad_active_status': 'ALL',
'impression_condition': 'HAS_IMPRESSIONS_LIFETIME',
'search_terms': f"'{args.query}'",
'limit': 500,
'fields': 'ad_creation_time,ad_creative_body,ad_creative_link_caption,ad_creative_link_description,ad_creative_link_title,ad_delivery_start_time,ad_delivery_stop_time,ad_snapshot_url,currency,demographic_distribution,funding_entity,impressions,page_id,page_name,region_distribution,spend'
}
if args.page:
request_args['search_page_ids'] = args.page
if args.byline:
request_args['byline'] = args.byline
if args.date_limit:
print(args.date_limit)
datelimit = dateparser.parse(f"{args.date_limit} +0000")
else:
datelimit = dateparser.parse("1970-01-01 00:00:00 +0000") # Arbitrarily early date for when there's no limit
outfields = [
'ad_creation_time',
'ad_creative_body',
'ad_creative_link_caption',
'ad_creative_link_description',
'ad_creative_link_title',
'ad_delivery_start_time',
'ad_delivery_stop_time',
'ad_snapshot_url',
'currency',
'top_demographic',
'top_demographic_pct',
'funding_entity',
'impressions.lower_bound',
'impressions.upper_bound',
'page_id',
'page_name',
'top_region',
'top_region_pct',
'spend.lower_bound',
'spend.upper_bound'
]
out = csv.DictWriter(open(f"searches/{ args.query.replace(' ', '+') }_{ args.page or 'all' }.csv", 'w'), fieldnames=outfields)
out.writeheader()
result = { 'paging': { 'next': 'https://graph.facebook.com/v4.0/ads_archive?'+urlencode(request_args) } }
count = 0
while result.get('paging') and result.get('paging').get('next'):
count += 1
print(f"Loading page {count} of results...")
result = requests.get(result['paging']['next']).json()
if not 'data' in result:
print("Error in result")
print(result)
break
backup = open(f"backups/{ args.query.replace(' ', '+') }_{ args.page or 'all' }_{ count }.json","w")
backup.write(json.dumps(result))
backup.close()
for ad in result['data']:
if datelimit > dateparser.parse(ad['ad_delivery_start_time']):
print(f"Halting query at date {ad['ad_delivery_start_time']}...")
result = {}
break
obj = {}
tops = {}
for k in outfields:
if ad.get(k):
obj[k] = ad.get(k)
elif '.' in k:
k2 = k.split('.')
if ad.get(k2[0]):
obj[ k ] = ad.get(k2[0]).get(k2[1])
else:
obj[ k ] = ''
elif 'top_' in k:
pass # we'll do this manually
else:
obj[k] = ''
for d in ['region', 'demographic']:
if ad.get(d+'_distribution'):
top = max(ad.get(d+'_distribution'), key=lambda k: float(k['percentage']))
if top:
obj['top_'+d+'_pct'] = top['percentage']
if d == 'demographic':
obj['top_'+d] = top['age'] + ' ' + top['gender']
else:
obj['top_'+d] = top[d]
else:
obj['top_'+d] = ''
obj['top_'+d+'_pct'] = ''
else:
obj['top_'+d] = ''
obj['top_'+d+'_pct'] = ''
out.writerow(obj)