From 908f18c7de7efc74d7b443a114b71b3afcdb2047 Mon Sep 17 00:00:00 2001 From: magnolia-fan Date: Tue, 14 Jun 2011 20:21:36 +0400 Subject: [PATCH] Added MB models --- app/models/music_brainz_album.rb | 61 ++++++++++++++++++++++++++++++ app/models/music_brainz_artist.rb | 3 +- app/models/music_brainz_release.rb | 7 ++++ app/models/music_brainz_track.rb | 6 +++ 4 files changed, 75 insertions(+), 2 deletions(-) create mode 100644 app/models/music_brainz_album.rb create mode 100644 app/models/music_brainz_release.rb create mode 100644 app/models/music_brainz_track.rb diff --git a/app/models/music_brainz_album.rb b/app/models/music_brainz_album.rb new file mode 100644 index 0000000..bdeaad2 --- /dev/null +++ b/app/models/music_brainz_album.rb @@ -0,0 +1,61 @@ +class MusicBrainzAlbum < ActiveRecord::Base + establish_connection :musicbrainz + set_table_name 'musicbrainz.bh_release_group' + + belongs_to :music_brainz_artist + has_many :music_brainz_releases, :conditions => "release_type = 1", :order => 'date ASC, id ASC' + + require 'iconv' + + def cover artist + covers = AlbumPic.where(album_id: id).first + unless covers.nil? + covers.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.where( + album_id: id, + small: m[0][1], + medium: m[1][1], + large: m[2][1], + extralarge: m[3][1], + mega: m[4][1] + ).create + m[3][1] + end + end + + def tracksInDb + tracks = [] + result = [] + tracks_in_db = [] + track_ids = [] + + self.releases.first.tracks.each do |track| + tracks << track + track_ids << track.id.to_s + end + in_db = TrackData.any_in(id: track_ids).only("id") + unless in_db.nil? + in_db.each do |track| + tracks_in_db << track["id"].to_i; + end + end + tracks.each do |track| + result << { + :object => track, + :in_db => tracks_in_db.include?(track.id) ? nil : true + } + end + result + end +end \ No newline at end of file diff --git a/app/models/music_brainz_artist.rb b/app/models/music_brainz_artist.rb index 9f96720..c3ca09c 100644 --- a/app/models/music_brainz_artist.rb +++ b/app/models/music_brainz_artist.rb @@ -1,12 +1,11 @@ class MusicBrainzArtist < ActiveRecord::Base - establish_connection :musicbrainz set_table_name 'musicbrainz.bh_artist' has_many :albums, :conditions => "release_type = 1", :order => 'year ASC, id ASC' def self.getByName(name) - Artist.first(:conditions => ['name = ? AND id=credit_id', name], :order => 'rating DESC') + MusicBrainzArtist.first(:conditions => ['name = ? AND id=credit_id', name], :order => 'rating DESC') end def self.getLastFmAutocomplete(query) diff --git a/app/models/music_brainz_release.rb b/app/models/music_brainz_release.rb new file mode 100644 index 0000000..f9d014e --- /dev/null +++ b/app/models/music_brainz_release.rb @@ -0,0 +1,7 @@ +class MusicBrainzRelease < ActiveRecord::Base + establish_connection :musicbrainz + set_table_name 'musicbrainz.bh_release' + + belongs_to :music_brainz_album + has_many :music_brainz_tracks, :order => 'position ASC' +end \ No newline at end of file diff --git a/app/models/music_brainz_track.rb b/app/models/music_brainz_track.rb new file mode 100644 index 0000000..178438b --- /dev/null +++ b/app/models/music_brainz_track.rb @@ -0,0 +1,6 @@ +class MusicBrainzTrack < ActiveRecord::Base + establish_connection :musicbrainz + set_table_name 'musicbrainz.bh_track' + + belongs_to :music_brainz_release +end \ No newline at end of file