Class: ISRC::PPLUK

Inherits:
Object
  • Object
show all
Defined in:
lib/isrc.rb

Instance Method Summary collapse

Instance Method Details

#match(opts = {}) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/isrc.rb', line 64

def match(opts = {})
  return {
    :isrc => 'No Match',
    :delta => -1
  } if @matches.count == 0

  return {
    :artist => @matches.first[0],
    :title => @matches.first[1],
    :isrc => @matches.first[2],
    :delta => 0
  } if @matches.count == 1

  seconds = opts[:time]

  if seconds
    # if string, convert to integer. Format '5:08'
    seconds = timestring_to_integer(seconds) if seconds.class == String
    match_quality = []

    @matches.each do |song_match|
      song_seconds = timestring_to_integer(song_match[5].match(/([0-9]:[0-9]{2})/)[1])
      match_quality << { :delta => (song_seconds - seconds).abs, :match => song_match }
    end

    best_match = match_quality.min_by { |m| m[:delta] }

    return {
      :artist => best_match[:match][0],
      :title => best_match[:match][1],
      :isrc => best_match[:match][2],
      :delta => best_match[:delta]
    }
  end

  nil
end

#retrieve(opts) ⇒ Object



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
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/isrc.rb', line 17

def retrieve(opts)
  # NOTE the online search is a bit funky: adding more to the search make the results worse
  # trying out a three word limit

  pieces = self.extract_song_peices(opts[:title])

  # try the first two pieces at first

  # if the song is only one word, submit request with on processing
  if pieces[:all].size == 1
    @matches = self.request(opts)
  elsif pieces[:all].size > 1
    # let's try increasing the number of pieces, starting from 2
    pieces_count = 1

    begin
      pieces_count += 1

      shortened_title = pieces[:all].slice(0, pieces_count).join(' ')

      @matches = self.request({
        title: shortened_title,
        artist: opts[:artist]
      })
    end while @matches.empty? && pieces[:all].size > pieces_count + 1

    # given 'Surrender [Original Mix]' the above algorithm will submit the entire title
    # if that didn't work, strip out all meta elements and try greatest to least number of song pieces

    # TODO we shouldn't allow one letter or two letter title requests

    pieces_count = pieces[:title].size

    while @matches.empty? && pieces_count > 0
      shortened_title = pieces[:title].slice(0, pieces_count).join(' ')

      @matches = self.request({
        title: shortened_title,
        artist: opts[:artist]
      })

      pieces_count -= 1
    end

  end
end