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
+11
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
+2
View File
@@ -0,0 +1,2 @@
module TrackHelper
end
+24 -12
View File
@@ -7,17 +7,29 @@ class Album < ActiveRecord::Base
require 'iconv'
def cover artist
q_artist = CGI::escape(artist)
q_album = CGI::escape(name)
#i = Iconv.new('Windows-31J', 'UTF-8')
path = open(
'http://ws.audioscrobbler.com/2.0/' <<
'?method=album.getinfo' <<
'&api_key=b25b959554ed76058ac220b7b2e0a026' <<
'&artist=' << q_artist <<
'&album=' << q_album
).read
path.match(/<image\ssize=\"mega\">(.*)<\/image>/i)[1]
covers = AlbumPic.where(album_id: id)
unless covers.empty?
covers.first.extralarge
else
q_artist = CGI::escape(artist)
q_album = CGI::escape(name)
path = open(
'http://ws.audioscrobbler.com/2.0/' <<
'?method=album.getinfo' <<
'&api_key=04fda005dbf61a50af5abc3e90f111f2' <<
'&artist=' << q_artist <<
'&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
+11
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
+2 -6
View File
@@ -2,11 +2,7 @@ class Track < ActiveRecord::Base
set_table_name 'musicbrainz.bh_track'
belongs_to :release
def clearName
name.gsub('&', 'and').gsub(/[^a-z0-9]/, '').downcase
end
def files
TrackData(:id => id)
def inDb
! TrackData.where(id: id.to_s).empty?
end
end
+5 -5
View File
@@ -2,9 +2,9 @@ class TrackData
include Mongoid::Document
store_in :track_data
key :id, Integer
key :artist, String
key :title, String
key :length, Integer
key :files, Array
key :id, Integer
key :artist, String
key :title, String
key :length, Integer
key :files, Array
end
+2 -2
View File
@@ -10,8 +10,8 @@
%>
<ul class="tracks">
<% first_release.tracks.each do |track| %>
<li>
<span class="play"></span>
<li id="<%= track.id %>">
<span class="play<%= (track.inDb ? '' : ' disabled') %>"></span>
<span class="track-name"><%= track.name %></span>
<span class="duration"><%= track.length.toTime %></span>
</li>