Class: Sh::Fingerprint

Inherits:
Object show all
Defined in:
lib/sh_fingerprint.rb

Class Method Summary collapse

Class Method Details

.identify!(song) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/sh_fingerprint.rb', line 6

def self.identify! song
  Log.debug "Earworming #{song.path}"
  ew = Earworm::Client.new(KEYS[:music_dns])
  info = ew.identify :file => song.path
  title = info.title.unescape_html
  artist_name = info.artist_name.unescape_html
  puids = info.puid_list || []
  return false unless title and artist_name

  query = MusicBrainz::Webservice::Query.new
  tracks = query.get_tracks :title => title, :artist => artist_name, :puid => puids.first
  track = tracks.first.entity
  # Sort track matches with same score to find most appropriate one
  all_matches = tracks.reject {|t| t.score < tracks.first.score}
  all_matches.each do |entry|
    t = entry.entity
    rel = t.releases.first
    if Album.first(:title => rel.title)
      track = t
      break
    elsif rel.types.include? MusicBrainz::Model::Release::TYPE_ALBUM
      track = t
    end
  end
  artist = track.artist
  release = track.releases.first
  album_artist = query.get_release_by_id(release.id.uuid, :artist => true).artist
  track_num = release.tracks.offset + 1

  song.title = title
  song.track_num = track_num
  song.album = Database.get_or_insert_album(:title => release.title)
  song.album.mbid = release.id.uuid
  song.album.artist = Database.get_or_insert_artist(:name => album_artist.name)
  song.album.artist.mbid = album_artist.id.uuid
  song.album.artist.save
  song.album.save
  song.artist = Database.get_or_insert_artist(:name => artist.name)
  song.artist.mbid = artist.id.uuid
  song.artist.save
  song.save

  return true
end