Skip to content

Instantly share code, notes, and snippets.

@seadog007
Created January 26, 2018 06:05
Show Gist options
  • Save seadog007/f017d730c298b4dff1cf0b75b04b4870 to your computer and use it in GitHub Desktop.
Save seadog007/f017d730c298b4dff1cf0b75b04b4870 to your computer and use it in GitHub Desktop.
For http://moves-export.herokuapp.com/ when you have too many days. Run it in your console
function save() {
var textToWrite = out;
var textFileAsBlob = new Blob([textToWrite], {
type: 'text/json'
});
var fileNameToSaveAs = "moves-export.json";
var downloadLink = document.getElementById("download");
downloadLink.download = fileNameToSaveAs;
downloadLink.innerHTML = "";
if (window.webkitURL != null) {
// Chrome allows the link to be clicked
// without actually adding it to the DOM.
downloadLink.href = window.webkitURL.createObjectURL(textFileAsBlob);
} else {
// Firefox requires the link to be added to the DOM
// before it can be clicked.
downloadLink.href = window.URL.createObjectURL(textFileAsBlob);
downloadLink.onclick = destroyClickedElement;
downloadLink.style.display = "none";
document.body.appendChild(downloadLink);
}
downloadLink.click();
}
var i = $('#date').val();
var out = '';
var firstday = new Date(i.slice(0, 4), i.slice(4, 6) - 1, i.slice(6, 8));
var today = new Date();
// Calculate all days to export
var difference = Math.abs(today.getTime() - firstday.getTime());
var totaldays = Math.ceil(difference / (1000 * 3600 * 24));
console.log("Total days to export: " + totaldays);
out += '{"export":['; // Open JSON object
// Loop trough each date and get a response
day = firstday;
var i = 0;
function fetch_data() {
if (i < totaldays) {
y = day.getFullYear().toString();
m = day.getMonth();
m = m + 1;
if (m < 10) {
m = '0' + m.toString();
} else {
m = m.toString();
}
d = day.getUTCDate();
if (d < 10) {
d = '0' + d.toString();
} else {
d = d.toString();
}
export_day = y + m + d;
// Do a AJAX request to get the data of that day.
var posting = $.post('/data', {
date: export_day,
access_token: '44e5BNVJ72oGQkv2aEzEEMTlo58tx9o40NqYWPsB10_Tr0qt84_duheZDRr30fyU'
});
// Add the response to a filed
posting.done(function(data) {
out += JSON.stringify(data['data'][0]);
if (i != totaldays - 1 || i != 0) { // First and last trailing comma removed to produce valid JSON.
out += ',';
}
});
console.log((totaldays - 1) + ' ' + i);
// Add another day
day = new Date(day.getTime() + (24 * 60 * 60 * 1000));
// Continue the loop in 2s
setTimeout(fetch_data, 2000);
i++;
} else { // When the loop is done, end object
out += ']}';
alert('Done exporting!');
save();
}
}
setTimeout(fetch_data, 0);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment