Class: MusicArtist

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

Instance Method Summary collapse

Methods included from LastfmRequest

included, #lastfm_request

Instance Method Details

#bonus_tracks_releaseObject



202
203
204
# File 'app/models/music_artist.rb', line 202

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

#import_bonus_tracksObject



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'app/models/music_artist.rb', line 141

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



169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
# File 'app/models/music_artist.rb', line 169

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



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'app/models/music_artist.rb', line 79

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)


56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'app/models/music_artist.rb', line 56

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



206
207
208
209
210
211
212
213
214
215
216
217
218
# File 'app/models/music_artist.rb', line 206

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



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'app/models/music_artist.rb', line 105

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