1
0
Fork 0
oldhaven/app/models/music_brainz_album.rb

61 lines
1.4 KiB
Ruby

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\ssize=\"(.*)\">(.*)<\/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