-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathextractFromDb.js
executable file
·133 lines (132 loc) · 4.66 KB
/
extractFromDb.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
#!/usr/bin/env node
const { MONGODB: DB, ES } = process.env
const mongoose = require('mongoose')
const PaperUtils = require('./view/paperutils')
const fs = require('fs')
const path = require('path')
const outputDir = process.cwd()
const elasticsearch = require('elasticsearch')
const sspdf = require('./lib/sspdf')
mongoose.Promise = global.Promise
let db = mongoose.createConnection(DB)
let es = new elasticsearch.Client({
host: ES
})
db.on('error', err => {
console.error(err)
process.exit(1)
})
db.on('open', () => {
let texStr = ''
const pageHeightInternal = 842
const scale = 0.0352626749
let seenQuestions = new Map()
let totalQNumber = 0
require('./lib/dbModel.js')(db, es).then(({PastPaperIndex, PastPaperDoc}) => {
PastPaperDoc.find({subject: '9702', paper: 1, type: 'qp'}).then(docs => {
docs.sort(PaperUtils.funcSortSet)
function doDoc(i) {
if (i >= docs.length) {
process.stdout.write('\n')
fs.writeFileSync(path.join(outputDir, 'struct.tex'), texStr, {encoding: 'utf8'})
process.exit(0)
return
}
let doc = docs[i]
if (parseInt(doc.time.substr(1)) < 10) {
doDoc(i + 1)
return
}
let setName = PaperUtils.setToString(doc)
let fname = `${setName}_${doc.type}.pdf`
doc.getFileBlob().then(blob => {
fs.writeFile(path.join(outputDir, fname), blob, {encoding: null}, (err) => {
if (err) {
console.error(err)
process.exit(1)
return
}
process.stdout.write(`Extracted ${i + 1} out of ${docs.length} total. (${Math.round(((i + 1) / docs.length) * 1000) / 10}%) \r`)
if (doc.type !== 'qp' && doc.type !== 'sp') {
doDoc(i + 1)
return
}
let texBk = texStr
texStr += `\\paper{${setName.replace(/_/g, ' ')}}\n`
doc.ensureDir().then(dir => {
if (!dir.dirs || dir.dirs.length === 0) {
texStr += `\\error{${setName}}{Empty dir}\n`
doDoc(i + 1)
return
}
let qs = dir.dirs
let qAdded = 0
function doQi (qi) {
if (qi >= qs.length) {
if (qAdded === 0) {
texStr = texBk
}
doDoc(i + 1)
return
}
let q = qs[qi]
if (q.qN > 20) return doQi(qi + 1)
let seenBeforeQ = seenQuestions.get(q.qT.trim())
if (seenBeforeQ) {
texStr += `\\duplicateskipped{${q.qN}}{${q.qT.replace(/[^A-Za-z0-9\-\s]/g, '').split(/\s+/).slice(0, 5).join(' ')}\\ldots}{${seenBeforeQ}}\n`
doQi(qi + 1)
return
}
seenQuestions.set(q.qT, 'q' + totalQNumber)
new Promise((resolve, reject) => {
if (qi < qs.length - 1) {
let nextQ = qs[qi + 1]
if (nextQ.page === q.page) {
resolve(nextQ.qNRect.y1 - 16.56)
} else {
findSfw()
}
} else {
findSfw()
}
function findSfw () {
sspdf.getPage(blob, q.page, function(err, ssResult) {
if (err) {
process.stderr.write(err.toString())
resolve(782.24)
return
}
let sfw = ssResult.text.lastIndexOf('Space for working')
if (sfw >= 0) {
resolve(ssResult.rects[sfw].y1 - 5)
} else {
process.stderr.write(`\nResorting to page bottom for ${fname}...\n`)
resolve(782.24)
}
})
}
}).then(nextY1 => {
texStr += `\\labelq{q${totalQNumber}}\\includegraphics[page=${q.page + 1},trim=1cm ${(pageHeightInternal - nextY1) * scale}cm 1cm ${q.qNRect.y1 * scale}cm,clip,width=\\textwidth]{${fname}}\n`
totalQNumber ++
qAdded ++
doQi(qi + 1)
})
}
doQi(0)
}, err => {
texStr += `\\error{${setName}}{Can not generate dir}\n`
doDoc(i + 1)
})
})
}, err => {
console.error(err)
process.exit(1)
})
}
doDoc(0)
})
}, err => {
console.error(err)
process.exit(1)
})
})