-
Notifications
You must be signed in to change notification settings - Fork 9
/
index.html
185 lines (155 loc) · 4.29 KB
/
index.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
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
177
178
179
180
181
182
183
184
185
<html>
<head>
<title> Deezer Demo </title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
<link href='http://fonts.googleapis.com/css?family=Open+Sans' rel='stylesheet' type='text/css'>
<style type="text/css">
body {
font-family: 'Open Sans', sans-serif;
}
#header {
margin-top:20px;
text-align:center;
width: 100%;
}
#footer {
margin-top:30px;
text-align:center;
width:100%;
font-size:10px;
}
.sdiv {
margin:30px;
}
.timg {
margin-right: 20px;
width:120px;
height:120px;
}
.title {
font-weight: bold;
color: green;
margin-bottom: 6px;
}
#playlist {
width:500px;
margin-left:auto;
margin-right:auto;
}
#info {
width:80%;
text-align:center;
height: 14px;
margin-left:auto;
margin-right:auto;
margin-top:20px;
}
.player {
margin-top:20px;
}
</style>
</head>
<body>
<div id='header'>
<h1> The Echo Nest + Deezer Playlist Demo</h1>
Enter an artist: <input id='artist-name' type='text'>
</div>
<div id="info"> </div>
<div id='playlist'> </div>
<div id='footer'>
Powered by The Echo Nest and Deezer. See <a href='http://blog.echonest.com'> The Echo Nest blog</a> for more info.
</div>
<script type="text/javascript">
var DEEZER_DEMO_API_KEY='V3WFT6DHG0RJX4UD2';
var audio
jQuery.ajaxSettings.traditional = true;
function createSongDiv(song) {
var songDiv = $("<div class='sdiv' id=" + song.id + "-div>");
return songDiv;
}
function addSongs(songs) {
var playlist = $("#playlist");
for (var i = 0; i < songs.length; i++) {
var div = createSongDiv(songs[i]);
playlist.append(div);
fetchDeezerTrack(songs[i], div);
}
}
function fetchDeezerTrack(song, div) {
if (song.tracks.length > 0) {
var tid = song.tracks[0].foreign_id.split(':')[2];
var url = 'http://api.deezer.com/2.0/track/' + tid + '?callback=?'
jQuery.getJSON(url, { output:'jsonp'},
function(data) {
var link = $("<a target='deezer'>").attr('href', data.link);
var cover = $("<img class='timg'>").attr('src', data.album.cover).attr("style", "float:left");
link.append(cover);
div.append(link);
var tdiv = $("<div class='tdiv'>");
tdiv.append( $("<div class='title'>").text(data.title));
tdiv.append( $("<div>").text(data.album.title));
tdiv.append( $("<div>").text(data.artist.name));
div.append(tdiv);
div.append(createPlayer(data.preview));
div.append($('<br clear="left">'));
}
);
}
}
function createPlayButton(audio) {
var button = $("<button>").text("preview");
button.click(
function() {
alert("Playing " + audio);
}
);
return button;
}
function createPlayer(audio) {
var player = $("<audio class='player' preload='none' controls='controls'>").attr("src", audio);
return player;
}
function info(msg) {
$("#info").text(msg);
}
function fetchPlaylist(artist) {
info("Getting playlist for " + artist);
$("#playlist").empty();
var url = 'http://developer.echonest.com/api/v4/playlist/static?callback=?'
jQuery.getJSON(url,
{ artist:artist,
type:'artist-radio',
format:'jsonp',
bucket: ['id:deezer', 'tracks'],
limit: true,
'api_key' : DEEZER_DEMO_API_KEY
},
function(data) {
info("");
if (data.response.status.code == 0) {
var songs = data.response.songs;
addSongs(songs);
} else {
info("Can't create a playlist for " + artist);
}
}
);
}
function initUI() {
$("#artist-name").keypress(
function(e) {
if(e.which == 13) {
var artist = $("#artist-name").val();
fetchPlaylist(artist);
}
}
);
}
$(document).ready(
function() {
initUI();
}
);
</script>
</body>
</html>