Skip to content

Commit

Permalink
Now pulling in Last.fm API and displaying recent tracks for a user.
Browse files Browse the repository at this point in the history
  • Loading branch information
PxlBuzzard committed Feb 15, 2014
1 parent e075fc9 commit a100146
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 2 deletions.
17 changes: 17 additions & 0 deletions .jshintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"node": true,
"browser": true,
"esnext": true,
"bitwise": false,
"curly": false,
"eqeqeq": true,
"eqnull": true,
"immed": true,
"latedef": true,
"newcap": true,
"noarg": true,
"strict": false,
"trailing": true,
"smarttabs": true,
"undef": false
}
2 changes: 1 addition & 1 deletion .meteor/packages
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@
# but you can also edit it by hand.

standard-app-packages
insecure
preserve-inputs
http
2 changes: 1 addition & 1 deletion client/main.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
</div>
</header>
<div id="main" class="row-fluid">
{{> songsList}}
{{> hello}}
</div>
</div>
</body>
11 changes: 11 additions & 0 deletions client/views/hello.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<template name="hello">
<h3>Meteor: Last.FM check</h3>
<input type="text" id="userName" value="{{userName}}"></input>
<input type="button" value="Fetch" id="fetchButton" />

<ul>
{{#each recentTracks}}
<li>{{name}}, {{artist.text}}</li>
{{/each}}
</ul>
</template>
29 changes: 29 additions & 0 deletions client/views/hello.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
var userName = "Pxl_Buzzard";

Template.hello.events({
'click #fetchButton': function (e) {
e.preventDefault();
console.log("Recent tracks from last.fm!");
$('#fetchButton').attr('disabled', 'true').val('loading...');
userName = $('#userName').val();
Meteor.call('fetchFromService', userName, function (err, respJson) {
if (err) {
window.alert("Error: " + err.reason);
console.log("error occured on receiving data on server. ", err);
} else if(respJson) {
console.log("respJson: ", respJson);
//window.alert(respJson.length + ' tracks received.');
Session.set("recentTracks", respJson);
}
$('#fetchButton').removeAttr('disabled').val('Fetch');
});
}
});

Template.hello.recentTracks = function () {
return Session.get("recentTracks") || [];
};

Template.hello.userName = function() {
return userName;
};
25 changes: 25 additions & 0 deletions collections/songs.js
Original file line number Diff line number Diff line change
@@ -1 +1,26 @@
Songs = new Meteor.Collection('songs');

Meteor.methods({
fetchFromService: function(userName) {
var url = "http://ws.audioscrobbler.com/2.0/?method=user.getrecenttracks&user="+userName+"&api_key="+Meteor.settings.lastfm_apikey+"&format=json";

var result = HTTP.get(url, {timeout:30000});
if(result.statusCode === 200) {
var respJson = JSON.parse(result.content);
respJson = respJson.recenttracks.track;

// clean last fm json
for(var i = 0; i < respJson.length; ++i) {
respJson[i].artist.text = respJson[i].artist['#text'];
delete respJson[i].artist['#text'];
}

console.log("response received.");
return respJson;
} else {
console.log("Response issue: ", result.statusCode);
var errorJson = JSON.parse(result.content);
throw new Meteor.Error(result.statusCode, errorJson.error);
}
}
});

0 comments on commit a100146

Please sign in to comment.