-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmusic-stats.js
225 lines (199 loc) · 7.46 KB
/
music-stats.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
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
// Last.fm music data loading functionality
document.addEventListener('DOMContentLoaded', function() {
// Get all timespan buttons
const timespanButtons = document.querySelectorAll('.timespan-btn');
// Add click event to all timespan buttons
timespanButtons.forEach(button => {
button.addEventListener('click', function() {
// Remove active class from all buttons
timespanButtons.forEach(btn => btn.classList.remove('active'));
// Add active class to clicked button
this.classList.add('active');
// Get selected timespan
const timespan = this.dataset.timespan;
// Load music data for selected timespan
loadMusicData(timespan);
});
});
// Load default timespan (7 days)
loadMusicData('7day');
});
// Enhanced mock data with realistic values
const mockData = {
topArtists: [
{
name: "Radiohead",
playcount: "127",
image: "https://lastfm.freetls.fastly.net/i/u/174s/2a96cbd8b46e442fc41c2b86b821562f.png"
},
{
name: "Daft Punk",
playcount: "98",
image: "https://lastfm.freetls.fastly.net/i/u/174s/2a96cbd8b46e442fc41c2b86b821562f.png"
}
],
topTracks: [
{
name: "Karma Police",
artist: "Radiohead",
playcount: "36",
image: "https://lastfm.freetls.fastly.net/i/u/174s/2a96cbd8b46e442fc41c2b86b821562f.png"
},
{
name: "Get Lucky",
artist: "Daft Punk",
playcount: "29",
image: "https://lastfm.freetls.fastly.net/i/u/174s/2a96cbd8b46e442fc41c2b86b821562f.png"
}
],
topAlbums: [
{
name: "OK Computer",
artist: "Radiohead",
playcount: "87",
image: "https://lastfm.freetls.fastly.net/i/u/174s/2a96cbd8b46e442fc41c2b86b821562f.png"
},
{
name: "Random Access Memories",
artist: "Daft Punk",
playcount: "75",
image: "https://lastfm.freetls.fastly.net/i/u/174s/2a96cbd8b46e442fc41c2b86b821562f.png"
}
]
};
// Update loadMusicData to remove the "last updated" notification
async function loadMusicData(timespan) {
console.log(`Loading music data for timespan: ${timespan}`);
// Show loading state
document.querySelectorAll('.music-box-content').forEach(box => {
box.innerHTML = '<div class="music-item-loading">Loading...</div>';
});
try {
// Try to fetch from the static JSON file
let data = null;
let usedMockData = false;
try {
// Add cache-busting parameter
const timestamp = new Date().getTime();
const url = `/data/lastfm-${timespan}.json?_=${timestamp}`;
console.log(`Fetching from static file: ${url}`);
const response = await fetch(url);
if (!response.ok) {
throw new Error(`Failed to load static data: ${response.status}`);
}
data = await response.json();
console.log('Static data loaded:', data);
// Remove the "last updated" notification code from here
} catch (fetchError) {
console.warn("Failed to fetch from static file, using mock data:", fetchError);
data = mockData;
usedMockData = true;
}
// Display the data
updateMusicDisplay(data);
// Show a note if we used mock data
if (usedMockData) {
document.querySelectorAll('.music-box-content').forEach(box => {
const noteElement = document.createElement('div');
noteElement.className = 'mock-data-note';
noteElement.textContent = 'Using sample data';
noteElement.style.fontSize = '0.7rem';
noteElement.style.fontStyle = 'italic';
noteElement.style.color = '#999';
noteElement.style.marginTop = '8px';
box.appendChild(noteElement);
});
}
} catch (error) {
console.error('Error in loadMusicData:', error);
document.querySelectorAll('.music-box-content').forEach(box => {
box.innerHTML = '<div class="music-item-loading">Error loading data</div>';
});
}
}
// Improved updateMusicDisplay function with cover images
function updateMusicDisplay(data) {
// Format function for number formatting
const formatNumber = (num) => {
return parseInt(num).toLocaleString();
};
// Check if image URL is valid
const isValidImageUrl = (url) => {
if (!url) return false;
if (url.length < 10) return false;
if (url.endsWith('/noimage/noimage.png')) return false;
if (url.includes('2a96cbd8b46e442fc41c2b86b821562f')) return false; // Last.fm default placeholder
return true;
};
// Default image paths
const defaultArtistImage = './assets/default-artist.jpg';
const defaultTrackImage = './assets/default-track.jpg';
const defaultAlbumImage = './assets/default-album.jpg';
// Update top artists box with thumbnails
if (data.topArtists && data.topArtists.length > 0) {
const artistsList = data.topArtists.slice(0, 5).map(artist => {
const imageUrl = isValidImageUrl(artist.image) ? artist.image : defaultArtistImage;
return `
<div class="music-list-item">
<img class="music-thumbnail" src="${imageUrl}" alt="${artist.name}" onerror="this.src='${defaultArtistImage}';">
<div class="music-item-text">
<span class="play-count">${formatNumber(artist.playcount)} plays</span>
<span class="item-separator">-</span>
<span class="item-name">${artist.name}</span>
</div>
</div>
`;
}).join('');
document.getElementById('top-artist-box').querySelector('.music-box-content').innerHTML = `
<div class="music-list">
${artistsList}
</div>
`;
}
// Update top tracks box with thumbnails
if (data.topTracks && data.topTracks.length > 0) {
const tracksList = data.topTracks.slice(0, 5).map(track => {
const imageUrl = isValidImageUrl(track.image) ? track.image : defaultTrackImage;
return `
<div class="music-list-item">
<img class="music-thumbnail" src="${imageUrl}" alt="${track.name}" onerror="this.src='${defaultTrackImage}';">
<div class="music-item-text">
<span class="play-count">${formatNumber(track.playcount)} plays</span>
<span class="item-separator">-</span>
<span class="item-name">${track.name}</span>
<span class="item-separator">-</span>
<span class="item-artist">${track.artist}</span>
</div>
</div>
`;
}).join('');
document.getElementById('top-track-box').querySelector('.music-box-content').innerHTML = `
<div class="music-list">
${tracksList}
</div>
`;
}
// Update top albums box with thumbnails
if (data.topAlbums && data.topAlbums.length > 0) {
const albumsList = data.topAlbums.slice(0, 5).map(album => {
const imageUrl = isValidImageUrl(album.image) ? album.image : defaultAlbumImage;
return `
<div class="music-list-item">
<img class="music-thumbnail" src="${imageUrl}" alt="${album.name}" onerror="this.src='${defaultAlbumImage}';">
<div class="music-item-text">
<span class="play-count">${formatNumber(album.playcount)} plays</span>
<span class="item-separator">-</span>
<span class="item-name">${album.name}</span>
<span class="item-separator">-</span>
<span class="item-artist">${album.artist}</span>
</div>
</div>
`;
}).join('');
document.getElementById('top-album-box').querySelector('.music-box-content').innerHTML = `
<div class="music-list">
${albumsList}
</div>
`;
}
}