-
Notifications
You must be signed in to change notification settings - Fork 42
/
DataGenerator.js
106 lines (94 loc) · 2.14 KB
/
DataGenerator.js
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
import Popular from './data/Popular';
import { getApiUrl } from './itunes';
// https://rss.itunes.apple.com/en-us
const popularAPI =
'https://rss.itunes.apple.com/api/v1/us/apple-music/top-songs/all/75/explicit.json';
const moods = [
'Fun',
'Upbeat',
'Dreamy',
'Romantic',
'Bold',
'Mellow',
'Inspirational',
'Suspenseful',
];
const genres = [
'Christmas',
'Hip Hop',
'R&B and Soul',
'Rock',
'Pop',
'Country',
'Latin',
'Electronic',
'Jazz',
'Classical',
'Reggae',
'Ambient',
'Folk',
'Indian',
'Cinematic',
];
// Function to download data to a file
function download(data, filename, type) {
var file = new Blob([data], { type });
if (window.navigator.msSaveOrOpenBlob)
// IE10+
window.navigator.msSaveOrOpenBlob(file, filename);
else {
// Others
var a = document.createElement('a'),
url = URL.createObjectURL(file);
a.href = url;
a.download = filename;
document.body.appendChild(a);
a.click();
setTimeout(function() {
document.body.removeChild(a);
window.URL.revokeObjectURL(url);
}, 0);
}
}
async function getGenreAsync(genre) {
const api = getApiUrl({
media: 'music',
query: genre,
});
// const api = popularAPI;
const resp = await fetch(api);
const json = await resp.json();
return json;
}
async function createCategoryData(input, name) {
let data = {};
for (const genre of input) {
data[genre] = await getGenreAsync(encodeURI(genre));
}
console.log(name + ' :DATA: ', JSON.stringify(data));
download(JSON.stringify(getGenreAsync('')), name + '.json', 'txt');
}
async function getPPP() {
let data = [];
for (const song of Popular) {
const api = getApiUrl({
media: 'music',
query: song.name,
});
try {
const { results } = await getGenreAsync(api);
data.push(results[0]);
} catch (error) {
data.push({ api });
}
}
console.log('JJJ', data);
download(JSON.stringify(data), 'Popular-itunes' + '.json', 'txt');
}
// getPPP();
// getGenreAsync([], 'Popular');
// createCategoryData(moods, 'Moods');
// const api = getApiUrl({
// media: 'music',
// query: 'popular',
// });