class LyricFind::API

Public Class Methods

new(search_api_key, display_api_key) click to toggle source
# File lib/lyricfind/get_lyrics_by_song_name.rb, line 6
def initialize search_api_key, display_api_key
  @search_key = search_api_key
  @display_key = display_api_key
end

Public Instance Methods

album(artist, song_name) click to toggle source
# File lib/lyricfind/album.rb, line 6
def album artist, song_name
  doc = get_search_api_response artist, song_name
  return nil if doc.nil?

  get_album doc
end
check_success(doc, code) click to toggle source
# File lib/lyricfind/get_lyrics_by_song_name.rb, line 40
def check_success doc, code
  begin
    response = doc.xpath('//response')
    response[0]['code'].to_i == code
  rescue
    false
  end
end
duration(artist, song_name) click to toggle source
# File lib/lyricfind/duration.rb, line 6
def duration artist, song_name
  doc = get_search_api_response artist, song_name
  return nil if doc.nil?

  get_duration doc

end
get_album(doc) click to toggle source
# File lib/lyricfind/get_song_info.rb, line 23
def get_album doc
  doc.xpath('//album')[0].content.gsub("\r\n", '').strip
end
get_duration(doc) click to toggle source
# File lib/lyricfind/get_song_info.rb, line 27
def get_duration doc
  doc.xpath('//tracks/track')[0]['duration']
end
get_instrumental(doc) click to toggle source
# File lib/lyricfind/get_song_info.rb, line 35
def get_instrumental doc
  doc.xpath('//tracks/track')[0]['instrumental'] == 'true'
end
get_lyrics(track) click to toggle source
# File lib/lyricfind/get_song_info.rb, line 43
def get_lyrics track
  begin
    query_url = URI.escape "http://api.lyricfind.com/lyric.do?apikey=#{@display_key}&reqtype=default&trackid=amg:#{track}"

    response = open(query_url).read
    doc = Nokogiri::HTML(response)
    doc.encoding = 'utf-8'

    return nil if !(check_success doc, 101)

    lyrics = doc.xpath('//lyrics')
    lyrics[0].content
  rescue Exception => ex
    nil
  end

end
get_lyrics_by_song_name(artist, song_name) click to toggle source
# File lib/lyricfind/get_lyrics_by_song_name.rb, line 11
def get_lyrics_by_song_name artist, song_name
  doc = get_search_api_response artist, song_name
  return if doc == nil

  get_lyrics get_track_amg doc
end
get_search_api_response(artist, song_name) click to toggle source
# File lib/lyricfind/get_lyrics_by_song_name.rb, line 18
def get_search_api_response artist, song_name
  return nil if artist.nil? or artist.length == 0
  return nil if song_name.nil? or song_name.length == 0

  artist = URI.escape artist
  song_name = URI.escape song_name

  begin
    query_url = URI.escape "http://api.lyricfind.com/search.do?apikey=#{@search_key}&reqtype=default&searchtype=track&artist=#{artist}&track=#{song_name}"
    response = open(query_url).read
    doc = Nokogiri::HTML(response)
    doc.encoding = 'utf-8'

    return nil if !(check_success doc, 100)
    tracks = doc.xpath('//tracks')[0]['totalresults'].to_i
    return nil if tracks == 0
    doc
  rescue Exception => ex
    nil
  end
end
get_snippet(doc) click to toggle source
# File lib/lyricfind/get_song_info.rb, line 31
def get_snippet doc
  doc.xpath('//snippet')[0].content.gsub("\r\n",'').strip
end
get_snippet_by_song_name(artist, song_name) click to toggle source
# File lib/lyricfind/get_snippet_by_song_name.rb, line 6
def get_snippet_by_song_name artist, song_name
  doc = get_search_api_response artist, song_name
  return nil if doc.nil?

  get_snippet doc

end
get_song_info(artist, song_name) click to toggle source
# File lib/lyricfind/get_song_info.rb, line 5
def get_song_info artist, song_name
  song_info = SongInfo.new

  doc = get_search_api_response artist, song_name
  return if doc == nil

  song_info.artist        = artist
  song_info.song_name     = song_name
  song_info.album         = get_album doc
  song_info.duration      = get_duration doc
  song_info.snippet       = get_snippet doc
  song_info.instrumental  = get_instrumental doc

  song_info.lyrics = get_lyrics get_track_amg doc

  song_info
end
get_track_amg(doc) click to toggle source
# File lib/lyricfind/get_song_info.rb, line 39
def get_track_amg doc
  doc.xpath('//tracks/track')[0]['amg']
end
instrumental?(artist, song_name) click to toggle source
# File lib/lyricfind/instrumental.rb, line 6
def instrumental? artist, song_name
  doc = get_search_api_response artist, song_name
  return nil if doc.nil?

  get_instrumental doc
end