Class: MusicArtist

Inherits:
ActiveRecord::Base
  • Object
show all
Includes:
LastfmRequest
Defined in:
app/models/music_artist.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from LastfmRequest

included, #lastfm_request

Class Method Details

.create_by_name(name) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'app/models/music_artist.rb', line 59

def self.create_by_name(name)
  music_brainz_artists = MusicBrainz::Artist.search(name).select{|a| a[:name].downcase == name.downcase }
  artist_mbids = music_brainz_artists.map{|a| a[:mbid] }
  existing_artists = where(mbid: artist_mbids).map{|a| [a.id, a.mbid] }
  ids = existing_artists.map(&:first); mbids = existing_artists.map(&:last)
  
  artist_mbids.select{|m| !mbids.include?(m) }.each do |mbid|
    artist = create(name: name, mbid: mbid, is_ambiguous: music_brainz_artists.length > 1)
    ids << artist.id if artist.persisted?
  end 
  
  ids
end

Instance Method Details

#bonus_tracks_releaseObject



219
220
221
# File 'app/models/music_artist.rb', line 219

def bonus_tracks_release
  releases.where(name: '[Bonus Tracks]').first
end

#import_bonus_tracksObject



158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'app/models/music_artist.rb', line 158

def import_bonus_tracks
  offset = 0
  count = 100
  bonus_track_names = []
  
  begin
    recordings = MusicBrainz::Recording.search(mbid, nil, limit: 100, offset: offset, create_models: true)
    count = recordings.total_count
    recordings = recordings.select{|r| !MusicTrack.name_included_in_bonustrack_blacklist?(r.title) && r.disambiguation.blank? }
    recording_titles = recordings.map{|r| MusicTrack.format_name(r.title).downcase }.uniq
    voluntary_names = MusicTrack.where("artist_id = :artist_id AND LOWER(name) IN(:name)", artist_id: id, name: recording_titles).map{|t| t.name.downcase }
    
    recordings.select{|r| !voluntary_names.include?(MusicTrack.format_name(r.title).downcase) }.each do |recording|
      next if bonus_track_names.include? MusicTrack.format_name(recording.title).downcase
      
      track = MusicTrack.new(artist: self, name: MusicTrack.format_name(recording.title))
      track.artist_mbid = mbid
      
      if track.is_bonus_track?
        track.create_bonus_track(recording.id)
        bonus_track_names << track.name.downcase
      end
    end
    
    offset += 100
  end while offset < count
end

#import_music_videos_from_tapetvObject



186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
# File 'app/models/music_artist.rb', line 186

