Sha256: 4f8b2cf6df5c0486d4525178bf9c9e4a9fc95c88b978d9ffee7307847de39f75

Contents?: true

Size: 1.27 KB

Versions: 2

Compression:

Stored size: 1.27 KB

Contents

require 'open-uri'
require 'nokogiri'

module LyricFind
  class API
    def initialize search_api_key, display_api_key
      @search_key = search_api_key
      @display_key = display_api_key
    end

    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

    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

    def check_success doc, code
      begin
        response = doc.xpath('//response')
        response[0]['code'].to_i == code
      rescue
        false
      end
    end

  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
LyricFind-0.1.7 lib/lyricfind/get_lyrics_by_song_name.rb
LyricFind-0.1.6 lib/lyricfind/get_lyrics_by_song_name.rb