Skip to content

Commit

Permalink
Merge branch 'master' into radio-mode
Browse files Browse the repository at this point in the history
  • Loading branch information
Rayman committed Nov 29, 2014
2 parents ddac089 + d518877 commit 2190d7b
Show file tree
Hide file tree
Showing 8 changed files with 123 additions and 91 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,15 @@ pids
*.pid
*.seed

#osx
*.DS_Store

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage
build

# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt
Expand Down
46 changes: 9 additions & 37 deletions client/components/searchbar/searchbar.js
Original file line number Diff line number Diff line change
@@ -1,38 +1,25 @@
// initialize active_tab
Session.set('active_tab', 'songs');

//Initialisation of searchbar component,
// template example:
// {{> searchBar lovedToggle=0 viewToggle=0 collection="Playlists" playlistId=this._id }}
// {{> searchBar viewToggle=true callback="searchCallback" }}
/*--------------------------
Available parameters
collection (Collection) Name of the collection to add objects to, requires extra code in
the add function, or sh*t will hit the fan (default: Playlists)
playlistId (int) pointer to which playlist songs should be added (required)
callback (string) The name of the helper of the parent template that
will be called when a song has been selected
viewToggle (boolean) show/hide the toggle view buttons (default: show)
lovedToggle (boolean) show/hide the add songs from loved songs button (default: show)
---------------------------*/

Template.searchBar.created = function( template ) {
//debug purposes
var d = Template.currentData() || {}; //at least return an obj
this.collection = (typeof d.playlistId !== "undefined") ? window[d.collection] : window.Playlists;
this.playlistId = (typeof d.playlistId !== "undefined") ? d.playlistId : null;
var d = this.data;
this.lovedToggle = (typeof d.lovedToggle !== "undefined") ? d.lovedToggle : 1;
this.viewToggle = (typeof d.viewToggle !== "undefined") ? d.viewToggle : 1;


this.lovedToggleWidth = (this.lovedToggle === 1) ? 1 : 0;
this.viewToggleWidth = (this.viewToggle === 1) ? 2 : 0;
this.searchBarWidth = 12 - this.lovedToggleWidth - this.viewToggleWidth;
console.log(this);

};

