Created
March 4, 2018 12:15
-
-
Save ruohki/7780042b28d3c83eb71e71418ee8eb91 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const MongoClient = require('mongodb').MongoClient; | |
const _ = require('lodash') | |
const { parseString } = require('xml2js') | |
const fetch = require('node-fetch') | |
const moment = require('moment') | |
const MONGO_URL = "mongodb://XXX"; | |
const XML_URL = "http://www.erfurt.de/sve/pls/info_v1.xml"; | |
const parseNumbers = (str) => { | |
if (!isNaN(str)) { | |
str = str % 1 === 0 ? parseInt(str, 10) : parseFloat(str); | |
} | |
return str; | |
}; | |
(async () => { | |
try { | |
const connection = await MongoClient.connect(MONGO_URL, { keepAlive: true }); | |
const db = connection.db('heroku_83jx6qpf'); | |
const Parkhaeuser = db.collection('parkhaeuser'); | |
const Messungen = db.collection('messungen'); | |
const liveData = await fetch(XML_URL) | |
.then(res => res.text()) | |
.then(body => new Promise( (res, rej) => parseString(body, { explicitArray: false, valueProcessors: [parseNumbers]}, (err, data) => err ? rej(err) : res(_.get(data, 'parkhaeuser.ph', []) )))) | |
for(let entry of liveData) { | |
const { id, longname, kapazitaet } = entry; | |
const timestamp = moment(entry.zeitpunkt) | |
await Parkhaeuser.update({ _id: id}, { $set: { longname, kapazitaet } }, { upsert: true }) | |
if (timestamp.isValid()) { | |
await Messungen.update({ zeitpunkt: timestamp.toDate(), parkhausId: id }, { $set: Object.assign({}, | |
_.pick(entry, ['oeffnungszustand','status','tendenz','belegung']), | |
{ | |
parkhausId: id, | |
zeitpunkt: timestamp.toDate() | |
} | |
)}, { upsert: true }) | |
} | |
} | |
connection.close(); | |
} catch (err) { | |
console.error(err) | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment