-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
executable file
·89 lines (53 loc) · 2.02 KB
/
main.js
File metadata and controls
executable file
·89 lines (53 loc) · 2.02 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
// Element im working with
let display = document.querySelector('#display');
let textValue = document.querySelector('#TextValue');
let searchLogo = document.querySelector('.searchLogo');
let token = '?client_id=095fe1dcd09eb3d0e1d3d89c76f5618f&q=';
// key down event for enter, pass down ID value
textValue.addEventListener('keydown', function(event){
if(event.keyCode === 13){
display.textContent = ""
event.preventDefault();
return(getInfo(event.target.value));
}
});
// fetch API pass down artist event and stores users ID
function getInfo(event){
fetch(`http://api.soundcloud.com/users/${token}${event}`) //API address used to grab info
.then( function(response) {
return response.json() //returns API in Json format
}).then(function(data){
let userID = data[0].id;
TrackInfo(userID);
});
};
// Another Fetch request that uses the ID to search for unique track
function TrackInfo(userID){
fetch(`http://api.soundcloud.com/users/${userID}/tracks/${token}`)
.then( function(response){
return response.json()
})
.then( function(data){
moreInfo(data)
})
}
// fill in dynamic html song-cards with fetch values
function moreInfo(data){
displayResults="";
for(i=0; i<data.length; i++){
let id = data[i].id
displayResults += `
<div class="w3-card-4" type="click" value=${id} id=${id} onclick=audio(${id})>
<img class="albumArt" data-song-link="${data[i].stream_url}" src="${data[i].artwork_url}"/>
<p> <b>Title: </b> ${data[i].title}</p>
<p> <b>Artist: </b> ${data[i].user.username}</p>
</div>
`
}
display.insertAdjacentHTML('afterbegin', displayResults);
}
// querySelect audio tag give src attribute that adds + stream link API
function audio(id){
let audioSource = document.querySelector("audio");
audioSource.src = "https://api.soundcloud.com/tracks/"+id+"/stream?client_id=095fe1dcd09eb3d0e1d3d89c76f5618f"
}