This repository has been archived by the owner on Dec 5, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathmigrate-cds-scraper.js
237 lines (195 loc) · 6.65 KB
/
migrate-cds-scraper.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
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
/* Hacky hacky */
const fs = require('fs')
const path = require('path')
const is = require('is')
// Utils /////////////////////////////////////
function makeLiScraper (cdsScraper, startDate) {
const getBody = (string) => string.substring(
string.indexOf("{") + 1,
string.lastIndexOf("}")
)
const body = getBody(cdsScraper.toString())
// Each call to 'fetch' is a new crawl.
const crawlRe = /fetch\.(.*?)\(.*?, (.*?), '(.*?)'/g
const crawlMatches = []
let match = crawlRe.exec(body)
while(match !== null ) {
crawlMatches.push(match)
match = crawlRe.exec(body)
}
const crawl = crawlMatches.map(cm => {
let type = cm[1]
if (type === 'fetch')
type = 'raw'
const hsh = {
type: type,
url: cm[2]
}
if (cm[3] !== 'default')
hsh.name = cm[3]
return hsh
})
const s = {
startDate: startDate,
crawl,
scrape: new Function('data, date, helpers', '/* TODO uncomment + fix! \n' + body + '\n */')
}
return s
}
// Stringize a js object, including functions.
function stringize (obj, level = 0) {
const indent = () => { return ' '.repeat(level * 2) }
if (is.string(obj)) {
return "'" + obj.replace(/\'/, "\'" + "'") + "',"
}
if (is.number(obj)) {
return obj + ','
}
if (is.array(obj)) {
let ret = []
ret.push('[')
level += 1
ret = ret.concat(obj.map(o => stringize(o, level)))
level -= 1
ret.push(indent() + '],')
return ret.join('\n')
}
if (is.hash(obj)) {
let ret = []
ret.push(indent() + '{')
level += 1
for (const k of Object.keys(obj)) {
ret.push(indent() + k + ': ' + stringize(obj[k], level))
}
level -= 1
ret.push(indent() + '},')
return ret.join('\n')
}
return obj
}
////////////////////////////////////////////////////
// Main
// Things to fix, printed at the end.
const todos = []
const fname = process.argv[2].replace(/\\/g, '/')
if (!fs.existsSync(fname)) {
console.log(`No file ${fname}, quitting.`)
process.exit(0)
}
let rawcontent = fs.readFileSync(fname, 'utf-8')
// If there were any local imports, get those, and todo those files.
const localImportsRe = /^import.*?from '\.\/.*?;$/gm
const localImportMatches = []
let match = localImportsRe.exec(rawcontent)
while(match !== null) {
localImportMatches.push(match[0])
match = localImportsRe.exec(rawcontent)
}
// Start hacking away at content until it evals successfully locally.
let content = rawcontent.replace(/^import.*;$/gmi, '')
content = content.replace('export default scraper;', '')
content = content.replace(/^\s*[\r\n]/gm, '')
content = content.replace('const scraper =', 'module.exports =')
const requires = `
const srcShared = '../src/shared/'
// const assert = require('assert')
const constants = require(srcShared + 'sources/_lib/constants.js')
const datetime = require(srcShared + 'datetime/index.js')
const geography = require(srcShared + 'sources/_lib/geography/index.js')
const maintainers = require(srcShared + 'sources/_lib/maintainers.js')
const parse = require(srcShared + 'sources/_lib/parse.js')
const sorter = require(srcShared + 'utils/sorter.js')
const transform = require(srcShared + 'sources/_lib/transform.js')
// const { UNASSIGNED } = require(srcShared + 'sources/_lib/constants.js')
const { join, sep } = require('path')
// const usStates = require('./us-states.json')
// const glob = require('glob').sync
// const globJoin = require('../../utils/glob-join.js')
// const gssCodeMap = require('../_shared.js')
// const gssCodes = require('./gss-codes.json')
// const latinizationMap = require('./latinization-map.json')
`
todos.push('delete unused requires')
const source = eval(requires + content)
if (source.sources) {
if (source.sources.length > 1)
todos.push('multiple sources listed, only taking first, please verify')
source.friendly = source.sources[0]
delete source.sources
}
const { url, type, data } = source
if (url) {
todos.push('fyi deleted source.url, value = ' + source.url)
delete source.url
}
if (type) {
todos.push('fyi deleted source.type, value = ' + source.type)
delete source.type
}
if (data) {
todos.push('fyi deleted source.data, value = ' + source.data)
delete source.data
}
// Make Li scrapers
const finalScrapers = []
if (is.function(source.scraper)) {
// this is a single-func only thing
todos.push('fix 1999-09-09 start date')
finalScrapers.push(makeLiScraper(source.scraper, '1999-09-09'))
todos.push('fix 1999-09-09 scrape and crawl')
} else if (is.object(source.scraper)) {
// keys are the dates
Object.keys(source.scraper).forEach(k => {
if (k === '0') {
todos.push('fix 0 start date')
}
finalScrapers.push(makeLiScraper(source.scraper[k], k))
todos.push('fix ' + k + ' scrape and crawl')
})
} else {
todos.push('fix busted scraper thing ??? Not sure what this is ...')
}
// Remove CDS, add Li scrapers
delete source.scraper
source.scrapers = finalScrapers
// Print ////////////////////////////////////////
// New file newcontent
const newcontent = []
const sepAt = 'src/shared/scrapers/'
const shortfname = sepAt + fname.split(sepAt)[1]
newcontent.push(`// Migrated from coronadatascraper, ${shortfname}\n`)
const fileDepth = fname.split(sepAt)[1].split('/').length
// Dump some common headers.
// Hacking src/shared so that the gen'd file has the right rel path for src/shared.
// Yep, hacky hacky.
const newSrc = requires.replace("const srcShared = '../src/shared/'", `const srcShared = '${'../'.repeat(fileDepth)}'`)
newcontent.push(newSrc)
if (localImportMatches.length > 0) {
todos.push('fix local imports')
localImportMatches.map(s => newcontent.push(s))
newcontent.push('')
}
let output = 'module.exports = ' + stringize(source)
if (output.includes('maintainers'))
todos.push('"normalize" maintainers (replace with references, sorry :-) )')
output = output.replace(/scrape: function anonymous\(data, date, helpers\n\) \{/gm, 'scrape (data, date, helpers) {')
newcontent.push(output)
newcontent.push('\n')
todos.map(s => '// TODO: ' + s).map(s => newcontent.push(s))
const outfile = process.argv[3]
if (outfile === null || outfile === undefined) {
console.log(newcontent.join('\n'))
process.exit(0)
}
const fullPath = path.join(process.cwd(), 'src', 'shared', 'sources', ...outfile.split(path.sep))
const parts = fullPath.split(path.sep)
const filename = parts[parts.length - 1]
const dirname = parts.slice(0, parts.length - 1).join(path.sep)
console.log('Writing ' + filename + ' to ' + dirname)
if (!fs.existsSync(dirname))
fs.mkdirSync(dirname, { recursive: true })
if (fs.existsSync(fullPath)) {
console.log(`File ${outfile} already exists, quitting ...`)
process.exit(0)
}
fs.writeFileSync(fullPath, newcontent.map(s => s.replace(/;$/, '')).join('\n'))