Permalink
Browse files

added a distance in time in words to lastfm tracks

  • Loading branch information...
1 parent f441c1b commit fa8450efa915115a294358c4166c901b1c4f61ea @markryall committed Jan 15, 2013
Showing with 16 additions and 4 deletions.
  1. +1 −1 lib/app.rb
  2. +12 −1 lib/lastfm.rb
  3. +1 −0 public/mobile-lastfm.coffee
  4. +1 −1 public/mobile-lastfm.js
  5. +1 −1 views/lastfm.slim
View
@@ -156,7 +156,7 @@ def change_volume inc
slim :lastfm, locals: {tracks: recent_lastfm_tracks_for(ENV['LAST_FM_USER']) }
end
- get '/api/lastfm' do |size|
+ get '/api/lastfm' do
json tracks: recent_lastfm_tracks_for(ENV['LAST_FM_USER'])
end
end
View
@@ -10,12 +10,13 @@ def recent_lastfm_tracks_for user
response = get_url "http://ws.audioscrobbler.com/2.0/?method=user.getrecenttracks&nowplaying=true&user=#{user}&api_key=#{ENV['LASTFM_API_KEY']}&format=json"
tracks = []
tracks = response['recenttracks']['track'] if response && response['recenttracks'] && response['recenttracks']['track']
+ now = Time.now.to_i
tracks.map do |track|
t = {
'name' => track['name'],
'artist' => track['artist']['#text'],
'album' => track['album']['#text'],
- 'date' => track['date']['uts'].to_i
+ 'when' => dotiw(now-track['date']['uts'].to_i)
}
track['image'].each do |image|
t['image'] = image['#text'] if image['size'] = 'large'
@@ -24,6 +25,16 @@ def recent_lastfm_tracks_for user
end
end
+ def dotiw seconds
+ return "#{seconds} seconds ago" if seconds < 60
+ minutes = seconds / 60
+ return "#{minutes} minutes ago" if minutes < 60
+ hours = minutes / 60
+ return "#{hours} hours ago" if hours < 24
+ days = hours / 24
+ "#{days} days ago"
+ end
+
def get_url url
HTTParty.get url
end
@@ -8,6 +8,7 @@ $ ->
<h3 class="ui-li-heading">{{name}}</h3>
<p class="ui-li-desc">{{album}}</p>
<p class="ui-li-desc">{{artist}}</p>
+ <p class="ui-li-desc">{{when}}</p>
</a>
</li>
{{/tracks}}
View
@@ -2,7 +2,7 @@
$(function() {
var load_lastfm, template;
- template = "<ul id=\"lastfm-list\" data-role=\"listview\">\n {{#tracks}}\n <li>\n <a href=\"#\">\n <img src=\"{{image}}\" class=\"ui-li-thumb\">\n <h3 class=\"ui-li-heading\">{{name}}</h3>\n <p class=\"ui-li-desc\">{{album}}</p>\n <p class=\"ui-li-desc\">{{artist}}</p>\n </a>\n </li>\n {{/tracks}}\n</ul>";
+ template = "<ul id=\"lastfm-list\" data-role=\"listview\">\n {{#tracks}}\n <li>\n <a href=\"#\">\n <img src=\"{{image}}\" class=\"ui-li-thumb\">\n <h3 class=\"ui-li-heading\">{{name}}</h3>\n <p class=\"ui-li-desc\">{{album}}</p>\n <p class=\"ui-li-desc\">{{artist}}</p>\n <p class=\"ui-li-desc\">{{when}}</p>\n </a>\n </li>\n {{/tracks}}\n</ul>";
load_lastfm = function() {
return $.get('/api/lastfm', {}, function(data) {
var list;
View
@@ -7,4 +7,4 @@
h2 = track['name']
p = track['album']
p = track['artist']
- p = Time.at track['date']
+ p = track['when']

0 comments on commit fa8450e

Please sign in to comment.