From 78be17fd58edab0a3f2f14d63c04453f4ea671b8 Mon Sep 17 00:00:00 2001 From: magnolia-fan Date: Sun, 27 Nov 2011 00:56:09 +0400 Subject: [PATCH] Built-in Last.fm TOP playlists --- app/assets/javascripts/player.coffee | 22 +++++++++++++------ .../vksearch/vkontakte_search.coffee | 2 +- app/assets/stylesheets/albums.scss | 2 +- app/assets/stylesheets/misc.scss | 5 +++++ app/controllers/application_controller.rb | 4 ++++ app/controllers/artist_controller.rb | 1 - app/controllers/last_fm_controller.rb | 2 +- app/controllers/playlist_controller.rb | 14 ++++++++++++ app/models/beathaven/playlist.rb | 2 +- app/views/artist/_page.html.haml | 14 +++++++----- config/routes.rb | 1 + 11 files changed, 52 insertions(+), 17 deletions(-) diff --git a/app/assets/javascripts/player.coffee b/app/assets/javascripts/player.coffee index 4c8b66a..f074b69 100644 --- a/app/assets/javascripts/player.coffee +++ b/app/assets/javascripts/player.coffee @@ -58,13 +58,13 @@ class window.Player s = len - Math.floor(len / 60) * 60 duration = m + ':' + (if s < 10 then '0' else '') + s item_class = (if item.available == false then 'unavailable' else '') - $('.playlist').append ' -
  • -
    ' +duration+ '
    -
    remove
    -
    ' +item.artist+ '
    -
    ' +item.name+ '
    -
  • ' + $('.playlist').append " +
  • +
    #{duration}
    +
    remove
    + +
    #{item.name}
    +
  • " _player.playlist.push item $('.playlist').sortable axis: 'y', cursor: 'move' if initial_count == 0 and not _player.hasTrack() @@ -282,3 +282,11 @@ $('.set-playlist').live 'click', -> _search.hideSpinner() false false + +$('.dynamic-playlist') .live 'click', -> + _search.showSpinner() + $.get "/playlist/#{$(this).data('playlist')}", (playlist) -> + _player.setPlaylist playlist + _search.hideSpinner() + false + false diff --git a/app/assets/javascripts/vksearch/vkontakte_search.coffee b/app/assets/javascripts/vksearch/vkontakte_search.coffee index 0a515dc..e21d360 100644 --- a/app/assets/javascripts/vksearch/vkontakte_search.coffee +++ b/app/assets/javascripts/vksearch/vkontakte_search.coffee @@ -14,7 +14,7 @@ class window.VkontakteMusic if @query_results[query]? and not return_all callback @query_results[query] that = this - VK.Api.call 'audio.search', q: query, (r) -> + VK.Api.call 'audio.search', q: query, count: 200, (r) -> results = that.range r.response, artist, track, duration top_result = null if results.length > 0 diff --git a/app/assets/stylesheets/albums.scss b/app/assets/stylesheets/albums.scss index b62224c..6339b21 100644 --- a/app/assets/stylesheets/albums.scss +++ b/app/assets/stylesheets/albums.scss @@ -22,7 +22,7 @@ display: block; margin: 1px 0; } -.set-playlist { +.set-playlist, .dynamic-playlist { float: right; cursor: pointer; } diff --git a/app/assets/stylesheets/misc.scss b/app/assets/stylesheets/misc.scss index 78ba596..ac4b68f 100644 --- a/app/assets/stylesheets/misc.scss +++ b/app/assets/stylesheets/misc.scss @@ -28,4 +28,9 @@ table.stats { width: 50%; +} + +.data.artist { + color: inherit; + cursor: pointer; } \ No newline at end of file diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index b2436ad..2c0c82f 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -23,6 +23,10 @@ class ApplicationController < ActionController::Base render partial: 'stat' end + def cache_for time + response.headers['Cache-Control'] = 'public, max-age=' + time.seconds.to_s + end + protected def authorize diff --git a/app/controllers/artist_controller.rb b/app/controllers/artist_controller.rb index 5278f6b..096f34f 100644 --- a/app/controllers/artist_controller.rb +++ b/app/controllers/artist_controller.rb @@ -32,7 +32,6 @@ class ArtistController < ApplicationController return render json: { status: 'fail', html: render_compact_partial(:fail) } end - # response.headers['Cache-Control'] = 'public, max-age='+1.week.seconds.to_s render json: { status: @artist.status_str, artist: @artist, diff --git a/app/controllers/last_fm_controller.rb b/app/controllers/last_fm_controller.rb index c3aaec7..30f4655 100644 --- a/app/controllers/last_fm_controller.rb +++ b/app/controllers/last_fm_controller.rb @@ -80,7 +80,7 @@ class LastFmController < ApplicationController end def self.top_playlist artist - playlist = Playlist.create(name: "#{artist.name}: Last.fm TOP") + playlist = Playlist.create(name: "#{artist.name}: Last.fm TOP", artist: artist) LastFM::Artist.get_top_tracks(artist: artist.name)["toptracks"]["track"].each do |track| tracks = Track.joins(:album, :artists).where(name: track["name"], "track_artists.artist_id" => artist.id) PlaylistItem.create(playlist_id: playlist.id, track_id: tracks.first.id) unless tracks.empty? diff --git a/app/controllers/playlist_controller.rb b/app/controllers/playlist_controller.rb index ef8076c..8c27eb0 100644 --- a/app/controllers/playlist_controller.rb +++ b/app/controllers/playlist_controller.rb @@ -3,4 +3,18 @@ class PlaylistController < ApplicationController @playlist = Playlist.find_by_id(params[:id]) render json: @playlist, include: { playlist_items: { include: { track: { include: { artists: {} }}}}} end + + def lastfm_top50 + artist = Artist.find_by_id(params[:id]) + return if artist.nil? + + playlist = Playlist.new(name: "#{artist.name}: Last.fm TOP 50", artist: artist) + LastFM::Artist.get_top_tracks(artist: artist.name)["toptracks"]["track"].each do |track| + tracks = Track.joins(:album, :artists).where(name: track["name"], "track_artists.artist_id" => artist.id) + playlist.playlist_items << PlaylistItem.new(track_id: tracks.first.id) unless tracks.empty? + end + + cache_for 1.week + render json: playlist, include: { playlist_items: { include: { track: { include: { artists: {} }}}}} + end end diff --git a/app/models/beathaven/playlist.rb b/app/models/beathaven/playlist.rb index 92fdea2..9e4c582 100644 --- a/app/models/beathaven/playlist.rb +++ b/app/models/beathaven/playlist.rb @@ -1,5 +1,5 @@ class Playlist < ActiveRecord::Base belongs_to :user belongs_to :artist - has_many :playlist_items + has_many :playlist_items, dependent: :destroy end diff --git a/app/views/artist/_page.html.haml b/app/views/artist/_page.html.haml index 6e5f3c6..413c720 100644 --- a/app/views/artist/_page.html.haml +++ b/app/views/artist/_page.html.haml @@ -18,11 +18,15 @@ %a.foreign-link{ href: service.url, target: '_blank' } = image_tag 'services/'+service.service+(service.service == 'official_homepage' ? '.png' : '.ico') -- unless @artist.playlists.empty? - .row - .span7.offset4 - %h3= t('player.playlists') - %table.zebra-striped +.row + .span7.offset4 + %h3= t('player.playlists') + %table.zebra-striped + %tr + %td + %span.label.success.dynamic-playlist{ href: "", 'data-playlist' => "lastfm-top50/#{@artist.id}" }= t('player.set_playlist') + %a.playlist-name{ href: "", 'data-playlist' => "lastfm-top50/#{@artist.id}" }= "#{@artist.name}: Last.fm TOP" + - @artist.playlists.each do |playlist| %tr - @artist.playlists.each do |playlist| %td diff --git a/config/routes.rb b/config/routes.rb index 1597053..8666641 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -16,6 +16,7 @@ Beathaven::Application.routes.draw do match 'track/report' => 'track#report_unavailable' match 'playlist/(:id)' => 'playlist#data' + match 'playlist/lastfm-top50/(:id)' => 'playlist#lastfm_top50' match 'settings' => 'user#settings'