Skip to content

Commit

Permalink
Implemented lastFM & Deezer API stuff.
Browse files Browse the repository at this point in the history
  • Loading branch information
mmarcon committed May 5, 2012
1 parent 15b9e60 commit c007726
Show file tree
Hide file tree
Showing 2 changed files with 146 additions and 8 deletions.
128 changes: 122 additions & 6 deletions app/API.js
Expand Up @@ -3,23 +3,31 @@

APP.API = {};
APP.API.Config = {
freeSoundBaseURL: 'http://www.freesound.org/api/sounds/geotag/?min_lat={BB_MIN_LAT}&min_lon={BB_MIN_LNG}&max_lat={BB_MAX_LAT}&max_lon={BB_MAX_LNG}&sounds_per_page=5&api_key=1142723002b040a0b1f378dc8787bb70'
freeSoundBaseURL: 'http://www.freesound.org/api/sounds/geotag/?min_lat={BB_MIN_LAT}&min_lon={BB_MIN_LNG}&max_lat={BB_MAX_LAT}&max_lon={BB_MAX_LNG}&sounds_per_page=5&api_key=1142723002b040a0b1f378dc8787bb70',
lastFMBaseURL: 'http://ws.audioscrobbler.com/2.0/?method=geo.getevents&lat={LATITUDE}&long={LONGITUDE}&api_key=6c9b80ce8e73b74ac58e22c0657d942c&format=json',
deezerBaseURL: 'http://api.deezer.com/2.0/search?q={ARTIST_NAME}&output=jsonp'
};

var F;
var F, L, D;

/************************************************************************/
APP.API.Freesound = function(){};

F = APP.API.Freesound.prototype;

F.retrieveSoundsFor = function(bb, callback) {
F.retrieveSoundsFor = function(bb, success, error) {
var url = APP.API.Config.freeSoundBaseURL, successFn, errorFn;
url = url.replace('{BB_MIN_LAT}', bb.minLat || '');
url = url.replace('{BB_MIN_LNG}', bb.minLng || '');
url = url.replace('{BB_MAX_LAT}', bb.maxLat || '');
url = url.replace('{BB_MAX_LNG}', bb.maxLng || '');

errorFn = function(){console.log('Error occurred.', arguments);};
errorFn = function(){
console.log('[Freesound] Error occurred.', arguments);
if (typeof error === 'function') {
error.apply(null, arguments);
}
};

successFn = function(response){
if (!response || !response.sounds) {
Expand All @@ -28,8 +36,8 @@
}
var sounds = response.sounds;
sounds.forEach(function(val, index, array){
if (typeof callback === 'function') {
callback(val);
if (typeof success === 'function') {
success(val);
}
});
};
Expand All @@ -41,4 +49,112 @@
});
};

/************************************************************************/

APP.API.LastFM = function(){};

L = APP.API.LastFM.prototype;

L.retrieveEventsFor = function(pos, success, error) {
var url = APP.API.Config.lastFMBaseURL, errorFn, successFn, deezer = new APP.API.Deezer();
url = url.replace('{LATITUDE}', pos.lat || '');
url = url.replace('{LONGITUDE}', pos.lng || '');

errorFn = function(){
console.log('[Last FM] Error occurred.', arguments);
if (typeof error === 'function') {
error.apply(null, arguments);
}
};

successFn = function(response){
if (!response || !response.events) {
errorFn('Invalid or empty response');
console.log(response);
return;
}
if (!response.events.event || !response.events.event.length === 0) {
errorFn('No events were found for the given location');
return;
}
response.events.event.forEach(function(e, index, array){
var meta = {
title: e.title || '',
artist: e.artists.headliner || false,
venue: {
name: e.venue.name || '',
location: {
lat: e.venue.location['geo:point']['geo:lat'] || '',
lng: e.venue.location['geo:point']['geo:long'] || '',
city: e.venue.location.city || '',
country: e.venue.location.country || '',
street: e.venue.location.street
}
},
date: e.startDate || '',
tags: (e.tags && e.tags.tag) || ''
};
if (meta.artist) {
deezer.retrieveSoundsFor(meta, success)
}
});

};


$.ajax({
url: url,
success: successFn,
error: errorFn
});
};

/************************************************************************/

APP.API.Deezer = function(){};

D = APP.API.Deezer.prototype;

D.retrieveSoundsFor = function(meta, success, error) {
var url = APP.API.Config.deezerBaseURL, errorFn, successFn;
url = url.replace('{ARTIST_NAME}', meta.artist || '');

errorFn = function(){
console.log('[Deezer] Error occurred.', arguments);
if (typeof error === 'function') {
error.apply(null, arguments);
}
};

successFn = function(response) {
if (!response || !response.data || response.data.length === 0) {
errorFn('Invalid or empty response');
return;
}
var data = {
title: response.data[0].title || '',
preview: response.data[0].preview || '',
artist: response.data[0].artist.name || '',
album: {
title: response.data[0].album.title,
cover: response.data[0].album.cover
},
meta: meta
};
if (typeof success === 'function') {
success(data);
}
};

$.ajax({
url: url,
dataType: 'jsonp',
success: successFn,
error: errorFn
});
};

/************************************************************************/


})();
26 changes: 24 additions & 2 deletions app/index.html
Expand Up @@ -42,6 +42,8 @@
APP.ListenerObj.bindKeyboardEvents();
APP.freesound = new APP.API.Freesound();
APP.freesound.aSounds = [];
APP.lastfm = new APP.API.LastFM();
APP.lastfm.aSounds = [];

// Authentication set-up is the first thing that you must do with the API
nokia.maps.util.ApplicationContext.set({
Expand Down Expand Up @@ -87,14 +89,15 @@
minLng: vp.topLeft.longitude,
maxLat: vp.topLeft.latitude,
maxLng: vp.bottomRight.longitude},
addSound);
addFreesoundSound);
APP.lastfm.retrieveEventsFor({lat: APP.map.center.latitude, lng: APP.map.center.longitude}, addLastFMEvent);
},

ensureSize = function() {
// $('#mapContainer').height($('body').height() - $('#header').height());
},

addSound = function(sound) {
addFreesoundSound = function(sound) {
var soundObj = Audio.createSource();
Audio.setAudioSource(soundObj, sound['preview-hq-mp3']);
soundObj.panner.setPosition(sound.geotag.lon, sound.geotag.lat, 0);
Expand All @@ -110,6 +113,25 @@
APP.map.objects.add(marker);
soundObj.source.noteOn(Audio.getCurrentTime() + 0.020);
APP.freesound.aSounds.push(soundObj);
},

addLastFMEvent = function(event) {
var soundObj = Audio.createSource();
Audio.setAudioSource(soundObj, event.preview);
soundObj.panner.setPosition(event.meta.venue.location.lng, event.meta.venue.location.lat, 0);
var marker = new nokia.maps.map.StandardMarker({latitude: event.meta.venue.location.lat, longitude: event.meta.venue.location.lng}, {brush: {color: '#cc0000'}});
marker.text = event.artist;
marker.coord = {latitude: event.meta.venue.location.lat, longitude: event.meta.venue.location.lng};

marker.addListener("click", function (evt) {
   APP.infoBubbles.addBubble(this.text, this.coord);
   evt.preventDefault();
   evt.stopPropagation();
});
APP.map.objects.add(marker);
soundObj.source.noteOn(Audio.getCurrentTime() + 0.020);
APP.lastfm.aSounds.push(soundObj);

};

var L;
Expand Down

0 comments on commit c007726

Please sign in to comment.