-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.html
206 lines (182 loc) · 6.15 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
<!DOCTYPE html>
<html>
<head>
<title>Fitbit Data JSON to .csv File</title>
<style>
body {
font: 14px "Arial", sans-serif;
text-align: center;
}
header {
background-color: steelblue;
color: whitesmoke;
padding: 10px;
}
</style>
<!-- Global site tag (gtag.js) - Google Analytics -->
<script
async
src="https://www.googletagmanager.com/gtag/js?id=UA-105987818-6"
></script>
<!-- Global site tag (gtag.js) - Google Analytics -->
<script
async
src="https://www.googletagmanager.com/gtag/js?id=UA-105987818-6"
></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag() {
dataLayer.push(arguments);
}
gtag("js", new Date());
gtag("config", "UA-105987818-6");
</script>
</head>
<body>
<header>
<h1>Fitbit Data JSON to .csv File Converter</h1>
<h3>Easy as 1, 2, 3!</h3>
</header>
<div class="container">
<h2>
<strong>1</strong>: Choose your timezone to convert data timestamp from
GMT
</h2>
<select id="timezoneSelector">
<option disabled selected value> -- Select An Option -- </option>
</select>
<h2>
<strong>2</strong>: Select the Fitbit JSON data file(s) you want in .csv
format
</h2>
<input type="checkbox" id="mergeFiles" name="merge" value="merge" />
Merge/join files into one
<input type="file" id="files" name="files[]" multiple />
<p>
When the file selection appears, hold down "Shift" to select more than
one file!
</p>
<h2><strong>3</strong>: File(s) will automatically download</h2>
</div>
<script src="./moment.js"></script>
<script src="./moment-timezones.js"></script>
<script src="https://cdn.jsdelivr.net/npm/json2csv"></script>
<script>
var select = document.getElementById("timezoneSelector"),
timezones = moment.tz.names();
for (var i = 0; i <= timezones.length; i++) {
var opt = document.createElement("option");
opt.value = timezones[i];
opt.innerHTML = timezones[i];
select.appendChild(opt);
}
function handleFileSelect(evt) {
var files = evt.target.files; // FileList object
var merge = document.getElementById("mergeFiles").checked;
if (merge) {
mergeFiles(files, handleData);
} else {
// Loop through the FileList and render image files as thumbnails.
for (var i = 0, f; (f = files[i]); i++) {
// Only process json files.
if (!f.type.match("application/json")) {
continue;
}
var reader = new FileReader();
var fileName = f.name;
// Closure to capture the file information.
reader.onload = (function(theFile) {
return function(e) {
var contents = e.target.result;
contents = JSON.parse(contents);
handleData(contents, fileName);
};
})(f);
// Read in the image file as text.
reader.readAsText(f);
}
}
}
function resetFile() {
const file = document.getElementById("files");
file.value = "";
}
function isSameDataType(files) {
var dataType = files[0].name.split("-")[0];
for (var i = 1; i < files.length; i++) {
var test = files[i].name.split("-")[0];
if (test !== dataType) {
return false;
}
}
return true;
}
function mergeFiles(files, callback) {
var count = files.length - 1;
var mergedFile = [];
if (!isSameDataType(files)) {
alert("Sorry, when merging all files must be of same data type!");
} else {
for (var i = 0; i < count; i++) {
mergeFile(files[i]);
}
}
function mergeFile(file) {
var reader = new FileReader();
reader.onload = function(event) {
var contents = JSON.parse(this.result);
mergedFile = mergedFile.concat(contents);
if (!--count) callback(mergedFile);
};
reader.readAsText(file);
}
}
function handleData(contents, fileName) {
fileName = fileName || "Fitbit-Export";
if (contents.length === 0) return;
var timezoneSelector = document.getElementById("timezoneSelector");
var timezoneSelection =
timezoneSelector.options[timezoneSelector.selectedIndex].text;
if (timezoneSelection !== "-- Select An Option --") {
for (var i = 0; i < contents.length; i++) {
if (!contents[i].dateTime) {
break;
}
var london = moment.tz(
contents[i].dateTime,
"MM-DD-YYYY HH:mm:ss",
"Europe/London"
);
var desiredTimezone = london.clone().tz(timezoneSelection);
contents[i].dateTime = desiredTimezone.format(
"MM-DD-YYYY HH:mm:ss"
);
}
}
var csv = json2csv.parse(contents, {
flatten: true,
includeEmptyRows: true
});
var uri = "data:text/csv;charset=utf-8," + encodeURI(csv);
// Now the little tricky part.
// you can use either>> window.open(uri);
// but this will not work in some browsers
// or you will not get the correct file extension
//this trick will generate a temp <a /> tag
var link = document.createElement("a");
link.href = uri;
//set the visibility hidden so it will not effect on your web-layout
link.style = "visibility:hidden";
fileName = fileName.replace(".json", "");
link.download = fileName + ".csv";
//this part will append the anchor tag and remove it after automatic click
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
}
document
.getElementById("files")
.addEventListener("change", handleFileSelect, false);
</script>
</body>
</html>