-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathappSpotifyLogic.js
183 lines (142 loc) · 5.67 KB
/
appSpotifyLogic.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
var spotifyApi = new SpotifyWebApi();
const clientId = 'e95a863a8a7f4244b9e7981c519014aa';
const clientSecret = 'a96759e036bd4659908aeaa9fe454234';
// Get a token
const getToken = async () => {
const result = await fetch('https://accounts.spotify.com/api/token', {
method: 'POST',
headers: {
'Content-Type' : 'application/x-www-form-urlencoded',
'Authorization' : 'Basic ' + btoa(clientId + ':' + clientSecret)
},
body: 'grant_type=client_credentials'
});
const data = await result.json();
console.log(data.access_token)
spotifyApi.setAccessToken(data.access_token);
return data.access_token;
}
getToken();
// https://github.com/vasturiano/force-graph/blob/master/example/dynamic/index.html
var artistSearchResultName;
var artistSearchResultGenre;
// Search and return a single artist, with artist info
// function returnArtist(searchArtist){
// spotifyApi.getGeneric('https://api.spotify.com/v1/search?q=artist:' + searchArtist + '&type=artist&limit=1').then(
// function (data) {
// //console.log(searchArtist);
// artistSearchResultName = data.artists.items[0].name;
// artistSearchResultGenre = data.artists.items[0].genres[0];
// //console.log("Artist name is "+artistSearchResultName);
// console.log(artistSearchResultGenre);
// })
//}
function addNodes(inArtist, searchArtist){
const { nodes, links } = Graph.graphData();
// Check if the artist is already in node list
const checkUsername = obj => obj.id === inArtist;
exists = Graph.graphData().nodes.some(checkUsername)
// If it's not in the list,
if (exists == false){
//get artist genre
//returnArtist(inArtist);
//add node to nodelist
//console.log(inArtist + " not here yet");
//console.log(artistSearchResultGenre);
let outNode = {"id": inArtist, "genre": artistSearchResultGenre, "size": 1}
Graph.graphData({
nodes: [outNode, ...nodes],
links: [...links],
});
} else {
// console.log(inArtist + " already here");
}
}
function addLinks(inArtist, searchArtist){
const { nodes, links } = Graph.graphData();
if (inArtist != searchArtist){
let outLink = {"source": searchArtist, "target": inArtist};
// Increase the size of each node in the link
Graph.graphData().nodes.find(x => x.id === inArtist).size +=0.01
Graph.graphData().nodes.find(x => x.id === searchArtist).size +=0.01
//console.log(result2);
Graph.graphData({
nodes: [...nodes],
links: [...links, outLink]
});
}
}
function searchForArtist(searchArtist){
// Based on the searchArtist, look to see if we already have that node in the graphData
var searchArtistNode = Graph.graphData().nodes.find(node => node.id === searchArtist);
// If we do, and the node's searched property exists, skip the following, otherwise, continue
if (searchArtistNode === undefined || !searchArtistNode.hasOwnProperty('searched')){
// Mark this searchedArtist's 'searched' property as true, so that we can skip it if the user tries to search it again
// search tracks whose Artist is 'searchArtist'
spotifyApi.getGeneric('https://api.spotify.com/v1/search?q=artist:' + searchArtist + '&type=track&limit=50').then(
function (data) {
let output = data;
// console.log('Artist is ' + searchArtist, output.tracks.items);
for (let track in output.tracks.items){
artists = output.tracks.items[track].artists
// console.log(artists);
for (let artist in data.tracks.items[track].artists){
var out = data.tracks.items[track].artists[artist].name;
// console.log(out);
addNodes(out, searchArtist);
addLinks(out, searchArtist);
}
searchArtistNode = Graph.graphData().nodes.find(node => node.id === searchArtist);
searchArtistNode.searched = true;
}
},
function (err) {
console.error(err);
}
);
// Track has the search artist in the title
spotifyApi.getGeneric('https://api.spotify.com/v1/search?q=track:' + searchArtist + '&type=track&limit=50').then(
function (data) {
let output = data;
console.log(output);
// console.log('Artist is ' + searchArtist, output.tracks.items);
for (let track in output.tracks.items){
// Check first that at least ONE artist is search artist
var ok = false;
for (let artist in data.tracks.items[track].artists){
if (output.tracks.items[track].artists[artist].name == searchArtist){
ok = true;
}
}
// If the previous test is positive:
if (ok == true){
artists = output.tracks.items[track].artists
//console.log(artists);
for (let artist in data.tracks.items[track].artists){
var out = data.tracks.items[track].artists[artist].name;
// console.log(out);
addNodes(out, searchArtist);
addLinks(out, searchArtist);
}
}
}
},
function (err) {
console.error(err);
}
);
Graph.graphData().nodes.sort((a, b) => (a.size > b.size) ? 1 : -1);
}
else(searchArtistNode => {
// Center/zoom on node
Graph.centerAt(searchArtistNode.x, searchArtistNode.y, 1000);
Graph.zoom(8, 2000);
});
}
// console.log('New try below');
// // Get top tracks for Bon Iver
// spotifyApi.getArtistTopTracks('4LEiUm1SRbFMgfqnQTwUbQ', "US").then(
// function (data) {
// console.log('Bon Iver Artist ID', data);
// }
// );