-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
253 lines (193 loc) · 6.3 KB
/
main.js
File metadata and controls
253 lines (193 loc) · 6.3 KB
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
const fs = require('fs');
filepath = 'testdata/all_streams.json';
fs.readFile(filepath, 'utf8', (err, data) => {
const jsonData = JSON.parse(data);
//Geht durch den JSON Datensatz und ändert das Format
const result = jsonData.reduce(
(acc, entry) => {
if (entry.ms_played > 30000 && entry.master_metadata_track_name) {
const trackArtistKey = `${entry.master_metadata_track_name}-${entry.master_metadata_album_artist_name}`;
const period = entry.ts.slice(0, 7);
let id = acc.ids[trackArtistKey];
// Check if the combination of track and artist has an id
if (!id) {
id = acc.idCounter++;
acc.ids[trackArtistKey] = id;
acc.songsData[id] = {
track: entry.master_metadata_track_name,
artist: entry.master_metadata_album_artist_name,
uri: entry.spotify_track_uri,
album: entry.master_metadata_album_album_name ?? "Local file",
};
}
// Increase Play Count
const periodMap = acc.playCountData[period] || (acc.playCountData[period] = {});
periodMap[id] = (periodMap[id] ?? 0) + 1;
}
return acc;
},
{ ids: {}, songsData: {}, playCountData: {}, idCounter: 1 }
);
let { songsData, playCountData } = result;
playCountData = Object.keys(playCountData).sort().reduce(
(obj, key) => {
obj[key] = playCountData[key];
return obj;
},
{}
);
const playCount = Object.create(process);
playCount.data = playCountData;
playCount.songs = songsData;
const topArtist = playCount.returnCopy()
const honorableMentions = playCount.returnCopy()
const allTime = playCount.returnCopy();
const topSongs = playCount.convertToSortedArray().getTop(10).returnUnique()
playCount.convertToSongObj()
topArtist.groupAll().groupByArtist().convertToSortedArray().getTop(10).convertToArtistObj()
honorableMentions.groupByYear().removeSongsInSet(topSongs).convertToSortedArray().getTop(15).convertToSongObj()
allTime.groupAll().convertToSortedArray().convertToSongObj()
// Write to file
fs.writeFileSync('json/topArtist.json', JSON.stringify(topArtist.data, null, 2));
fs.writeFileSync('json/playCount.json', JSON.stringify(playCount.data, null, 2));
fs.writeFileSync('json/honorableMentions.json', JSON.stringify(honorableMentions.data, null, 2));
fs.writeFileSync('json/topSongs.json', JSON.stringify(allTime.data, null, 2));
console.log('Data exported');
});
const process = {
data: undefined,
songs: undefined,
returnCopy: function() {
const copy = Object.create(process);
copy.data = structuredClone(this.data);
copy.songs = this.songs;
return copy;
},
//Keeps only entries after this
after: function(year, month) {
let res = {};
Object.entries(this.data).forEach(([period, data]) => {
const year2 = Number(period.slice(0,4));
const month2 = Number(period.slice(5,7));
if(year2 > year || (year2 === year && month2 > month)) {
res[period] = data;
}
});
this.data = res;
return this;
},
//Keeps only entries before this
before: function(year, month) {
let res = {};
Object.entries(this.data).forEach(([period, data]) => {
const year2 = Number(period.slice(0,4));
const month2 = Number(period.slice(5,7));
if(year2 < year || (year2 === year && month2 < month)) {
res[period] = data;
}
});
this.data = res;
return this;
},
groupAll: function() {
const res = {};
Object.values(this.data).forEach(obj => {
Object.entries(obj).forEach(([id, playCount]) => {
res[id] = (res[id] ?? 0) + playCount;
});
});
this.data = {total: res};
return this;
},
groupByYear: function() {
const res = {};
Object.entries(this.data).forEach(([period, obj]) => {
const year = period.slice(0,4);
Object.entries(obj).forEach(([id, playCount]) => {
const yearMap = res[year] || (res[year] = {});
yearMap[id] = (yearMap[id] ?? 0) + playCount;
});
});
this.data = res;
return this;
},
groupByArtist: function() {
const res = {};
Object.entries(this.data).forEach(([period, obj]) => {
Object.entries(obj).forEach(([id, playCount]) => {
const artist = this.songs[id].artist
const periodMap = res[period] || (res[period] = {});
periodMap[artist] = (periodMap[artist] ?? 0) + playCount;
});
});
this.data = res;
return this;
},
removeSongsInSet(set) {
Object.entries(this.data).forEach(([period, obj]) => {
this.data[period] = Object.keys(obj)
.filter(key => !set.has(key))
.reduce((newObject, key) => {
newObject[key] = obj[key];
return newObject;
}, {})
})
return this;
},
hasMorePlaysThan(val) {
Object.entries(this.data).forEach(([period, obj]) => {
this.data[period] = Object.fromEntries(
Object.entries(obj).filter(([, value]) => value >= val)
);
})
return this;
},
//Converts to a sorted array, all functions after this only work on sorted arrays
convertToSortedArray: function() {
Object.entries(this.data).forEach(([period, arr]) => {
this.data[period] = Object.entries(arr).sort(([, a], [, b]) => b - a);
})
return this;
},
getTop: function(top) {
Object.entries(this.data).forEach(([period, arr]) => {
if (arr.length < top) {
this.data[period] = arr
}
else {
const minPlays = arr[top-1][1];
this.data[period] = arr.slice(0, arr.findIndex((element) => element[1] < minPlays));
}
})
return this;
},
convertToSongObj: function() {
Object.entries(this.data).forEach(([period, arr]) => {
let res = {};
arr.forEach((entry, pos) => {
res[pos] = {...this.songs[entry[0]], playCount: entry[1]};
});
this.data[period] = res;
})
return this;
},
convertToArtistObj: function() {
Object.entries(this.data).forEach(([period, arr]) => {
let res = {};
arr.forEach((entry, pos) => {
res[pos] = {artist: entry[0], playCount: entry[1]};
});
this.data[period] = res;
})
return this;
},
returnUnique: function() {
const res = new Set();
Object.values(this.data).forEach((arr) => {
arr.forEach((entry) => {
res.add(entry[0]);
});
});
return res;
}
}