Reputation: 31
I am trying to write a program that collects theme park wait times to try and create a database for some data science practice. I'm trying to collect this data from the themeparks node.js package.
My background is more in R and python, so I'm learning on the fly. I have gotten to the point now where I can output a .csv file from specified park(s), in the correct format that I want, with the file name to include the date, the headers be the name of the rides for that park, and the rows containing the wait time for the ride, with a time stamp added at the end to specify when the times were extracted.
Currently I have the file being created, the header added, then the ride time rows to be added every 3 seconds (normally 5 minutes, but for testing purposes its set at 3 seconds).
The problem I'm facing is that currently, my time stamps are not updating and neither are current times.
For future planning reference, I plan on later adding a specific time that time collection would end and start based on the park hours, but for now, I have it set to start when the app is started and run till infinity. Also, I same snippet, running for 3 other parks (take a guess which ones) within the same app to create other .csv for them, but of course they have their own credentials in order to get that data. The intent later would be to have each park have its own app, that a main app would call on them to run, but fo now they are within the same app and basically have the same structure as this snippet. As of right now, I just need to know how to get the time and data to update, and why it is not.
// include the Themeparks library
var Themeparks = require("themeparks");
var fs = require('fs');
//Date
var datetime = require('node-datetime');
var dt = datetime.create();
var TodayDate = dt.format('m-d-Y');
var TimeDate = dt.format('m-d-Y H:m');
var TimeStamp = "Time Stamp" //The Header for Time Stamp
//************************ MAGIC KINGDOM ***********************
var disneyMagicKingdom = new Themeparks.Parks.WaltDisneyWorldMagicKingdom
disneyMagicKingdom.GetWaitTimes().then(function(rides) {
for(var i=0 , ride; ride=rides[i++];) {
// Write out the data in this format: "<RIDE NAME>",<WAIT TIME>
fs.appendFileSync('Magic Kingdom ' + TodayDate + '.csv', "\"" + ride.name + "\"" + ",");
}
//Goes to the next line in the csv, so the times will start on the next line.
fs.appendFileSync('Magic Kingdom ' + TodayDate + '.csv', TimeStamp);
fs.appendFileSync('Magic Kingdom ' + TodayDate + '.csv', '\r\n');
//Repeats the Wait times interval
setInterval(MKTime, 1000*3)
function MKTime() {
for(var i=0 , ride; ride=rides[i++];) {
// Write out the data in this format: "<RIDE NAME>",<WAIT TIME>
fs.appendFileSync('Magic Kingdom ' + TodayDate + '.csv', ride.waitTime + ",");
// Write out a new line so that when this loop repeats, the next row will be written on its own line
}
fs.appendFileSync('Magic Kingdom ' + TodayDate + '.csv', TimeDate);
//Goes to the next line in the csv so when the next interval starts, the times will be on the next line
fs.appendFileSync('Magic Kingdom ' + TodayDate + '.csv', '\r\n');
}
}, console.error);
Upvotes: 0
Views: 329
Reputation: 2220
Your date and time variables are created at the begining of the code block and not inside the function called by setInterval
. Thus they are going to be whatever they were when the code was run and not change over multiple setInterval
calls. A simple fix would be to move those variable inside the function MKTime
or define new variables inside that method.
Upvotes: 3