Template.searchBar.helpers({
isActiveTab: function(route) {
return Session.equals("active_tab", route) ? "active" : "";
},
lovedSongs: function() {
var user = Meteor.user();
if (!user)
Expand All @@ -47,11 +34,6 @@ Template.searchBar.helpers({


Template.searchBar.events = {
'click ul.playlist-tabs > li': function (e) {
var li = $(e.currentTarget);
var route = li.data('id');
Session.set("active_tab", route);
},
'submit form.youtube-search': function (e) {
e.preventDefault();
},
Expand All @@ -75,24 +57,14 @@ Template.searchBar.events = {

//add search result to Meteor collection
'click .youtube-result, click a.loved': function(e, template) {
$("input.youtube-query").focus();

var videoId = (this.id !== undefined) ? this.id : this._id;

//see if search bar is linked up to a collection
if (!template.collection) { console.warn("no collection defined"); return; }
if (!template.playlistId) { console.warn("no collection id defined"); return; }

console.log('queue video:', videoId, 'to playlist', template.playlistId);
$("input.youtube-query").focus();
var songObject = {
"added" : new Date(),
"author" : Meteor.userId(),
"songId" : videoId
};
template.collection.update(
{ _id: template.playlistId},
{ $push: { songs: songObject} }
);
// callback here
var callbackName = template.data.callback;
var cb = template.view.parentView.parentView.template.__helpers.get(callbackName);
cb(videoId);
},

//Loved a song!
Expand Down
1 change: 1 addition & 0 deletions client/components/suggestions/suggestions.html
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ <h3 class="panel-title">
<li><a href="#">Jazz</a></li>
<li><a href="#">Soul</a></li>
<li><a href="#">Hip Hop</a></li>
<li><a href="#" data-genre="hypemPopular">HypeMachine Popular</a></li>
</ul>
</div>
</h3>
Expand Down
85 changes: 47 additions & 38 deletions client/components/suggestions/suggestions.js
Original file line number Diff line number Diff line change
@@ -1,44 +1,23 @@
var refreshSuggestions = function() {
Meteor.call('hotRightNow', Session.get("suggestion-genre"), function(error,result) {
//hack to make calls to Hypemachine API when selecting this "genre"
var apiCall = (Session.get("suggestion-genre") === "hypemPopular") ? Session.get("suggestion-genre") : "hotRightNow";
Meteor.call(apiCall, Session.get("suggestion-genre"), function(error,result) {
if(error) { console.log(error); return; }
Session.set("suggestions", result);
});
}
};

Template.suggestions.created = function() {
Session.set("suggestion-genre","alternative rock");
refreshSuggestions();
}

Template.suggestions.songs = function() {
return Session.get('suggestions');
}

Template.suggestions.events = {
'click button#refreshSuggestions' : refreshSuggestions,

'click .suggestion-genres li' : function(e) {
var genre = e.target.text.toLowerCase();
Session.set("suggestion-genre",genre);
console.log(e);
$(".suggestion-genres li").removeClass("active");
$(e.currentTarget).addClass("active");
refreshSuggestions();
},

'click #addSong': function (e,template) {
var playlistId = Router.current().params._id;

//prepare query for Youtube
var options = {
part: 'id',
type: 'video',
videoEmbeddable: 'true',
q: this.artist_name + " " + this.title
}
//perform Youtube search using artist and song information
Meteor.call('youtube_search', options, function(error, data) {
if (error) { return false; }
var addSongfromQuery = function(query, playlistId) {
//prepare query for Youtube
var options = {
part: 'id',
type: 'video',
videoEmbeddable: 'true',
q: query
};
//perform Youtube search using artist and song information
Meteor.call('youtube_search', options, function(error, data) {
if (error) { return false; }

//Id from first search result
var videoId = data.items[0].id.videoId;
Expand All @@ -47,7 +26,7 @@ Template.suggestions.events = {
var options = {
'part': 'snippet,contentDetails,statistics',
'id': videoId,
}
};
Meteor.call('youtube_videos_list', options);

//Put it in current playlist
Expand All @@ -62,6 +41,36 @@ Template.suggestions.events = {
{ $push: { songs: songObject} }
);
});
};


Template.suggestions.created = function() {
Session.set("suggestion-genre","alternative rock");
refreshSuggestions();
};

Template.suggestions.helpers({
"songs" : function() {
return Session.get("suggestions");
}
});

Template.suggestions.events = {
'click button#refreshSuggestions' : refreshSuggestions,

'click .suggestion-genres li' : function(e) {
var genre = (e.target.getAttribute("data-genre") !== null) ? e.target.getAttribute("data-genre") : e.target.text.toLowerCase();
Session.set("suggestion-genre",genre);
console.log(e);
$(".suggestion-genres li").removeClass("active");
$(e.currentTarget).addClass("active");
refreshSuggestions();
},

'click #addSong': function (e,template) {
var playlistId = Router.current().params._id;
addSongfromQuery( this.artist_name + " " + this.title, playlistId);


},
}
};
2 changes: 1 addition & 1 deletion client/views/playlists/playlist.html
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ <h3 class="panel-title">Stats</h3>
</div>

<div class="col-md-8">
{{> searchBar viewToggle=1 collection="Playlists" playlistId=this._id }}
{{> searchBar viewToggle=true callback="searchCallback" }}
{{> songs}}
</div>
</div>
Expand Down
18 changes: 18 additions & 0 deletions client/views/playlists/playlist.js
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,24 @@ Template.playlist.events = {
},
};

Template.playlist.helpers({
searchCallback: function (videoId) {
// this function will get called from the searchBar template
var playlistId = Template.parentData(1)._id;

console.log('queue video:', videoId, 'to playlist', playlistId);
var songObject = {
"added" : new Date(),
"author" : Meteor.userId(),
"songId" : videoId
};
Playlists.update(
{ _id: playlistId},
{ $push: { songs: songObject} }
);
}
});

Template.updatePlaylistForm.editingDoc = function () {
return Playlists.findOne({_id: this._id});
};
Expand Down
40 changes: 34 additions & 6 deletions server/echonest.js → server/apis.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ var ECHONEST_KEY = 'Z9TLXHBRUB49YXCNR';

Meteor.methods({
getArtistInfo: function(artist) {
var artist = encodeURIComponent(artist);
var url = "http://developer.echonest.com/api/v4/artist/profile?api_key="+ECHONEST_KEY+"&name="+artist+"&bucket=hotttnesss&bucket=terms&bucket=images&bucket=songs"

artist = encodeURIComponent(artist);
var url = "http://developer.echonest.com/api/v4/artist/profile?api_key="+ECHONEST_KEY+"&name="+artist+"&bucket=hotttnesss&bucket=terms&bucket=images&bucket=songs";
var result;
try {
var result = HTTP.get(url);
result = HTTP.get(url);
} catch(e) {
console.log(e.toString());
return false;
Expand All @@ -22,8 +22,9 @@ Meteor.methods({
//default genre
genre = typeof genre !== 'undefined' ? genre : 'alternative rock';
var url = "http://developer.echonest.com/api/v4/playlist/static?api_key="+ECHONEST_KEY+"&type=genre-radio&genre="+genre+"&results=10&bucket=song_hotttnesss";
var result;
try {
var result = HTTP.get(url);
result = HTTP.get(url);
} catch(e) {
console.log(e.toString());
return false;
Expand All @@ -34,8 +35,35 @@ Meteor.methods({
});
return result.data.response.songs;
} else {
//console.log(result.content);
return false;
}
},

//Hypem related methods
//Should return data more or less in the same way as the echnonest does, so these
//two methods can be exchanged
hypemPopular: function() {
var url = "http://hypem.com/playlist/popular/3day/json/1/data.js";
var result;
try {
result = HTTP.get(url);
} catch(e) {
console.log(e.toString());
return false;
}
if (result.data) {
var nrs = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19];
var songs = nrs.map(function(nr) {
var song = result.data[nr];
return { artist_name:song.artist,
title:song.title
};
});
return songs;
} else {
return false;
}

}

});

0 comments on commit 2190d7b

Please sign in to comment.