def import_music_videos_from_tapetv
  tapetv_videos, page = [], 1
  
  begin
    response = nil
    
    begin
      response = JSON.parse(HTTParty.get("http://www.tape.tv/#{name.split(' ').join('-').downcase}/videos.json?page=#{page}&page_size=8").body)
    rescue JSON::ParserError
    end
    
    break if response.nil?
    
    tapetv_videos += response.select{|v| !v['title'].match(/\(/) && !v['title'].match(/live/i) }.map{|v| { name: v['title'].gsub(/\(Video\)/, '').strip, url: v['share_url'] } }
    sleep 5
    
    break if response.length != 8
    
    page += 1
  end while !response.nil? && response.length > 0
  
  return if tapetv_videos.empty?
  
  tracks.where('LOWER(name) IN(?)', tapetv_videos.map{|v| MusicTrack.format_name(v[:name]).downcase }).each do |track|
    tapetv_video = tapetv_videos.select do |v| 
      MusicTrack.format_name(v[:name]).downcase == track.name.downcase ||
      MusicTrack.format_name(v[:name]).downcase.gsub('ß', 'ss') == track.name.downcase.gsub('ß', 'ss')
    end.first
    
    MusicVideo.create(status: 'Official', track_id: track.id, url: tapetv_video[:url]) unless tapetv_video.nil?
  end
end

#import_releases(musicbrainz_artist = nil) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'app/models/music_artist.rb', line 96

def import_releases(musicbrainz_artist = nil)
  musicbrainz_artist = MusicBrainz::Artist.find(mbid) unless musicbrainz_artist
  offset, count, voluntary_releases = 0, 100, []
    
  begin
    count, working_release_groups = release_groups(musicbrainz_artist, voluntary_releases, offset, false, true)
    
    working_release_groups.each do |array|
      musicbrainz_release_group, musicbrainz_releases = array
      release = releases.create(
        artist_name: name, name: musicbrainz_release_group.title,
        is_lp: musicbrainz_release_group.type == 'Album'
      )
      
      unless release.persisted? && release.valid?
        raise [release.errors.full_messages, release].inspect
      end
        
      release.releases = musicbrainz_releases
      release.import_metadata!
    end
    
    offset += 100
  end while offset < count  
end

#is_classical?(lastfm = nil) ⇒ Boolean

Returns:

  • (Boolean)


73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'app/models/music_artist.rb', line 73

def is_classical?(lastfm = nil)
  lastfm ||= Lastfm.new(LastfmApiKey, LastfmApiSecret)
  
  begin
    lastfm_artist_tags = lastfm_request(
      lastfm, :artist, :get_top_tags, 'The artist you supplied could not be found', artist: name, raise_if_response_is_just_nil: true
    )
      
    if lastfm_artist_tags.nil?
      raise 'lastfm failed: ' + [:artist, :get_top_tags, 'The artist you supplied could not be found', { artist: name }].inspect
    end
    
    tags = lastfm_artist_tags.map{|t| t['name'].downcase }[0..9] rescue []
    tags.select{|t| ['classic', 'classical'].include?(t) }.any? && tags.select{|t| ['pop', 'rock', 'crossover', 'alternative'].include?(t) }.none?
  rescue StandardError => e
    if e.message.match('last.fm response is just nil without exceptions')
      false
    else
      raise e
    end 
  end
end

#musicbrainz_date_to_iso_date(date) ⇒ Object



223
224
225
226
227
228
229
230
231
232
233
234
235
# File 'app/models/music_artist.rb', line 223

def musicbrainz_date_to_iso_date(date)
  return date if date.blank?
  
  splitted_date = date.split('-')
  
  if splitted_date.length == 1
    "#{splitted_date.first}-01-01"
  elsif splitted_date.length == 2
    "#{splitted_date.first}-#{splitted_date.last}-01"
  else
    date
  end
end

#release_groups(musicbrainz_artist, voluntary_releases, offset, without_limitation, with_releases = false) ⇒ Object



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'app/models/music_artist.rb', line 122

def release_groups(musicbrainz_artist, voluntary_releases, offset, without_limitation, with_releases = false)
  musicbrainz_artist = MusicBrainz::Artist.find(mbid) unless musicbrainz_artist
  count = 100
  working_release_groups = musicbrainz_artist.release_groups(extra_query: 'AND (type:album OR type:ep OR type:soundtrack)', offset: offset)
  count = working_release_groups.total_count
  working_release_groups = working_release_groups.select{|r| ['Album', 'Soundtrack', 'EP'].include?(r.type) && r.secondary_types.select{|st| MusicRelease::SECONDARY_TYPES_BLACKLIST.include?(st)}.none? && r.artists.length == 1}
  
  voluntary_releases += if working_release_groups.none?
    []
  else 
    releases.where('LOWER(music_releases.name) IN (?)', working_release_groups.map(&:title).uniq.map(&:downcase)).map{|r| "#{(r.is_lp ? 1 : 0)};#{r.name.downcase}"}
  end
  
  voluntary_releases.uniq!
  list = []
  
  working_release_groups = working_release_groups.select{|r| !voluntary_releases.include?("#{r.type == 'Album' ? 1 : 0};#{r.title.downcase}")}.each do |musicbrainz_release_group|
    release_is_lp_plus_name = "#{musicbrainz_release_group.type == 'Album' ? 1 : 0};#{musicbrainz_release_group.title.downcase}"
    
    next if voluntary_releases.include?(release_is_lp_plus_name)
    
    voluntary_releases << release_is_lp_plus_name
    
    unless without_limitation
      musicbrainz_release_group.releases = nil
      musicbrainz_releases = musicbrainz_release_group.releases
      
      next if musicbrainz_releases.select{|r| r.status == 'Official' && (r.media.map(&:format).none? || r.media.map(&:format).select{|f| !['DVD-Video', 'DVD'].include?(f) }.any?) }.none?
    end
    
    list << (with_releases ? [musicbrainz_release_group, musicbrainz_releases] : musicbrainz_release_group)
  end.select{|r| !r.nil?}
  
  [count, list] 
end

#to_jsonObject



237
238
239
240
241
242
243
244
# File 'app/models/music_artist.rb', line 237

def to_json
  { 
    id: id, mbid: mbid, name: name, is_ambiguous: is_ambiguous, 
    disambiguation: disambiguation, listeners: listeners, plays: plays,
    founded_at: founded_at.try(:iso8601), dissolved_at: dissolved_at.try(:iso8601), 
    state: state
  } 
end