-
Notifications
You must be signed in to change notification settings - Fork 0
/
extract.py
executable file
·294 lines (253 loc) · 8.66 KB
/
extract.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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
#!/usr/bin/env python3
import os
import re
import json
import PyPDF2
import logging
import pytesseract
from glob import glob
from PIL import Image, ImageChops
from dateutil.parser import parse as parse_date
def main():
logging.basicConfig(
filename='extract.log',
level=logging.INFO,
format='%(asctime)s - %(levelname)s - %(message)s'
)
for pdf in glob('data/*/*.pdf'):
extract_images(pdf)
for png in glob('data/*/*.png'):
extract_ocr(png)
write_site()
def extract_images(pdf):
img_file = pdf.replace('.pdf', '-00.png')
if os.path.isfile(img_file):
logging.info('%s already exists, skipping', img_file)
return
pdf_reader = PyPDF2.PdfFileReader(open(pdf, 'rb'))
num_pages = pdf_reader.getNumPages()
for page_num in range(0, num_pages):
logging.info('extracting page %s from %s', page_num + 1, pdf)
page = pdf_reader.getPage(page_num)
try:
objects = page['/Resources']['/XObject'].getObject()
count = 0
types = []
for obj in objects:
o = objects[obj]
if o['/Subtype'] == '/Image':
count += 1
size = (o['/Width'], o['/Height'])
data = o.getData()
img_path = pdf.replace('.pdf', '-%02i.png' % page_num)
if os.path.isfile(img_path):
logging.info('%s already exists, skipping', img_path)
else:
img = Image.frombytes('RGB', size, data)
img.save(img_path)
logging.info('wrote %s', img_path)
except Exception as e:
logging.error('unable to extract images from %s: %s', pdf, e)
def extract_ocr(img_file):
ocr_file = img_file.replace('.png', '.txt')
if os.path.isfile(ocr_file):
logging.info('ocr file %s already exists, skipping', ocr_file)
else:
try:
img = Image.open(img_file)
txt = pytesseract.image_to_string(img)
with open(ocr_file, 'w') as fh:
fh.write(txt)
logging.info('wrote ocr to %s', ocr_file)
except Exception as e:
logging.error('unable to ocr %s: %s', img_file, e)
def extract_metadata(ocr_file):
logging.info('extracting metadata from %s', ocr_file)
txt = open(ocr_file).read()
# if 02.txt exists then the text from 00.txt ran over into 01.txt
file3 = re.sub('00.txt$', '02.txt', ocr_file)
if ocr_file != file3 and os.path.isfile(file3):
txt += open(re.sub('00.txt$', '01.txt', ocr_file)).read()
m = {
'id': match_int('Ad ID (\d+)', txt),
'pdf': ocr_file.replace('-00.txt', '.pdf'),
'text': match('(?s)Ad Text (.+)Ad Landing Page', txt, strip=False),
'url': match('Ad Landing Page (.+)', txt),
'impressions': match_int('Ad Impressions (.+)', txt),
'clicks': match_int('Ad Clicks (.+)', txt),
'spend': {
'amount': '{0:.2f}'.format(match_float('Ad Spend ([0-9,\.]+)', txt)),
'currency': match('Ad Spend [0-9,\.]+ (.+)', txt)
},
'created': match_datetime('Ad Creation Date (.+)', txt),
'ended': match_datetime('Ad End Date (.+)', txt),
'targeting': targeting(txt)
}
return m
def match(pattern, string, strip=True):
m = re.search(pattern, string, re.MULTILINE)
if not m:
return None
s = m.group(1)
if strip:
s = s.replace('\n', ' ')
s = re.sub(' +', ' ', s)
s = s.strip()
return s
def match_int(pattern, string):
s = match(pattern, string)
if s:
return int(s.replace(',', ''))
else:
return 0
def match_float(pattern, string):
s = match(pattern, string)
f = 0.0
if s:
f = float(s.replace(',', ''))
return f
def match_datetime(pattern, string):
s = match(pattern, string)
if s:
# clean up some known ocr edge cases
if s == '02/03/17 01 32:43 AM PST':
s = '02/03/17 01:32:43 AM PST'
if s == '05/17/1612121113 AM PDT':
s = '05/17/16 12:21:13 AM PDT'
s = re.sub(' :', ':', s)
s = re.sub('O', '0', s)
s = s.replace('z', ':')
m = re.match('^(\d\d/\d\d/\d\d)(\d.+)$', s)
if m:
s = m.group(1) + ' ' + m.group(2)
try:
dt = parse_date(s, fuzzy=True, tzinfos={
'PDT': -25200,
'PST': -28800
})
return dt.isoformat()
except Exception as e:
logging.error('unable to convert time %s: %s', s, e)
return None
def targeting(s):
# this is gnarly but a state machine appears to be required to match
# the structure in the Ad Targeting section
t = match('(?s)Ad Targeting (.+)Ad Impressions', s, strip=False)
meta = {}
key = None
if not t:
return meta
for line in t.split('\n'):
m = re.match('^([A-Z].{1,25}?):(.+)', line)
if m:
key = m.group(1)
line = m.group(2)
if key:
meta[key] = meta.get(key, '') + ' ' + line
# lower case the keys and unpack the values if there are comma or
# semicolon delimited lists
new_meta = {}
for k, v in meta.items():
k = k.lower()
k = re.sub('[^a-z ]', '', k)
k = re.sub(' +', '_', k)
sep = ';' if k == 'location' else ','
if k == "custom_audience":
new_meta[k] = v.strip()
else:
new_meta[k] = unpack(v, sep)
return new_meta
def unpack(s, sep=','):
# let it be known: this is crazypants
# ... sigh
if not s:
return []
s = re.sub(' +', ' ', s).strip()
prefix = ''
m = re.match('^([A-Z].{1,25}?):(.+)', s)
if m:
prefix = m.group(1).lower().replace(' ', '_')
s = m.group(2)
parts = s.split(sep)
if ' or ' in parts[-1]:
parts.extend(parts.pop().split(' or '))
parts = [p.strip() for p in parts]
if prefix:
data = {}
data[prefix] = parts
else:
data = parts
return data
def write_site():
if not os.path.isdir('site/images'):
os.makedirs('site/images')
items = []
for ocr in glob('data/*/*-00.txt'):
i = write_item(ocr)
if i is not None:
items.append(i)
with open('site/index.json', 'w') as fh:
json.dump(items, fh, indent=2)
logging.info('wrote site/index.json with %s items', len(items))
def write_item(ocr_file):
m = extract_metadata(ocr_file)
if not m['id']:
logging.warn('missing id %s, skipping', m['pdf'])
return None
post_file = pick_post_image(m['pdf'])
logging.info('found post image %s', post_file)
if post_file:
img_file = 'images/{}.png'.format(m['id'])
if not os.path.isfile('site/' + img_file):
crop(post_file, 'site/' + img_file)
logging.info('cropped %s as site/%s', post_file, img_file)
else:
logging.warn('cropped image file already exists site/%s', img_file)
m['image'] = img_file
else:
m['image'] = None
logging.info('no post image found for id=%s %s', m['id'], m['pdf'])
logging.info('got metadata for id=%s %s', m['id'], m['pdf'])
return m
def crop(path, new_path):
logging.info('cropping %s as %s', path, new_path)
im = Image.open(path)
w, h = im.size
# remove the bottom portion of he image that contains some text
# that seems to confuse cropping
im = im.crop((0, 0, w, h - 100))
# get the bounding box of the central region of he image
bg = Image.new(im.mode, im.size, im.getpixel((0,0)))
diff = ImageChops.difference(im, bg)
diff = ImageChops.add(diff, diff, 2.0, -100)
bbox = diff.getbbox()
# crop the image and save it
if bbox:
# add a margin
m = 50
bbox = (bbox[0] - m, bbox[1] - m, bbox[2] + m, bbox[3] + m)
# crop
new_img = im.crop(bbox)
# resize
w, h = new_img.size
if w > h:
new_w = 800
new_h = int(new_w * (h / w))
else:
new_h = 800
new_w = int(new_h * (w / h))
new_img = new_img.resize((new_w, new_h), Image.ANTIALIAS)
# save
logging.info('saving cropped image %s', new_path)
new_img.save(new_path)
def pick_post_image(pdf_file):
logging.info('picking post image for %s', pdf_file)
stem = pdf_file.replace('.pdf', '*.png')
files = glob(stem)
if len(files) == 1:
logging.warn('looks like missing extracted post image for %s', pdf_file)
return None
files.sort()
return files[-1]
if __name__ == "__main__":
main()