-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexfm.js
96 lines (95 loc) · 3.05 KB
/
exfm.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
var exfm = {
name: "Exfm",
search_results_count: 20,
weight: 2,
tooltip_text: 'Ex.fm is a music discovery platform that scans through countless websites, blogs and tumblelogs to find new music; It offers features to find similar artists and song tags, so the associations it picks are likely to make sense.',
search_all: function(query, callback) {
exfm.search_artist(query, callback);
exfm.search_tags(query, callback);
},
search_tags: function(query, callback) {
exfm.search_helper('http://ex.fm/api/v3/trending/tag/' + encodeURIComponent(query) + '?results=20', callback);
},
search_artist: function(url, callback) {
exfm.search_helper('http://ex.fm/api/v3/song/search/' + encodeURIComponent(query) + '?results=20', callback);
},
search_helper: function(url, callback) {
$.getJSON(url, function(data) {
console.log(data);
if(data.songs == undefined || data.songs.length <= 0) {
callback(exfm.name);
return;
}
results = new Array();
lookup_service = get_service('youtube');
$.each(data.songs, function(index, value) {
exfm.build_song(value, lookup_service, index, function(song) {
results.push(song);
});
});
callback(results);
});
},
build_song: function(service_song, lookup_service, index, callback) {
ret = {
key: service_song.artist + "_" + service_song.title,
service: exfm,
service_id: service_song.id,
name: service_song.title,
artist: {
name: service_song.artist
}, embed: {
service: lookup_service,
key: service_song.artist + " " + service_song.title
},
score: exfm.compute_score(service_song.loved_count, index),
tags: service_song.tags,
similar_artists: service_song.similar_artists
};
callback(ret);
},
compute_score: function(loved_count, index) {
if(global_configuration.use_normalized_scores) {
return (exfm.search_results_count - index) * exfm.weight;
} else {
return 10 * (loved_count) * echonest.weight;
}
},
get_similar_artists: function(song, callback) {
if(song.similar_artists) {
callback(song.similar_artists);
return;
} else if(song.service == exfm) {
$.getJSON('http://ex.fm/api/v3/song/' + song.service_id, function(data) {
callback(data.song.similar_artists);
})
return;
} else {
$.getJSON('http://ex.fm/api/v3/song/search/' + escape(song.key) + '?results=1', function(data) {
if(!data.songs || data.songs.length <= 0) {
return;
}
callback(data.songs[0].similar_artists);
});
}
},
get_song_tags: function(song, callback) {
if(song.tags) {
callback(song.tags);
return;
} else if(song.service == exfm) {
$.getJSON('http://ex.fm/api/v3/song/' + song.service_id, function(data) {
callback(data.song.tags);
})
return;
} else {
$.getJSON('http://ex.fm/api/v3/song/search/' + escape(song.key) + '?results=1', function(data) {
if(!data.songs || data.songs.length <= 0) {
return;
}
callback(data.songs[0].tags);
});
}
}
};
available_services['Exfm'] = exfm;