-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
music.html
92 lines (79 loc) · 3.62 KB
/
music.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>music</title>
<link rel="stylesheet" href="/assets/css/mainSite.css">
<script type="text/javascript" src="https://unpkg.com/album-art"></script>
<link rel="shortcut icon" href="/assets/imgs/favicon.png" type="image/png">
</head>
<body style="margin-left: 15px; margin-right: 15px; margin-top: 15px;">
<h2>This page is experimental.</h2>
<p>This page does not currently use TransSocial's systems and is only here for developer testing for TransMusic and TransSocial's music sharing feature.</p>
<p>If you're here by accident, you can go back to the home page, this feature is available.</p>
<a href="/home"><button>Go Back to Home</button></a>
<br />
<br />
<br />
<p>If you aren't here by accident or are just curious, search for a track:</p>
<input type="text" placeholder="Search for a song" id="songQuery" />
<button onclick="displayTracks(document.getElementById('songQuery').value)">Search</button>
<br />
<br />
<div id="spotifyPlayer">
<p>Try searching for a song!</p>
</div>
<script>
// spotify api
// get access token
async function getAccessToken() {
const response = await fetch("https://accounts.spotify.com/api/token", {
method: "POST",
headers: {
"Authorization": "Basic " + btoa("c8ddbc039e5a4f0fb786812d601803dd:52c2a0dbd736474c8c5eb46a61a0e61c"),
"Content-Type": "application/x-www-form-urlencoded"
},
body: new URLSearchParams({
"grant_type": "client_credentials"
})
});
const data = await response.json();
return data.access_token;
}
// search for spotify tracks
async function searchTracks(query) {
const accessToken = await getAccessToken();
const response = await fetch(`https://api.spotify.com/v1/search?q=${encodeURIComponent(query)}&type=track&limit=10`, {
headers: {
"Authorization": `Bearer ${accessToken}`
}
});
const data = await response.json();
console.log(data);
return data.tracks.items;
}
// display music
async function displayTracks(query) {
const tracks = await searchTracks(query);
const resultsContainer = document.getElementById("spotifyPlayer");
resultsContainer.innerHTML = "";
if (tracks.length > 0) {
tracks.forEach(track => {
// create iframe to embed song
const embed = document.createElement("iframe");
embed.src = `https://open.spotify.com/embed/track/${track.id}`;
embed.width = "500";
embed.height = "100";
embed.frameBorder = "0";
embed.allowTransparency = "true";
embed.allow = "encrypted-media";
resultsContainer.appendChild(embed);
});
} else {
console.log("No tracks found.");
}
}
</script>
</body>
</html>