aboutsummaryrefslogtreecommitdiff
path: root/src/scraping/buxton/scraper.py
blob: 29cb8a2568dc9a60da78dea9288d319c1b29d0c7 (plain)
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
import os
from shutil import copyfile
import docx2txt
from docx import Document
from docx.opc.constants import RELATIONSHIP_TYPE as RT
import re
from pymongo import MongoClient
import shutil
import uuid
import datetime
from PIL import Image
import math
import sys

source = "./source"
dist = "../../server/public/files"

db = MongoClient("localhost", 27017)["Dash"]
target_collection = db.newDocuments
schema_guids = []
common_proto_id = ""


def extract_links(fileName):
    links = []
    doc = Document(fileName)
    rels = doc.part.rels
    for rel in rels:
        item = rels[rel]
        if item.reltype == RT.HYPERLINK and ".aspx" not in item._target:
            links.append(item._target)
    return text_doc_map(links)


def extract_value(kv_string):
    pieces = kv_string.split(":")
    return (pieces[1] if len(pieces) > 1 else kv_string).strip()


def mkdir_if_absent(path):
    try:
        if not os.path.exists(path):
            os.mkdir(path)
    except OSError:
        print("failed to create the appropriate directory structures for %s" % file_name)


def guid():
    return str(uuid.uuid4())


def listify(list):
    return {
        "fields": list,
        "__type": "list"
    }


def protofy(fieldId):
    return {
        "fieldId": fieldId,
        "__type": "proxy"
    }


def text_doc_map(string_list):
    def guid_map(caption):
        return write_text_doc(caption)
    return listify(proxify_guids(list(map(guid_map, string_list))))


def write_schema(parse_results, display_fields, storage_key):
    view_guids = parse_results["child_guids"]

    data_doc = parse_results["schema"]
    fields = data_doc["fields"]

    view_doc_guid = guid()

    view_doc = {
        "_id": view_doc_guid,
        "fields": {
            "proto": protofy(data_doc["_id"]),
            "x": 10,
            "y": 10,
            "width": 900,
            "height": 600,
            "panX": 0,
            "panY": 0,
            "zoomBasis": 1,
            "zIndex": 2
            "viewType": 2
        },
        "__type": "Doc"
    }

    fields["proto"] = protofy(common_proto_id)
    fields[storage_key] = listify(proxify_guids(view_guids))
    fields["schemaColumns"] = listify(display_fields)
    fields["backgroundColor"] = "white"
    fields["scale"] = 0.5
    fields["viewType"] = 2
    fields["author"] = "Bill Buxton"
    fields["creationDate"] = {
        "date": datetime.datetime.utcnow().microsecond,
        "__type": "date"
    }
    fields["isPrototype"] = True
    fields["page"] = -1

    target_collection.insert_one(data_doc)
    target_collection.insert_one(view_doc)

    data_doc_guid = data_doc["_id"]
    print(f"inserted view document ({view_doc_guid})")
    print(f"inserted data document ({data_doc_guid})\n")

    return view_doc_guid


def write_text_doc(content):
    data_doc_guid = guid()
    view_doc_guid = guid()

    view_doc = {
        "_id": view_doc_guid,
        "fields": {
            "proto": protofy(data_doc_guid),
            "x": 10,
            "y": 10,
            "width": 400,
            "zIndex": 2
        },
        "__type": "Doc"
    }

    data_doc = {
        "_id": data_doc_guid,
        "fields": {
            "proto": protofy("textProto"),
            "data": {
                "Data": '{"doc":{"type":"doc","content":[{"type":"paragraph","content":[{"type":"text","text":"' + content + '"}]}]},"selection":{"type":"text","anchor":1,"head":1}' + '}',
                "__type": "RichTextField"
            },
            "title": content,
            "nativeWidth": 200,
            "author": "Bill Buxton",
            "creationDate": {
                "date": datetime.datetime.utcnow().microsecond,
                "__type": "date"
            },
            "isPrototype": True,
            "autoHeight": True,
            "page": -1,
            "nativeHeight": 200,
            "height": 200,
            "data_text": content
        },
        "__type": "Doc"
    }

    target_collection.insert_one(view_doc)
    target_collection.insert_one(data_doc)

    return view_doc_guid


def write_image(folder, name):
    path = f"http://localhost:1050/files/{folder}/{name}"

    data_doc_guid = guid()
    view_doc_guid = guid()

    image = Image.open(f"{dist}/{folder}/{name}")
    native_width, native_height = image.size

    view_doc = {
        "_id": view_doc_guid,
        "fields": {
            "proto": protofy(data_doc_guid),
            "x": 10,
            "y": 10,
            "width": min(800, native_width),
            "zIndex": 2
        },
        "__type": "Doc"
    }

    data_doc = {
        "_id": data_doc_guid,
        "fields": {
            "proto": protofy("imageProto"),
            "data": {
                "url": path,
                "__type": "image"
            },
            "title": name,
            "nativeWidth": native_width,
            "author": "Bill Buxton",
            "creationDate": {
                "date": datetime.datetime.utcnow().microsecond,
                "__type": "date"
            },
            "isPrototype": True,
            "page": -1,
            "nativeHeight": native_height,
            "height": native_height
        },
        "__type": "Doc"
    }

    target_collection.insert_one(view_doc)
    target_collection.insert_one(data_doc)

    return view_doc_guid


