From 63136f8b67d81b9fdb72110f0f6993c1f0369a77 Mon Sep 17 00:00:00 2001 From: magnolia-fan Date: Thu, 7 Apr 2011 08:02:49 +0400 Subject: [PATCH] Cache album pics to mongo & listen url & check if track in db --- rails/app/controllers/track_controller.rb | 11 ++++++ rails/app/helpers/track_helper.rb | 2 ++ rails/app/models/album.rb | 36 ++++++++++++------- rails/app/models/album_pic.rb | 11 ++++++ rails/app/models/track.rb | 8 ++--- rails/app/models/track_data.rb | 10 +++--- rails/app/views/artist/view.rhtml | 4 +-- rails/config/routes.rb | 1 + rails/public/stylesheets/beathaven.css | 9 +++-- rails/test/fixtures/album_pics.yml | 11 ++++++ .../test/functional/track_controller_test.rb | 8 +++++ rails/test/unit/album_pic_test.rb | 8 +++++ rails/test/unit/helpers/track_helper_test.rb | 4 +++ rails/tmp/pids/server.pid | 1 - 14 files changed, 96 insertions(+), 28 deletions(-) create mode 100644 rails/app/controllers/track_controller.rb create mode 100644 rails/app/helpers/track_helper.rb create mode 100644 rails/app/models/album_pic.rb create mode 100644 rails/test/fixtures/album_pics.yml create mode 100644 rails/test/functional/track_controller_test.rb create mode 100644 rails/test/unit/album_pic_test.rb create mode 100644 rails/test/unit/helpers/track_helper_test.rb delete mode 100644 rails/tmp/pids/server.pid diff --git a/rails/app/controllers/track_controller.rb b/rails/app/controllers/track_controller.rb new file mode 100644 index 0000000..2867087 --- /dev/null +++ b/rails/app/controllers/track_controller.rb @@ -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 diff --git a/rails/app/helpers/track_helper.rb b/rails/app/helpers/track_helper.rb new file mode 100644 index 0000000..3e73cce --- /dev/null +++ b/rails/app/helpers/track_helper.rb @@ -0,0 +1,2 @@ +module TrackHelper +end diff --git a/rails/app/models/album.rb b/rails/app/models/album.rb index b690e06..1c3a3d2 100644 --- a/rails/app/models/album.rb +++ b/rails/app/models/album.rb @@ -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>/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>/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 diff --git a/rails/app/models/album_pic.rb b/rails/app/models/album_pic.rb new file mode 100644 index 0000000..fb818d6 --- /dev/null +++ b/rails/app/models/album_pic.rb @@ -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 diff --git a/rails/app/models/track.rb b/rails/app/models/track.rb index 25ab14d..8fdf4d4 100644 --- a/rails/app/models/track.rb +++ b/rails/app/models/track.rb @@ -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 diff --git a/rails/app/models/track_data.rb b/rails/app/models/track_data.rb index c4fd87a..1568c18 100644 --- a/rails/app/models/track_data.rb +++ b/rails/app/models/track_data.rb @@ -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 diff --git a/rails/app/views/artist/view.rhtml b/rails/app/views/artist/view.rhtml index 8b775e9..e1ba6cd 100644 --- a/rails/app/views/artist/view.rhtml +++ b/rails/app/views/artist/view.rhtml @@ -10,8 +10,8 @@ %>
    <% first_release.tracks.each do |track| %> -
  • - +
  • + <%= track.name %> <%= track.length.toTime %>
  • diff --git a/rails/config/routes.rb b/rails/config/routes.rb index fc1e854..848f975 100644 --- a/rails/config/routes.rb +++ b/rails/config/routes.rb @@ -57,4 +57,5 @@ Beathaven::Application.routes.draw do # match ':controller(/:action(/:id(.:format)))' match 'artist/:name' => 'artist#view' + match 'listen/:id' => 'track#listen' end diff --git a/rails/public/stylesheets/beathaven.css b/rails/public/stylesheets/beathaven.css index 08ce4bc..5a62095 100644 --- a/rails/public/stylesheets/beathaven.css +++ b/rails/public/stylesheets/beathaven.css @@ -21,7 +21,7 @@ body { display: block; width: 100%; text-align: center; - margin: 0 0 0.5em 0; + margin: 2em 0 0.5em 0; font-size: 3em; } @@ -74,4 +74,9 @@ body { background-image: url(/images/play.png); width: 31px; height: 30px; - margin: 0 0.3em 0 0; \ No newline at end of file + margin: 0 0.3em 0 0; +} + +.album .tracks li .play.disabled { + background-image: none; +} \ No newline at end of file diff --git a/rails/test/fixtures/album_pics.yml b/rails/test/fixtures/album_pics.yml new file mode 100644 index 0000000..2893341 --- /dev/null +++ b/rails/test/fixtures/album_pics.yml @@ -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 diff --git a/rails/test/functional/track_controller_test.rb b/rails/test/functional/track_controller_test.rb new file mode 100644 index 0000000..1c318c7 --- /dev/null +++ b/rails/test/functional/track_controller_test.rb @@ -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 diff --git a/rails/test/unit/album_pic_test.rb b/rails/test/unit/album_pic_test.rb new file mode 100644 index 0000000..d645f90 --- /dev/null +++ b/rails/test/unit/album_pic_test.rb @@ -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 diff --git a/rails/test/unit/helpers/track_helper_test.rb b/rails/test/unit/helpers/track_helper_test.rb new file mode 100644 index 0000000..7cd46c5 --- /dev/null +++ b/rails/test/unit/helpers/track_helper_test.rb @@ -0,0 +1,4 @@ +require 'test_helper' + +class TrackHelperTest < ActionView::TestCase +end diff --git a/rails/tmp/pids/server.pid b/rails/tmp/pids/server.pid deleted file mode 100644 index 25daf89..0000000 --- a/rails/tmp/pids/server.pid +++ /dev/null @@ -1 +0,0 @@ -2408 \ No newline at end of file