-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
160 lines (149 loc) · 4.25 KB
/
main.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
/*jshint esversion: 6 */
$(document).ready(function() {
"use strict";
$('#results').hide();
var audioObject;
// Artist Querying
$(document).bind('keypress', function(e) {
if(e.keyCode==13){
$('#searchButton').trigger('click');
}
});
$('#searchButton').click(function() {
let query = "https://api.spotify.com/v1/search?q=" + $('#artistField').val() + "&type=artist&limit=25";
$.getJSON(query, function(data) {
let artistsData = data.artists.items;
console.log(data);
let x = [];
let y = [];
var imageInfo = [];
$.each(artistsData, function(index, artist) {
x[index] = artist.name.replace(/\$/g,"S");
y[index] = artist.popularity;
if(artist.images[0] !== undefined &&
artist.id !== undefined &&
artist.name !== undefined){
imageInfo[index] = {
'src': artist.images[0],
'id': artist.id,
'name': artist.name
};
}
});
console.log(imageInfo);
console.log(x);
console.log(y);
var plotSettings = [{
x: x,
y: y,
type: 'bar',
margin: {
l: 50,
r: 50,
b: 150,
t: 50
},
marker: { // marker is an object, valid marker keys: #scatter-marker
color: 'rgb(36, 255, 55)' // more about "marker.color": #scatter-marker-color
},
autosize:true
}];
var layout = {
title: 'Spotify Artist Popularity',
xaxis: {
title: 'Artist',
titlefont: {
family: 'Courier New, monospace',
size: 18,
color: '#7f7f7f'
}
},
yaxis: {
title: 'Popularity',
titlefont: {
family: 'Courier New, monospace',
size: 18,
color: '#7f7f7f'
}
},
margin: {
l: 50,
r: 50,
b: 200,
t: 100
}
};
$('#results').show();
Plotly.newPlot('plot', plotSettings, layout);
$("#previews").html("");
$("#previews").css("display", "-webkit-flex");
$("#previews").css("display", "flex");
$("#previews").css("-webkit-align-items", "center");
$("#previews").css("align-items", "center");
$("#previews").css("-webkit-justify-content", "center");
$("#previews").css("justify-content", "center");
$("#previews").css("-webkit-flex-direction", "row");
$("#previews").css("flex-direction", "row");
$("#previews").css("-webkit-flex-wrap", "wrap");
$("#previews").css("flex-wrap", "wrap");
$("#previews").css("-webkit-flex-flow", "row wrap");
$("#previews").css("flex-flow", "row wrap");
$("#previews").css("-webkit-align-content", "flex-end");
$("#previews").css("align-content", "flex-end");
console.log(imageInfo);
$.each(imageInfo, function(index, image) {
if(image !== undefined &&
image.src !== undefined &&
image.src.url !== undefined &&
image.id !== undefined &&
image.name !== undefined){
var d = $(document.createElement('div'));
var i = $(document.createElement('img'));
var a = $(document.createElement('a'));
var artistPage = 'https://play.spotify.com/artist/' + image.id;
d.css("margin", "20px");
i.attr("id",image.id);
i.addClass('cover');
i.attr('src',image.src.url);
i.attr('width','200px');
a.text(image.name);
a.attr('href',artistPage);
a.css("text-decoration", "none");
a.css("color", "white");
a.css("display", "block");
i.appendTo(d);
a.appendTo(d);
d.appendTo($("#previews"));
}
});
});
});
$(document).on('click', '.cover', function (e) {
//restart audio or start playing audio on click
if(audioObject !== undefined){
audioObject.pause();
}
var id = e.target.id;
console.log(e.target);
if(e.target.id === ""){
id = e.target.parentElement.attributes.id;
}
console.log(e);
console.log(e.target.id);
let query = "https://api.spotify.com/v1/artists/" + e.target.id + "/top-tracks?country=US";
$.getJSON(query,function(data){
console.log(data.tracks[0].preview_url);
audioObject = new Audio(data.tracks[0].preview_url);
audioObject.play();
});
$(".playing").attr("class","cover");
$("#" + e.target.id).attr("class","playing");
});
$(document).on('click', '.playing', function (e) {
if(audioObject !== undefined){
audioObject.pause();
}
console.log(e.target.id);
$("#" + e.target.id).attr('class', 'cover');
});
});