]>
Commit | Line | Data |
---|---|---|
823e558b | 1 | module Lyricli |
f2ec7254 BB |
2 | |
3 | # This class gets the lyrics according to a given artist and song name. | |
823e558b BB |
4 | class LyricsEngine |
5 | ||
6 | include Util | |
7 | ||
f2ec7254 BB |
8 | # Starts a new instance of LyricsEngine |
9 | # | |
10 | # @param [String] artist the artist | |
11 | # @param [String] song the song to look for | |
823e558b BB |
12 | def initialize(artist, song) |
13 | @provider = URI("http://lyrics.wikia.com/api.php?artist=#{sanitize_param artist}&song=#{sanitize_param song}&fmt=realjson") | |
14 | end | |
15 | ||
f2ec7254 BB |
16 | # Asks Lyrics Wiki for the lyrics, also cleans up the output a little. |
17 | # | |
18 | # @return [String] the lyrics | |
823e558b BB |
19 | def get_lyrics |
20 | begin | |
21 | response = Net::HTTP.get(@provider) | |
22 | response = MultiJson.decode(response) | |
23 | ||
24 | doc = Nokogiri::HTML(open(response['url'])) | |
25 | node = doc.search(".lyricbox").first | |
26 | rescue | |
76e96cc0 | 27 | raise Exceptions::LyricsNotFoundError |
823e558b BB |
28 | end |
29 | ||
30 | node.search(".rtMatcher").each do |n| | |
31 | n.remove | |
32 | end | |
33 | ||
b434f05e BB |
34 | node.search("script").each do |n| |
35 | n.remove | |
36 | end | |
37 | ||
823e558b BB |
38 | node.search("br").each do |br| |
39 | br.replace "\n" | |
40 | end | |
41 | ||
b434f05e | 42 | node.inner_text.gsub(/\s+$/, "") |
823e558b | 43 | end |
823e558b BB |
44 | end |
45 | end |