Added histories import from old version

This commit is contained in:
dbroqua 2019-12-24 14:34:22 +01:00
parent 147812a3e2
commit 7015fc1671
5 changed files with 102 additions and 2 deletions

2
.gitignore vendored
View file

@ -1,2 +1,4 @@
run.sh
node_modules
import
import.sh

92
import.js Normal file
View file

@ -0,0 +1,92 @@
const path = require('path')
const moment = require('moment')
const models = require('./models').models
// Export data from base:
// docker exec mongo sh -c 'mongoexport --host localhost --db irc-bot-rx3 --collection histories --type=json --fields artist,songName,createdAt' > rx3.json
if (!process.argv[2]) {
console.error(new Error('Missing radio alias'))
process.exit(1)
}
const radio = process.argv[2]
const file = path.join('import', `${radio}.json`)
var lineReader = require('readline').createInterface({
input: require('fs').createReadStream(file)
})
let lines = 0
let checked = 0
let inserted = 0
let canDone = false
const done = () => {
if (canDone && lines === checked) {
console.log(`Inserted ${inserted} on ${lines} lines read`)
process.exit(0)
}
}
lineReader.on('close', () => {
console.log('All lines read, waiting for insert')
canDone = true
done()
})
lineReader.on('line', function (line) {
lines += 1
const currentLine = JSON.parse(line)
const value = {
radio: radio,
artist: currentLine.artist,
title: currentLine.songName,
createdAt: currentLine.createdAt.$date
}
// console.log('WHERE:', value)
models.Histories
.find({
radio: radio,
artist: currentLine.artist,
title: currentLine.songName
})
.exec(function (err, saved) {
// console.log('SAVED:', saved)
if (err) {
checked += 1
console.error('ERR:', err)
done()
} else {
let found = false
// console.log('CHECK FOR:', value.title)
for (let i = 0; i < saved.length; i += 1) {
const currentDate = moment(saved[i].createdAt).millisecond(0)
// console.log(currentDate.toDate(), moment(value.createdAt).millisecond(0).toDate())
if (currentDate.diff(moment(value.createdAt).millisecond(0), 'seconds') === 0) {
// console.log('======= FOUND =========')
found = true
break
}
}
if (!found) {
// Insert line
const history = new models.Histories(value)
history.save(() => {
checked += 1
inserted += 1
done()
})
} else {
checked += 1
done()
}
}
})
})

View file

@ -8,7 +8,7 @@ mongoose.set('useNewUrlParser', true)
mongoose.set('useUnifiedTopology', true)
mongoose.set('useFindAndModify', false)
mongoose.set('useCreateIndex', true)
mongoose.set('debug', true)
mongoose.set('debug', false)
mongoose.connect(process.env.MONGO_URL)

View file

@ -11,6 +11,7 @@
"license": "ISC",
"dependencies": {
"irc-upd": "^0.10.0",
"moment": "^2.24.0",
"mongoose": "^5.8.3",
"radio-stream": "^0.0.1"
},
@ -23,4 +24,4 @@
"eslint-plugin-standard": "^4.0.1",
"nodemon": "^2.0.2"
}
}
}

View file

@ -1114,6 +1114,11 @@ mkdirp@^0.5.1:
dependencies:
minimist "0.0.8"
moment@^2.24.0:
version "2.24.0"
resolved "https://registry.yarnpkg.com/moment/-/moment-2.24.0.tgz#0d055d53f5052aa653c9f6eb68bb5d12bf5c2b5b"
integrity sha512-bV7f+6l2QigeBBZSM/6yTNq4P2fNpSWj/0e7jQcy87A8e7o2nAfP/34/2ky5Vw4B9S446EtIhodAzkFCcR4dQg==
mongodb@3.4.1:
version "3.4.1"
resolved "https://registry.yarnpkg.com/mongodb/-/mongodb-3.4.1.tgz#0d15e57e0ea0fc85b7a4fb9291b374c2e71652dc"