-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
49 lines (39 loc) · 1.25 KB
/
index.ts
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
import fs from 'fs'
import { parseArgs } from 'util'
import { Glob } from 'bun'
const { positionals } = parseArgs({
args: Bun.argv,
strict: true,
allowPositionals: true
})
const scrapers = positionals.slice(2)
const glob = new Glob('./organizations/**/*.ts')
let foundScraper = false
let availableScrapers: string[] = []
for await (const file of glob.scan('.')) {
const match = file.match(/organizations\/(?<id>\w+).ts/)
const organizationId = match?.groups?.id
if (scrapers.length) {
if (organizationId && scrapers.includes(organizationId)) {
foundScraper = true
console.log(`Scraping ${organizationId}...`)
const { default: scrape } = await import(file)
const stream = fs.createWriteStream(
`./data/${organizationId}.ndjson`,
'utf8'
)
for await (const item of await scrape()) {
const itemWithOrganizationId = { organizationId, ...item }
console.log(itemWithOrganizationId)
stream.write(`${JSON.stringify(itemWithOrganizationId)}\n`)
}
stream.end()
}
} else if (organizationId) {
availableScrapers.push(organizationId)
}
}
if (!foundScraper) {
console.log('Available scrapers:')
console.log(availableScrapers.map((id) => ` - ${id}`).join('\n'))
}