1
0
Fork 0

Cache album pics to mongo & listen url & check if track in db

This commit is contained in:
magnolia-fan 2011-04-07 08:02:49 +04:00
parent 3c96c151bd
commit 63136f8b67
14 changed files with 96 additions and 28 deletions

View File

@ -0,0 +1,11 @@
class TrackController < ApplicationController
def listen
@track = TrackData.where(id: params[:id])
unless @track.empty?
out = {:status => 'ok', :data => @track.first.files.first[1]}
else
out = {:status => 'no_such_track'}
end
render :json => out
end
end

View File

@ -0,0 +1,2 @@
module TrackHelper
end

View File

@ -7,17 +7,29 @@ class Album < ActiveRecord::Base
require 'iconv' require 'iconv'
def cover artist def cover artist
q_artist = CGI::escape(artist) covers = AlbumPic.where(album_id: id)
q_album = CGI::escape(name) unless covers.empty?
#i = Iconv.new('Windows-31J', 'UTF-8') covers.first.extralarge
path = open( else
'http://ws.audioscrobbler.com/2.0/' << q_artist = CGI::escape(artist)
'?method=album.getinfo' << q_album = CGI::escape(name)
'&api_key=b25b959554ed76058ac220b7b2e0a026' << path = open(
'&artist=' << q_artist << 'http://ws.audioscrobbler.com/2.0/' <<
'&album=' << q_album '?method=album.getinfo' <<
).read '&api_key=04fda005dbf61a50af5abc3e90f111f2' <<
'&artist=' << q_artist <<
path.match(/<image\ssize=\"mega\">(.*)<\/image>/i)[1] '&album=' << q_album
).read
m = path.scan(/<image\ssize=\"(.*)\">(.*)<\/image>/i)
AlbumPic.new(
album_id: id,
small: m[0][1],
medium: m[1][1],
large: m[2][1],
extralarge: m[3][1],
mega: m[4][1]
)
m[3][1]
end
end end
end end

View File

@ -0,0 +1,11 @@
class AlbumPic
include Mongoid::Document
store_in :album_pics
key :album_id, Integer
key :small, String
key :medium, String
key :large, String
key :extralarge, String
key :mega, String
end

View File

@ -2,11 +2,7 @@ class Track < ActiveRecord::Base
set_table_name 'musicbrainz.bh_track' set_table_name 'musicbrainz.bh_track'
belongs_to :release belongs_to :release
def clearName def inDb
name.gsub('&', 'and').gsub(/[^a-z0-9]/, '').downcase ! TrackData.where(id: id.to_s).empty?
end
def files
TrackData(:id => id)
end end
end end

View File

@ -2,9 +2,9 @@ class TrackData
include Mongoid::Document include Mongoid::Document
store_in :track_data store_in :track_data
key :id, Integer key :id, Integer
key :artist, String key :artist, String
key :title, String key :title, String
key :length, Integer key :length, Integer
key :files, Array key :files, Array
end end

View File

@ -10,8 +10,8 @@
%> %>
<ul class="tracks"> <ul class="tracks">
<% first_release.tracks.each do |track| %> <% first_release.tracks.each do |track| %>
<li> <li id="<%= track.id %>">
<span class="play"></span> <span class="play<%= (track.inDb ? '' : ' disabled') %>"></span>
<span class="track-name"><%= track.name %></span> <span class="track-name"><%= track.name %></span>
<span class="duration"><%= track.length.toTime %></span> <span class="duration"><%= track.length.toTime %></span>
</li> </li>

View File

@ -57,4 +57,5 @@ Beathaven::Application.routes.draw do
# match ':controller(/:action(/:id(.:format)))' # match ':controller(/:action(/:id(.:format)))'
match 'artist/:name' => 'artist#view' match 'artist/:name' => 'artist#view'
match 'listen/:id' => 'track#listen'
end end

View File

@ -21,7 +21,7 @@ body {
display: block; display: block;
width: 100%; width: 100%;
text-align: center; text-align: center;
margin: 0 0 0.5em 0; margin: 2em 0 0.5em 0;
font-size: 3em; font-size: 3em;
} }
@ -74,4 +74,9 @@ body {
background-image: url(/images/play.png); background-image: url(/images/play.png);
width: 31px; width: 31px;
height: 30px; height: 30px;
margin: 0 0.3em 0 0; margin: 0 0.3em 0 0;
}
.album .tracks li .play.disabled {
background-image: none;
}

11
rails/test/fixtures/album_pics.yml vendored Normal file
View File

@ -0,0 +1,11 @@
# Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html
# This model initially had no columns defined. If you add columns to the
# model remove the '{}' from the fixture names and add the columns immediately
# below each fixture, per the syntax in the comments below
#
one: {}
# column: value
#
two: {}
# column: value

View File

@ -0,0 +1,8 @@
require 'test_helper'
class TrackControllerTest < ActionController::TestCase
# Replace this with your real tests.
test "the truth" do
assert true
end
end

View File

@ -0,0 +1,8 @@
require 'test_helper'
class AlbumPicTest < ActiveSupport::TestCase
# Replace this with your real tests.
test "the truth" do
assert true
end
end

View File

@ -0,0 +1,4 @@
require 'test_helper'
class TrackHelperTest < ActionView::TestCase
end

View File

@ -1 +0,0 @@
2408