-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
109 lines (89 loc) · 3.11 KB
/
index.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
Sentry.init({
dsn: "https://[email protected]/5706408",
// this assumes your build process sets "npm_package_version" in the env
release: "fresh-beats@",
integrations: [new Sentry.Integrations.BrowserTracing()],
// We recommend adjusting this value in production, or using tracesSampler
// for finer control
tracesSampleRate: 1.0,
});
console.log('SENTRY')
let clientId = 'a25e51780f7f86af0afa91f241d091f8';
let soundCloudURL = 'https://api.soundcloud.com/tracks/';
function todaysDateFormatted() {
let today = new Date();
return `${today.getFullYear().toString().padStart(4, '0')}-${(today.getMonth()+1).toString().padStart(2, '0')}-${today.getDate().toString().padStart(2, '0')}+00:00:00`;
// because less than 2 characters means 1 character... and everything under 10 (1 to 9) is 1 character, so we'll have 01, 02, ..., 09
}
function getTrack(selectedGenre) {
const params = {
'client_id': clientId,
'genres': selectedGenre,
'created_at[from]': todaysDateFormatted(),
'limit': 100
};
const queryString = formatQueryParams(params);
const url = soundCloudURL + '?' + queryString;
$.ajax({
type : "GET",
url: url,
dataType: "json"
})
.done(function(response){
// console.log(response)
displayTrack(response);
})
.fail(function(jqXHR, textStatus){
console.log(textStatus);
console.log(jqXHR);
$('#error-msg').text(`Something went wrong: ${txtStatus}`);
})
}
function formatQueryParams(params) {
const queryItems = Object.keys(params)
.map(key => `${encodeURIComponent(key)}=${encodeURIComponent(params[key])}`)
return queryItems.join('&')
.replace('%5B', '[')
.replace('%5D', ']')
.replace('%2B', '+')
.replace('%3A', ':')
.replace('%3A', ':');
}
function getPlayerIframe(randomTrackURL) {
fetch(`https://soundcloud.com/oembed?url=${randomTrackURL}&maxheight=150&format=json`)
.then(response => {
if (response.ok) {
return response.json()
}
throw new Error(response.statusText)
})
.then(responseJson => injectIframe(responseJson))
.catch(err => {
$('#error-msg').text(`Something went wrong: ${err.message}`);
})
}
function displayTrack(response) {
$('.results').empty();
// We only want to display 10 tracks
// The API returns the same 10 songs eveytime so we increase what is returned and pull 10 at random from the response
for (let i = 0; i < 10; i++) {
getPlayerIframe(response[Math.floor(Math.random() * response.length)].permalink_url);
}
}
function injectIframe(response) {
// console.log(trackTitle);
$('.results').append(
`<div class="track-container">
<div class="track-info">${response.title}</div>
<div class="sc-iframe">${response.html}</div>
</div>`
);
}
function watchForm() {
$('form').submit(e => {
e.preventDefault();
let selectedGenre = $("#genres :selected").val();
getTrack(selectedGenre);
})
}
$(watchForm);