def parse_document(file_name: str):
    print(f"parsing {file_name}...")
    pure_name = file_name.split(".")[0]

    result = {}

    dir_path = dist + "/" + pure_name
    mkdir_if_absent(dir_path)

    raw = str(docx2txt.process(source + "/" + file_name, dir_path))

    view_guids = []
    count = 0
    for image in os.listdir(dir_path):
        count += 1
        view_guids.append(write_image(pure_name, image))
        copyfile(dir_path + "/" + image, dir_path +
                 "/" + image.replace(".", "_o.", 1))
        copyfile(dir_path + "/" + image, dir_path +
                  "/" + image.replace(".", "_m.", 1))
    print(f"extracted {count} images...")

    def sanitize(line): return re.sub("[\n\t]+", "", line).replace(u"\u00A0", " ").replace(
        u"\u2013", "-").replace(u"\u201c", '''"''').replace(u"\u201d", '''"''').strip()

    def sanitize_price(raw: str):
        raw = raw.replace(",", "")
        start = raw.find("$")
        if start > -1:
            i = start + 1
            while (i < len(raw) and re.match(r"[0-9\.]", raw[i])):
                i += 1
            price = raw[start + 1: i + 1]
            return float(price)
        elif (raw.lower().find("nfs")):
            return -1
        else:
            return math.nan

    def remove_empty(line): return len(line) > 1

    lines = list(map(sanitize, raw.split("\n")))
    lines = list(filter(remove_empty, lines))

    result["file_name"] = file_name
    result["title"] = lines[2].strip()
    result["short_description"] = lines[3].strip().replace(
        "Short Description: ", "")

    cur = 5
    notes = ""
    while lines[cur] != "Device Details":
        notes += lines[cur] + " "
        cur += 1
    result["buxton_notes"] = notes.strip()

    cur += 1
    clean = list(
        map(lambda data: data.strip().split(":"), lines[cur].split("|")))
    result["company"] = clean[0][len(clean[0]) - 1].strip()
    result["year"] = clean[1][len(clean[1]) - 1].strip()
    result["original_price"] = sanitize_price(
        clean[2][len(clean[2]) - 1].strip())

    cur += 1
    result["degrees_of_freedom"] = extract_value(
        lines[cur]).replace("NA", "N/A")
    cur += 1

    dimensions = lines[cur].lower()
    if dimensions.startswith("dimensions"):
        dim_concat = dimensions[11:].strip()
        cur += 1
        while lines[cur] != "Key Words":
            dim_concat += (" " + lines[cur].strip())
            cur += 1
        result["dimensions"] = dim_concat
    else:
        result["dimensions"] = "N/A"

    cur += 1
    result["primary_key"] = extract_value(lines[cur])
    cur += 1
    result["secondary_key"] = extract_value(lines[cur])

    while lines[cur] != "Links":
        result["secondary_key"] += (" " + extract_value(lines[cur]).strip())
        cur += 1

    cur += 1
    link_descriptions = []
    while lines[cur] != "Image":
        link_descriptions.append(lines[cur].strip())
        cur += 1
    result["link_descriptions"] = text_doc_map(link_descriptions)

    result["hyperlinks"] = extract_links(source + "/" + file_name)

    images = []
    captions = []
    cur += 3
    while cur + 1 < len(lines) and lines[cur] != "NOTES:":
        images.append(lines[cur])
        captions.append(lines[cur + 1])
        cur += 2
    result["images"] = listify(images)

    result["captions"] = text_doc_map(captions)

    notes = []
    if (cur < len(lines) and lines[cur] == "NOTES:"):
        cur += 1
        while cur < len(lines):
            notes.append(lines[cur])
            cur += 1
    if len(notes) > 0:
        result["notes"] = listify(notes)

    print("writing child schema...")

    return {
        "schema": {
            "_id": guid(),
            "fields": result,
            "__type": "Doc"
        },
        "child_guids": view_guids
    }


def proxify_guids(guids):
    return list(map(lambda guid: {"fieldId": guid, "__type": "proxy"}, guids))


def write_common_proto():
    id = guid()
    common_proto = {
        "_id": id,
        "fields": {
            "proto": protofy("collectionProto"),
            "title": "Common Import Proto",
        },
        "__type": "Doc"
    }

    target_collection.insert_one(common_proto)

    return id


if os.path.exists(dist):
    shutil.rmtree(dist)
while os.path.exists(dist):
    pass
os.mkdir(dist)
mkdir_if_absent(source)

common_proto_id = write_common_proto()

candidates = 0
for file_name in os.listdir(source):
    if file_name.endswith('.docx'):
        candidates += 1
        schema_guids.append(write_schema(
            parse_document(file_name), ["title", "data"], "image_data"))

print("writing parent schema...")
parent_guid = write_schema({
    "schema": {
        "_id": guid(),
        "fields": {},
        "__type": "Doc"
    },
    "child_guids": schema_guids
}, ["title", "short_description", "original_price"], "data")

print("appending parent schema to main workspace...\n")
target_collection.update_one(
    {"fields.title": "WS collection 1"},
    {"$push": {"fields.data.fields": {"fieldId": parent_guid, "__type": "proxy"}}}
)

print("rewriting .gitignore...\n")
lines = ['*', '!.gitignore']
with open(dist + "/.gitignore", 'w') as f:
    f.write('\n'.join(lines))

suffix = "" if candidates == 1 else "s"
print(f"conversion complete. {candidates} candidate{suffix} processed.")