-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
180 lines (154 loc) · 7.46 KB
/
index.html
File metadata and controls
180 lines (154 loc) · 7.46 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Spotify Search</title>
<link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Roboto:wght@400;700&display=swap">
<link rel="stylesheet" href="styles.css">
</head>
<body>
<h1>Spotify Search</h1>
<input type="text" id="searchQuery" placeholder="Search for a track or playlist..." />
<button onclick="searchSpotify()">Search</button>
<div class="results">
<div class="playlist-container">
<h2>Playlists</h2>
<div id="playlistResults"></div>
<button class="load-more hidden" id="loadMorePlaylists" onclick="loadMorePlaylists()">Load More Playlists</button>
</div>
<!-- First Featured Playlist -->
<div class="additional-container">
<div class="additional-content">
<h2>Featured Playlist 1</h2>
<iframe
title="Spotify Embed: Featured Playlist 1"
src="https://open.spotify.com/embed/playlist/3If2VnN90qpgIcS1d3Nh4D?utm_source=generator&theme=0"
class="playlist-embed"
allow="autoplay; clipboard-write; encrypted-media; fullscreen; picture-in-picture"
loading="lazy"
></iframe>
</div>
</div>
<!-- Second Featured Playlist -->
<div class="additional-container">
<div class="additional-content">
<h2>Featured Playlist 2</h2>
<iframe
title="Spotify Embed: Featured Playlist 2"
src="https://open.spotify.com/embed/playlist/37i9dQZF1EpsJyMNLO4m7f?utm_source=generator&theme=0"
class="playlist-embed"
allow="autoplay; clipboard-write; encrypted-media; fullscreen; picture-in-picture"
loading="lazy"
></iframe>
</div>
</div>
<div class="track-container">
<h2>Tracks</h2>
<div id="trackResults"></div>
<button class="load-more hidden" id="loadMoreTracks" onclick="loadMoreTracks()">Load More Tracks</button>
</div>
</div>
<script src="script.js">
const token ='BQATncdm3LCNiKW_NyCfWw24rwBkU1bYztsX8x1EAWk7Y-cRqrUlE1rpgaIBj8HgEbpysOmAcXuX1wFfhlGXNqWJaZLSkwSFUExHM6cRzivPpTaJomuae6QvEmpqUWYw4l9yHeb2Joatb4iA8tanCioSNwtQwfODHsAMzDPTHk1eR59wMeXj2wiiEk6wISJRQmVDvGGvA8Rmp7XgcZ0R4OSRLwTO5JfF4ABIKgFXmWqg1JBKEZVm4GqK0USVKeZk_jrTPjhDexiij9qu06i5NYTw6PlmbItg5jmeLYUSgPU685sSVSrEogwl3rA2fmW8IdnZ1g';
let trackOffset = 0;
let playlistOffset = 0;
function searchSpotify() {
const query = document.getElementById('searchQuery').value;
if (query.trim() === '') return;
// Hide load more buttons initially
document.getElementById('loadMoreTracks').classList.add('hidden');
document.getElementById('loadMorePlaylists').classList.add('hidden');
console.log("Searching for:", query);
// Fetch initial results
fetchResults(query, 0, 0, 6, 6);
}
function fetchResults(query, trackOffset, playlistOffset, trackLimit, playlistLimit) {
// Fetch tracks
fetch(`https://api.spotify.com/v1/search?q=${encodeURIComponent(query)}&type=track&offset=${trackOffset}&limit=${trackLimit}`, {
headers: {
'Authorization': `Bearer ${accessToken}`
}
})
.then(response => {
if (!response.ok) {
throw new Error(`HTTP error! Status: ${response.status}`);
}
return response.json();
})
.then(data => {
console.log("Tracks data:", data); // Debug log
const trackResultsDiv = document.getElementById('trackResults');
const tracks = data.tracks.items;
if (trackOffset === 0) {
trackResultsDiv.innerHTML = '';
}
tracks.forEach(track => {
const trackDiv = document.createElement('div');
trackDiv.className = 'track';
const iframe = document.createElement('iframe');
iframe.src = `https://open.spotify.com/embed/track/${track.id}`;
iframe.width = "300";
iframe.height = "80"; // Default height
iframe.frameBorder = "0";
iframe.allow = "encrypted-media";
trackDiv.appendChild(iframe);
trackResultsDiv.appendChild(trackDiv);
});
// Show load more button if there are more tracks
if (data.tracks.next) {
document.getElementById('loadMoreTracks').classList.remove('hidden');
}
// Store the offset for the next page of tracks
window.trackOffset = trackOffset + trackLimit;
})
.catch(error => console.error('Error fetching tracks:', error));
// Fetch playlists
fetch(`https://api.spotify.com/v1/search?q=${encodeURIComponent(query)}&type=playlist&offset=${playlistOffset}&limit=${playlistLimit}`, {
headers: {
'Authorization': `Bearer ${accessToken}`
}
})
.then(response => {
if (!response.ok) {
throw new Error(`HTTP error! Status: ${response.status}`);
}
return response.json();
})
.then(data => {
console.log("Playlists data:", data); // Debug log
const playlistResultsDiv = document.getElementById('playlistResults');
const playlists = data.playlists.items;
if (playlistOffset === 0) {
playlistResultsDiv.innerHTML = '';
}
playlists.forEach(playlist => {
const iframe = document.createElement('iframe');
iframe.src = `https://open.spotify.com/embed/playlist/${playlist.id}`;
iframe.width = "300";
iframe.height = "380";
iframe.frameBorder = "0";
iframe.allow = "encrypted-media";
iframe.className = "playlist";
playlistResultsDiv.appendChild(iframe);
});
// Show load more button if there are more playlists
if (data.playlists.next) {
document.getElementById('loadMorePlaylists').classList.remove('hidden');
}
// Store the offset for the next page of playlists
window.playlistOffset = playlistOffset + playlistLimit;
})
.catch(error => console.error('Error fetching playlists:', error));
}
function loadMoreTracks() {
const query = document.getElementById('searchQuery').value;
fetchResults(query, window.trackOffset, window.playlistOffset, 3, 0);
}
function loadMorePlaylists() {
const query = document.getElementById('searchQuery').value;
fetchResults(query, window.trackOffset, window.playlistOffset, 0, 3);
}
</script>
</body>
</html>