Skip to content

Instantly share code, notes, and snippets.

@petercunha
Last active Apr 3, 2019
Embed
What would you like to do?
Script to aid in parsing Collection #1
// Reads in email:password file "input.txt" and converts it into JSON "output.json" for easy MongoDB import.
const readline = require('readline')
const fs = require('fs')
const writeStream = fs.createWriteStream('output.json')
let line_no = 0
let invalid = 0
let rl = readline.createInterface({
input: fs.createReadStream('input.txt')
})
setInterval(() => {
console.log(
'inserted ' +
Number((100 * line_no) / 10000000.0).toFixed(2) + /* Replace "10000000" with your number of lines" */
'% with ' +
Number((100 * invalid) / line_no).toFixed(2) +
'% invalid'
)
}, 500)
rl.on('line', line => {
try {
var i = line.indexOf(':')
var splits = [line.slice(0, i), line.slice(i + 1)]
var data = JSON.stringify({
email: splits[0],
password: splits[1]
})
writeStream.write(data + '\n')
} catch (error) {}
line_no++
})
rl.on('close', line => {
writeStream.close()
console.log('Total lines: ' + line_no)
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment