Built-in Last.fm TOP playlists
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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?
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user