aboutsummaryrefslogtreecommitdiff
path: root/src/scraping/buxton/node_scraper.ts
blob: f80821ea5cc78421e5cb9d8e4863d724ce276761 (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
import { readdirSync, writeFile } from "fs";
import * as path from "path";
import { red, cyan, yellow } from "colors";
const StreamZip = require('node-stream-zip');

export interface DeviceDocument {
    title: string;
    shortDescription: string;
    longDescription: string;
    company: string;
    year: number;
    originalPrice: number;
    degreesOfFreedom: number;
    dimensions: string;
    primaryKey: string;
    secondaryKey: string;
}

type Converter<T> = (raw: string) => { transformed?: T, error?: string };

interface Processor<T> {
    exp: RegExp;
    transformer?: Converter<T>;
}

const RegexMap = new Map<keyof DeviceDocument, Processor<any>>([
    ["title", { exp: /contact\s+(.*)Short Description:/ }],
    ["company", { exp: /Company:\s+([^\|]*)\s+\|/ }],
    ["year", { exp: /Year:\s+([^\|]*)\s+\|/, transformer: numberValue }],
    ["primaryKey", { exp: /Primary:\s+(.*)Secondary:/, transformer: collectTokens }],
    ["secondaryKey", { exp: /Secondary:\s+([^\{\}]*)Links/, transformer: collectTokens }],
    ["originalPrice", { exp: /Original Price \(USD\)\:\s+\$([0-9\.]+)/, transformer: numberValue }],
    ["degreesOfFreedom", { exp: /Degrees of Freedom:\s+([0-9]+)/, transformer: numberValue }],
    ["dimensions", { exp: /Dimensions\s+\(L x W x H\):\s+([0-9]+\s+x\s+[0-9]+\s+x\s+[0-9]+\s\([A-Za-z]+\))/ }],
    ["shortDescription", { exp: /Short Description:\s+(.*)Bill Buxton[’']s Notes/ }],
    ["longDescription", { exp: /Bill Buxton[’']s Notes(.*)Device Details/ }],
]);

function numberValue(raw: string) {
    const transformed = Number(raw);
    if (isNaN(transformed)) {
        return { error: `${transformed} cannot be parsed to a numeric value.` };
    }
    return { transformed };
}

function collectTokens(raw: string) {
    return { transformed: raw.replace(/,|\s+and\s+/g, " ").split(/\s+/).sort() };
}

const deviceKeys = Array.from(RegexMap.keys());

function printEntries(zip: any) {
    console.log("READY!", zip.entriesCount);
    for (const entry of Object.values(zip.entries()) as any[]) {
        const desc = entry.isDirectory ? 'directory' : `${entry.size} bytes`;
        console.log(`Entry ${entry.name}: ${desc}`);
    }
}

export async function open(path: string) {
    const zip = new StreamZip({
        file: path,
        storeEntries: true
    });
    return new Promise<string>((resolve, reject) => {
        zip.on('ready', () => {
            let body = "";
            zip.stream("word/document.xml", (error: any, stream: any) => {
                if (error) {
                    reject(error);
                }
                stream.on('data', (chunk: any) => body += chunk.toString());
                stream.on('end', () => {
                    resolve(body);
                    zip.close();
                });
            });
        });
    });
}

export async function extract(path: string) {
    const contents = await open(path);
    let body = "";
    const components = contents.toString().split('<w:t');
    for (const component of components) {
        const tags = component.split('>');
        const content = tags[1].replace(/<.*$/, "");
        body += content;
    }
    return body;
}

function tryGetValidCapture(matches: RegExpExecArray | null) {
    let captured: string;
    if (!matches || !(captured = matches[1])) {
        return undefined;
    }
    const lower = captured.toLowerCase();
    if (/to come/.test(lower)) {
        return undefined;
    }
    if (lower.includes("xxx")) {
        return undefined;
    }
    if (!captured.toLowerCase().replace(/[….\s]+/g, "").length) {
        return undefined;
    }
    return captured;
}

export function analyze(path: string, body: string): { device?: DeviceDocument, errors?: any } {
    const device: any = {};

    const segments = path.split("/");
    const filename = segments[segments.length - 1].replace("Bill_Notes_", "");

    const errors: any = { filename };

    for (const key of deviceKeys) {
        const { exp, transformer } = RegexMap.get(key)!;
        const matches = exp.exec(body);

        let captured = tryGetValidCapture(matches);
        if (!captured) {
            errors[key] = `ERR__${key.toUpperCase()}__: outer match ${matches === null ? "wasn't" : "was"} captured.`;
            continue;
        }

        if (!transformer) {
            captured = captured.replace(/\./g, ". ").replace(/\:/g, ": ").replace(/\,/g, ", ").replace(/\?/g, "? ").trimRight();
            captured = captured.replace(/\s{2,}/g, " ");
        } else {
            const { error, transformed } = transformer(captured);
            if (error) {
                errors[key] = `__ERR__${key.toUpperCase()}__TRANSFORM__: ${error}`;
                continue;
            }
            captured = transformed;
        }

        device[key] = captured;
    }

    const errorKeys = Object.keys(errors);
    if (errorKeys.length > 1) {
        console.log(red(`\n@ ${cyan(filename.toUpperCase())}...`));
        errorKeys.forEach(key => key !== "filename" && console.log(red(errors[key])));
        return { errors };
    }

    return { device };
}

async function parse() {
    const sourceDirectory = path.resolve(`${__dirname}/source`);
    const candidates = readdirSync(sourceDirectory).filter(file => file.endsWith(".doc") || file.endsWith(".docx")).map(file => `${sourceDirectory}/${file}`);
    const imported = await Promise.all(candidates.map(async path => ({ path, body: await extract(path) })));
    // const imported = [{ path: candidates[10], body: await extract(candidates[10]) }];
    const data = imported.map(({ path, body }) => analyze(path, body));
    const masterdevices: DeviceDocument[] = [];
    const masterErrors: any[] = [];
    data.forEach(({ device, errors }) => {
        if (device) {
            masterdevices.push(device);
        } else {
            masterErrors.push(errors);
        }
    });
    const total = candidates.length;
    // if (masterdevices.length + masterErrors.length !== total) {
    //     throw new Error(`Encountered a ${masterdevices.length} to ${masterErrors.length} mismatch in device / error split!`);
    // }
    console.log();
    await writeOutputFile("buxton.json", masterdevices, total, true);
    await writeOutputFile("errors.json", masterErrors, total, false);
    console.log();
}

async function writeOutputFile(relativePath: string, data: any[], total: number, success: boolean) {
    console.log(yellow(`Encountered ${data.length} ${success ? "valid" : "invalid"} documents out of ${total} candidates. Writing ${relativePath}...`));
    return new Promise<void>((resolve, reject) => {
        const destination = path.resolve(__dirname, relativePath);
        const contents = JSON.stringify(data, undefined, 4);
        writeFile(destination, contents, err => err ? reject(err) : resolve());
    });
}

parse();