1
0
Fork 0

Built-in Last.fm TOP playlists

This commit is contained in:
magnolia-fan 2011-11-27 00:56:09 +04:00
parent f3c178a092
commit 78be17fd58
11 changed files with 52 additions and 17 deletions

View File

@ -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 '
<li id="i' +Math.round(Math.random() * 999999)+ '" data-id="'+item.id+'" class="'+item_class+'">
<div class="song-duration">' +duration+ '</div>
<div class="label important remove">remove</div>
<div class="artist-name">' +item.artist+ '</div>
<div class="song-title">' +item.name+ '</div>
</li>'
$('.playlist').append "
<li id='i#{Math.round(Math.random() * 999999)}' data-id='#{item.id}' class='#{item_class}'>
<div class='song-duration'>#{duration}</div>
<div class='label important remove'>remove</div>
<div class='artist-name'><a class='data artist'>#{item.artist}</a></div>
<div class='song-title'>#{item.name}</div>
</li>"
_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

View File

@ -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

View File

@ -22,7 +22,7 @@
display: block;
margin: 1px 0;
}
.set-playlist {
.set-playlist, .dynamic-playlist {
float: right;
cursor: pointer;
}

View File

@ -29,3 +29,8 @@
table.stats {
width: 50%;
}
.data.artist {
color: inherit;
cursor: pointer;
}

View File

@ -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

View File

@ -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,

View File

@ -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?

View File

@ -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

View File

@ -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

View File

@ -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
.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

View File

@ -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'