-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtop5.js
More file actions
139 lines (122 loc) · 4.56 KB
/
top5.js
File metadata and controls
139 lines (122 loc) · 4.56 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
window.addEventListener('load', function () {
var string = localStorage.getItem('top5_button_was_pressed');
if (string) {
getTop5(string);
}
});
function getTop5(type) {
var tokens = JSON.parse(localStorage.getItem("tokens"));
fetch('https://api.spotify.com/v1/me/top/' + type, {
method: "GET",
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer ' + tokens.access_token
}
})
.then(response => {
if (!response.ok) {
throw new Error('Failed to fetch the top 5 ' + type + ': ' + response.statusText);
}
return response.json();
})
.then(data => {
if (type === 'artists') {
var top_artists = data.items.map(artist => {
return {
name: artist.name,
image: artist.images?.[1]?.url || ''
};
});
console.log('Top Artists:', top_artists);
localStorage.setItem("artists", JSON.stringify(top_artists));
manipulateArtists();
} else if (type === 'tracks') {
var top_tracks = data.items.map(track => {
return {
name: track.album.name,
image: track.album.images?.[1]?.url || ''
};
});
console.log('Top Songs:', top_tracks);
localStorage.setItem("tracks", JSON.stringify(top_tracks));
manipulateTracks();
} else {
console.log('Unknown type:', type);
}
})
.catch(error => {
console.error('Error:', error);
});
}
function manipulateTracks() {
document.querySelector("h1").textContent = "TOP 5 TRACKS";
document.querySelector("h1").style.marginTop = "2rem";
var tracks = JSON.parse(localStorage.getItem("tracks"));
var trackContainer = document.getElementById('output_top5');
trackContainer.innerHTML = '';
for (var i = 15; i < 20; i++) {
var trackCard = document.createElement('div');
trackCard.style.display = 'inline-block';
trackCard.style.textAlign = 'center';
trackCard.style.margin = '10px';
trackCard.style.padding = '10px';
trackCard.style.border = '1px solid #ddd';
trackCard.style.borderRadius = '8px';
trackCard.style.boxShadow = '0 2px 4px rgba(0, 0, 0, 0.1)';
trackCard.style.width = '150px';
var img = document.createElement('img');
img.style.width = '100px';
img.style.height = '100px';
img.style.borderRadius = '50%';
img.style.objectFit = 'cover';
if (tracks[i].image) {
img.src = tracks[i].image;
} else {
img.src = '';
}
var trackName = document.createElement('div');
trackName.textContent = tracks[i].name;
trackName.style.marginTop = '10px';
trackName.style.fontWeight = 'bold';
trackName.style.color = '#333';
trackCard.appendChild(img);
trackCard.appendChild(trackName);
trackContainer.appendChild(trackCard);
}
}
function manipulateArtists() {
document.querySelector("h1").textContent = "TOP 5 ARTISTS";
document.querySelector("h1").style.marginTop = "2rem";
var artists = JSON.parse(localStorage.getItem("artists"));
var artistContainer = document.getElementById('output_top5');
artistContainer.innerHTML = '';
for (var i = 0; i < 5; i++) {
var artistCard = document.createElement('div');
artistCard.style.display = 'inline-block';
artistCard.style.textAlign = 'center';
artistCard.style.margin = '10px';
artistCard.style.padding = '10px';
artistCard.style.border = '1px solid #ddd';
artistCard.style.borderRadius = '8px';
artistCard.style.boxShadow = '0 2px 4px rgba(0, 0, 0, 0.1)';
artistCard.style.width = '150px';
var img = document.createElement('img');
img.style.width = '100px';
img.style.height = '100px';
img.style.borderRadius = '50%';
img.style.objectFit = 'cover';
if (artists[i].image) {
img.src = artists[i].image;
} else {
img.src = '';
}
var artistName = document.createElement('div');
artistName.textContent = artists[i].name;
artistName.style.marginTop = '10px';
artistName.style.fontWeight = 'bold';
artistName.style.color = '#333';
artistCard.appendChild(img);
artistCard.appendChild(artistName);
artistContainer.appendChild(artistCard);
}
}