source: gs3-extensions/web-audio/trunk/js-mad/jsmad-master/officialfm.js@ 28388

Last change on this file since 28388 was 28388, checked in by davidb, 11 years ago

Set of JS, CSS, PNG etc web resources to support a mixture of audio player/document display capabilities

File size: 13.8 KB
Line 
1if(typeof jQuery == 'undefined') {
2 alert('jQuery must be loaded for officialfm-javascript to work.');
3}
4
5function OfficialFM(api_key, preload_player) {
6 this.api_key = api_key;
7
8 if(preload_player) OfficialFM.is_player_loaded();
9}
10
11OfficialFM.VERSION = '0.0.1';
12OfficialFM.API_URL = 'http://api.official.fm/';
13
14OfficialFM.LOADING_TRIES_INTERVAL = 250; /* in ms */
15OfficialFM.MAX_LOADING_TRIES = 30000 / OfficialFM.LOADING_TRIES_INTERVAL; /* try loading for 30 seconds */
16
17OfficialFM.player_loading = false;
18OfficialFM.loading_tries = 0;
19
20OfficialFM.is_player_loaded = function () {
21 if (typeof OfficialFM.Player == "undefined") {
22 if (!OfficialFM.player_loading) {
23 OfficialFM.player_loading = true;
24 $.getScript("https://github.com/officialfm/officialfm-javascript/raw/master/player_api.js");
25 }
26 return false;
27 }
28 return true;
29}
30
31OfficialFM.with_player = function (callback) {
32 if(!callback) return;
33
34 if(OfficialFM.is_player_loaded()) {
35 callback();
36 } else {
37 if(OfficialFM.loading_tries < OfficialFM.MAX_LOADING_TRIES) {
38 OfficialFM.loading_tries++;
39 setTimeout(function () {
40 OfficialFM.with_player(callback);
41 }, OfficialFM.LOADING_TRIES_INTERVAL);
42 }
43 }
44}
45
46/**
47 * Internal function used to query the server easily
48 */
49OfficialFM.prototype.call_api = function(sub_url, callback, other_params) {
50 var default_params = {
51 key: this.api_key,
52 format: 'json'
53 }
54 var params = default_params;
55 for (attrname in other_params) {
56 if(attrname == 'limit') {
57 params['api_max_responses'] = other_params[attrname];
58 } else if(attrname == 'embed') {
59 params['api_embed_codes'] = other_params[attrname];
60 } else {
61 params[attrname] = other_params[attrname];
62 }
63 }
64
65 var url = OfficialFM.API_URL + sub_url;
66 $.ajax({
67 url: url,
68 data: params,
69 dataType: 'jsonp',
70 success: callback
71 });
72}
73
74/**
75 * Create an Official.fm player
76 *
77 * Valid options:
78 * - container_id : string : id of the container. ex : 'player_container'
79 * - type : string : content type : track/playlist (todo : user, dynamic tracklist..)
80 * - id : content id : track/playlist's id
81 * - aspect : optional. string : aspect of player : [large], standard, artwork, small, mini. ex : 'artwork'
82 * - skin : optional. number : skin id (null = default skin). ex : 123
83 * - width : optional. string or number : width of player in pixel - or percentage when used with %. (valid only for artwork & small players). ex : '300' or '60%'
84 * - onReady : listener with no param. Called when player is ready to play.
85 * - onPlay : listener with 1 param track_id. Called when a track starts playing
86 * - onPause : listener with 1 param track_id. Called when a track is being paused
87 * - onProgress : listener with 1 param object {played_seconds, played_percent, total}. Called twice per second minimum.
88 * - onChangeTrack : listener with 1 param track_id. Called when player switch to another track
89 * - onChangeTracklist : listener with no param. Called when player switch to another tracklist
90 * - onTracklistEnd : listener with no param. Called when tracklist's end is reached.
91 *
92 * Subsequent calls to player() with the same div_id will replace
93 * previous instances of the player hosted in the given div
94 *
95 */
96OfficialFM.prototype.player = function (options, callback) {
97 if(!options.hasOwnProperty('container_id')) {
98 options.container_id = 'player_container';
99 }
100
101 if(!options.hasOwnProperty('type')) {
102 options.type = 'track';
103 }
104
105 if(!options.hasOwnProperty('aspect')) {
106 options.aspect = 'small';
107 }
108
109 OfficialFM.with_player(function() {
110 var _player = OfficialFM.Player.create(options);
111 /* Hack to add play_track to a player */
112 _player.play_track = function(track_id) {
113 this.play(track_id, OfficialFM.Player.build_feed('track', track_id));
114 }
115 callback(_player);
116 });
117}
118
119/**
120 * Create an Official.fm track player
121 *
122 * @param div_id The ID of the div the player will be put in
123 * @param track_id content id : The ID of the track that should be played
124 * @param options :
125 * - callback: function taking the player object as an argument
126 * - all other options valid in OfficialFM.player()
127 *
128 * Subsequent calls to track_player() with the same div_id will replace
129 * previous instances of the player hosted in the given div
130 *
131 * Example:
132 * ofm.track_player('player_div', 226556)
133 */
134OfficialFM.prototype.track_player = function (div_id, track_id, options) {
135 if(!options) options = {};
136 options.container_id = div_id;
137 options.type = 'track';
138 options.id = track_id;
139 if(!options.hasOwnProperty('callback')) options.callback = function() {};
140
141 this.player(options, options.callback);
142}
143
144/**
145 * Create an Official.fm playlist player
146 *
147 * @param div_id The ID of the div the player will be put in
148 * @param track_id content id : The ID of the track that should be played
149 * @param options :
150 * - callback: function taking the player object as an argument
151 * - all other options valid in OfficialFM.player()
152 *
153 * Subsequent calls to playlist_player() with the same div_id will replace
154 * previous instances of the player hosted in the given div
155 */
156OfficialFM.prototype.playlist_player = function (div_id, playlist_id, options) {
157 if(!options) options = {};
158 options.container_id = div_id;
159 options.type = 'playlist';
160 options.id = playlist_id;
161 if(!options.hasOwnProperty('callback')) options.callback = function() {};
162
163 this.player(options, options.callback);
164}
165
166/* ==================== users functions ===================== */
167
168/**
169 * Search for users
170 *
171 * @param string search_param: a search parameter (eg + name of the user)
172 * @param int limit (50) limit per page
173 * @User list
174 */
175OfficialFM.prototype.users = function (search_term, callback, options) {
176 this.call_api('search/users/' + encodeURI(search_term), callback, options);
177}
178
179OfficialFM.prototype.each_user = function (search_term, callback, options) {
180 this.users(search_term, function(users) { $.each(users, callback) }, options);
181}
182
183/**
184 * Retrieve information about a specific user
185 *
186 * @param string user_id: id or login
187 * @a User object
188 */
189OfficialFM.prototype.user = function (user_id, callback, options) {
190 this.call_api('user/' + user_id, function(data) { callback(data[0]); }, {});
191}
192
193/**
194 * Retrieve a list of the tracks of this user
195 *
196 * @param string user_id: id or login
197 * @param integer limit (50) limit per page
198 * @param bool embed (false) should embed codes be included in the response
199 * @array Track list
200 */
201OfficialFM.prototype.user_tracks = function (user_id, callback, options) {
202 this.call_api('user/' + user_id + '/tracks', callback, options);
203}
204
205OfficialFM.prototype.each_user_track = function (search_term, callback, options) {
206 this.user_tracks(search_term, function(user_tracks) { $.each(user_tracks, callback) }, options);
207}
208
209/**
210* Retrieve a list of the playlists of this user
211*
212* @param string user_id: id or login
213* @param integer limit (50) limit per page
214* @param bool embed (false) should embed codes be included in the response
215* @array Playlist list
216*/
217OfficialFM.prototype.user_playlists = function (user_id, callback, options) {
218 this.call_api('user/' + user_id + '/playlists', callback, options);
219}
220
221OfficialFM.prototype.each_user_playlists = function (search_term, callback, options) {
222 this.user_playlists(search_term, function(user_playlists) { $.each(user_playlists, callback) }, options);
223}
224
225/**
226 * Retrieve a list of the subscribers of this user
227 *
228 * @param string user_id: id or login
229 * @param integer limit (50) limit per page
230 * @array User list
231 */
232OfficialFM.prototype.user_subscribers = function (user_id, callback, options) {
233 this.call_api('user/' + user_id + '/subscribers', callback, options);
234}
235
236OfficialFM.prototype.each_user_subscribers = function (search_term, callback, options) {
237 this.user_subscribers(search_term, function(user_subscribers) { $.each(user_subscribers, callback) }, options);
238}
239
240/**
241 * Retrieve a list of the subscriptions of this user
242 *
243 * @param string user_id: id or login
244 * @param integer limit (50) limit per page
245 * @array User list
246 */
247OfficialFM.prototype.user_subscriptions = function (user_id, callback, options) {
248 this.call_api('user/' + user_id + '/subscriptions', callback, options);
249}
250
251OfficialFM.prototype.each_user_subscriptions = function (search_term, callback, options) {
252 this.user_subscriptions(search_term, function(user_subscriptions) { $.each(user_subscriptions, callback) }, options);
253}
254
255/**
256 * Retrieve a list of the contacts of this user
257 *
258 * @param string user_id: id or login
259 * @param integer limit (50) limit per page
260 * @param bool embed (false) should embed codes be included in the response
261 * @array User list
262 */
263OfficialFM.prototype.user_contacts = function (user_id, callback, options) {
264 this.call_api('user/' + user_id + '/contacts', callback, options);
265}
266
267OfficialFM.prototype.each_user_contacts = function (search_term, callback, options) {
268 this.user_contacts(search_term, function(user_contacts) { $.each(user_contacts, callback) }, options);
269}
270
271/* ==================== tracks functions ===================== */
272
273/**
274 * Search for tracks
275 *
276 * @param string search_param: a search parameter (eg + name of the track)
277 * @param integer limit (50) limit per page (optional)
278 * @array Track list
279 */
280OfficialFM.prototype.tracks = function (search_term, callback, options) {
281 this.call_api('search/tracks/' + encodeURI(search_term), callback, options);
282}
283
284OfficialFM.prototype.each_track = function (search_term, callback, options) {
285 this.tracks(search_term, function(tracks) { $.each(tracks, callback) }, options);
286}
287
288/**
289 * Retrieve information about a specific track
290 *
291 * Note: http://official + fm/developers/simple_api#track_show
292 * says that api_max_responses is a valid parameter + Why escapes me +
293 *
294 * @param string track_id: id
295 * @param bool embed (false) should embed codes be included in the response
296 * @array Track
297 */
298OfficialFM.prototype.track = function (track_id, callback, options) {
299 this.call_api('track/' + track_id, function(data) { callback(data[0]); }, options);
300}
301
302/**
303 * Retrieve users that have voted for this track
304 *
305 * @param string track_id: id
306 * @param integer limit (50) limit per page
307 * @array User list
308 */
309OfficialFM.prototype.track_votes = function (track_id, callback, options) {
310 this.call_api('track/' + track_id + '/votes', callback, options);
311}
312
313OfficialFM.prototype.each_track_vote = function (search_term, callback, options) {
314 this.track_votes(search_term, function(track_votes) { $.each(track_votes, callback) }, options);
315}
316
317/**
318 * Retrieve 200 tracks of selected chart
319 *
320 * @param string charting: 'today', 'week', 'month', 'year' or 'all_time'
321 * @param string genre: Genre string ('Electronic', 'Rock', 'Jazz', ...) (optional)
322 * @param string country: ISO country id (CH, FR, UK) (optional)
323 * @param bool embed (false) should embed codes be included in the response (optional)
324 * @param integer limit (200) limit per page (optional)
325 * @array Track list
326 */
327OfficialFM.prototype.charts = function (charting, callback, options) {
328 this.call_api('tracks/charts', callback, $.extend({}, options, { charting: charting }));
329}
330
331OfficialFM.prototype.each_chart = function (search_term, callback, options) {
332 this.charts(search_term, function(charts) { $.each(charts, callback) }, options);
333}
334
335/**
336 * Retrieve 200 latest tracks
337 *
338 * @param string genre: Genre string (Electronic, Rock, Jazz, + + + ) (optional)
339 * @param string country: ISO country id (CH, FR, UK) (optional)
340 * @param bool embed (false) should embed codes be included in the response (optional)
341 * @param integer limit (200) limit per page (optional)
342 * @array Track list
343 */
344OfficialFM.prototype.latest = function (callback, options) {
345 this.call_api('tracks/latest', callback, options);
346}
347
348OfficialFM.prototype.each_latest = function (callback, options) {
349 this.latest(function(latests) { $.each(latests, callback) }, options);
350}
351
352/* ==================== playlists functions ===================== */
353
354/**
355 * Search for playlists
356 *
357 * @param string search_param: a search parameter (eg + name of the playlist)
358 * @param integer limit (50) limit per page (optional)
359 * @array Playlist list
360 */
361 OfficialFM.prototype.playlists = function (search_param, callback, options) {
362 this.call_api('search/playlists/' + encodeURI(search_param), function(result) {
363 callback($.map(result, OfficialFM.improve_playlist));
364 }, options);
365 }
366
367 OfficialFM.prototype.each_playlist = function (search_term, callback, options) {
368 this.playlists(search_term, function(playlists) { $.each(playlists, callback) }, options);
369 }
370
371/**
372 * Retrieve information about a specific playlist
373 *
374 * @param string playlist_id: id
375 * @param bool embed (false) should embed codes be included in the response
376 * @array Playlist
377 */
378 OfficialFM.prototype.playlist = function (playlist_id, callback, options) {
379 this.call_api('playlist/' + playlist_id, function(result) {
380 callback(OfficialFM.improve_playlist(result[0]));
381 }, options);
382 }
383
384/**
385 * Retrieve users that have voted for this playlist
386 *
387 * @param string playlist_id: id
388 * @param integer limit (50) limit per page
389 * @array User list
390 */
391 OfficialFM.prototype.playlist_votes = function (playlist_id, callback, options) {
392 this.call_api('playlist/' + playlist_id + '/votes', callback, options);
393 }
394
395 OfficialFM.prototype.each_playlist_vote = function (search_term, callback, options) {
396 this.playlist_votes(search_term, function(playlist_votes) { $.each(playlist_votes, callback) }, options);
397 }
398
399 /* Hack to improve playlist id lists (see issue #4 in sandbox-api) */
400 OfficialFM.improve_playlist = function (playlist) {
401 playlist.running_time = playlist['length'];
402 /* TODO: actually improve the item list ha*/
403 return playlist;
404 }
405
Note: See TracBrowser for help on using the repository browser.