/
parsePdf.js
163 lines (135 loc) · 3.75 KB
/
parsePdf.js
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
const https = require('https')
const flatten = require('lodash/flatten')
const pdfreader = require('pdfreader')
async function bufferize(url) {
let location = url.substring(url.search("//") + 2)
let hostname = location.substring(0, location.search("/"))
let path = location.substring(location.search("/"))
let buff = new Buffer.alloc(0)
const options = {
hostname,
port: 443,
path,
method: 'GET',
}
return new Promise((resolve, reject) => {
const req = https.request(options, res => {
res.on('data', d => {
buff = Buffer.concat([ buff, d ])
})
res.on('end', () => {
resolve(buff)
})
})
req.on('error', err => {
reject(err)
})
req.end()
})
}
/**
* if second param is set then a space ' ' inserted whenever text
* chunks are separated by more than xwidth
* this helps in situations where words appear separated but
* this is because of x coords (there are no spaces between words)
* each page is a different array element
*/
async function readlines(buffer, xwidth) {
return new Promise((resolve, reject) => {
let pdftxt = new Array()
let pg = 0
new pdfreader.PdfReader().parseBuffer(buffer, (err, item) => {
if (err) {
console.log('pdf reader error: ' + err)
return resolve()
}
if (!item) {
pdftxt.forEach((a, idx) => {
pdftxt[idx].forEach((v, i) => {
pdftxt[idx][i].splice(1, 2)
})
})
return resolve(pdftxt)
}
if (item && item.page) {
pg = item.page - 1
pdftxt[pg] = []
return resolve(pdftxt)
}
if (item.text) {
var t = 0
var sp = ''
pdftxt[pg].forEach((val, idx) => {
if (val[1] == item.y) {
if (xwidth && item.x - val[2] > xwidth) {
sp += ' '
} else {
sp = ''
}
pdftxt[pg][idx][0] += sp + item.text
t = 1
}
})
if (t == 0) {
pdftxt[pg].push([ item.text, item.y, item.x ])
}
return resolve(pdftxt)
}
})
})
}
async function index(url) {
const INVALID_STRINGS = [
'[INTRODUCTION]',
'[SPONSOR MESSAGE]',
'[INTERVIEW]',
'[INTERVIEW CONTINUED]',
'[INTERVIEW CONTINUED ]',
'[END OF INTERVIEW]'
]
let buffer = await bufferize(url)
let lines = await readlines(buffer,1)
let transcriptHtml = ''
let isSponsor = false
let isEnd = false
lines = await JSON.parse(JSON.stringify(lines))
lines.forEach(_page => {
const page = flatten(_page)
page.forEach((p, i) => {
if (p.search(/\[SPONSOR MESSAGE\]/ig) >= 0) {
isSponsor = true
}
if (isSponsor && p.search(/\[INTERVIEW/ig) >= 0) {
isSponsor = false
}
const regExpStrong = /\[[0-9]+?:[0-9]+?:[0-9]+?.+:/g
const isValid = (
p &&
!isSponsor &&
p !== 'Transcript' &&
INVALID_STRINGS.indexOf(p) < 0 &&
p.search(/([A-Z])\w+\s[0-9]/g) < 0 &&
p.search(/\\[a-z]\d+/g) < 0
)
isEnd = (p.trim() === '[END]' || isEnd)
// Handle speaker title
p = p.replace(regExpStrong, txt => {
return `</p><p><strong>${txt}</strong>`
})
if (isValid && !isEnd) {
transcriptHtml += p
}
})
})
// Removes page footers
transcriptHtml = transcriptHtml.replace(/©\s20(\d+) Software Engineering Daily\0?\d+/g, '')
// TODO: Doesn't quite work yet. Incorrectly parses urls
// Separate paragraphs
// transcriptHtml = transcriptHtml.replace(/.\.[a-zA-Z]/g, txt => {
// return txt.replace(/\./, '.</p><p>')
// })
// Closes transcript HTML
transcriptHtml += `${transcriptHtml}</p><br />`
return transcriptHtml
}
module.exports = index