Class: AZLyrics

Inherits:
Lyrics show all
Defined in:
lib/lyrics/lyrics_AZLyrics.rb

Instance Attribute Summary

Attributes inherited from Lyrics

#cleanup_lyrics

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Lyrics

#build_google_feeling_lucky_url, #build_song_add_url, #decrease_tabulation_level, #fetch_lyrics_page, #fetch_suggestions_page, #increase_tabulation_level, #initialize, #known_url?, known_url?, #log, #log?, #logger=, #lyrics_direct_search, #lyrics_from_suggestions, #lyrics_from_url, #lyrics_full_search, #lyrics_test_data, #notify, #site_host, #site_name, #suggestions, #suggestions_test_data, suggestions_test_data, #verbose_log?

Constructor Details

This class inherits a constructor from Lyrics

Class Method Details

.build_google_feeling_lucky_url(artist, title = nil) ⇒ Object



48
49
50
51
52
53
# File 'lib/lyrics/lyrics_AZLyrics.rb', line 48

def AZLyrics.build_google_feeling_lucky_url( artist, title=nil )
	query = Strings.google_search_quote( artist )
	query << " " << Strings.google_search_quote( title ) if title
	query << " lyrics"
	return Strings.build_google_feeling_lucky_url( query, title ? "#{site_host()}/lyrics" : site_host() )
end

.build_song_add_url(request) ⇒ Object



44
45
46
# File 'lib/lyrics/lyrics_AZLyrics.rb', line 44

def AZLyrics.build_song_add_url( request )
	return "http://#{site_host()}/add.html"
end

.lyrics_test_dataObject



35
36
37
38
39
40
41
42
# File 'lib/lyrics/lyrics_AZLyrics.rb', line 35

def AZLyrics.lyrics_test_data()
	return [
		Request.new( "Radiohead", "Optimistic", "Kid A" ),
		Request.new( "Pearl Jam", "Alive", "Ten" ),
		Request.new( "Massive Attack", "Protection", "Protection" ),
		Request.new( "Portishead", "Wandering Star", "Dummy" ),
	]
end

.site_hostObject



27
28
29
# File 'lib/lyrics/lyrics_AZLyrics.rb', line 27

def AZLyrics.site_host()
	return "www.azlyrics.com"
end

.site_nameObject



31
32
33
# File 'lib/lyrics/lyrics_AZLyrics.rb', line 31

def AZLyrics.site_name()
	return "AZ Lyrics"
end

Instance Method Details

#build_lyrics_fetch_data(request) ⇒ Object



55
56
57
# File 'lib/lyrics/lyrics_AZLyrics.rb', line 55

def build_lyrics_fetch_data( request )
	return FetchPageData.new( build_google_feeling_lucky_url( request.artist, request.title ) )
end

#build_suggestions_fetch_data(request) ⇒ Object



86
87
88
# File 'lib/lyrics/lyrics_AZLyrics.rb', line 86

def build_suggestions_fetch_data( request )
	return FetchPageData.new( build_google_feeling_lucky_url( request.artist ) )
end

#lyrics_page_valid?(request, page_body, page_url) ⇒ Boolean

Returns:

  • (Boolean)


59
60
61
62
63
64
65
# File 'lib/lyrics/lyrics_AZLyrics.rb', line 59

def lyrics_page_valid?( request, page_body, page_url )
	md = /<TITLE>([^<]+) LYRICS - ([^<]+)<\/TITLE>/.match( page_body )
	return md ?
		Strings.normalize( request.artist ) == Strings.normalize( md[1] ) &&
		Strings.normalize( request.title ) == Strings.normalize( md[2] ) :
		false
end

#parse_lyrics(response, page_body) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/lyrics/lyrics_AZLyrics.rb', line 67

def parse_lyrics( response, page_body )

	page_body = Strings.latin12utf8( page_body )
	page_body.tr_s!( " \n\r\t", " " )

	if (md = /<B>([^<]+) LYRICS ?<\/B> ?<BR> ?<BR> ?<FONT size=2> ?<B> ?"([^<]+)" ?<\/b>/.match( page_body ))
		response.artist, response.title = Strings.titlecase( md[1], true, true ), md[2]
	end

	return if ! page_body.sub!( /^.*"<\/b><BR> ?<BR> ?/i, "" )
	return if ! page_body.sub!( /\ ?<BR> ?<BR> ?\[ <a href=.*$/i, "" )

	page_body.gsub!( /<i>\[Thanks to.*$/i, "" )
	page_body.gsub!( /\ ?<br ?\/?> ?/i, "\n" )
	page_body.gsub!( /\n{3,}/, "\n\n" )

	response.lyrics = page_body
end

#parse_suggestions(request, page_body, page_url) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/lyrics/lyrics_AZLyrics.rb', line 95

def parse_suggestions( request, page_body, page_url )

	page_body = Strings.latin12utf8( page_body )
	page_body.tr_s!( " \n\r\t", " " )

	suggestions = []

	return suggestions if ! page_body.gsub!( /.*<tr><td align=center valign=top> <font face=verdana size=5><br> ?<b>[^<]+ lyrics<\/b>/i, "" )
	return suggestions if ! page_body.gsub!( /<\/font> ?<\/font> ?<\/td> ?<\/tr> ?<\/table>.*$/i, "" )

	page_body.split( /<br>/i ).each() do |entry|
		if (md = /<a href="\.\.([^"]+)" target="_blank">([^<]+)<\/a>/i.match( entry ))
			suggestions << Suggestion.new( request.artist, md[2], "http://#{site_host()}#{md[1]}" )
		end
	end

	return suggestions
end

#suggestions_page_valid?(request, page_body, page_url) ⇒ Boolean

Returns:

  • (Boolean)


90
91
92
93
# File 'lib/lyrics/lyrics_AZLyrics.rb', line 90

def suggestions_page_valid?( request, page_body, page_url )
	md = /<TITLE>([^<]+) lyrics<\/TITLE>/.match( page_body )
	return md ? Strings.normalize( request.artist ) == Strings.normalize( md[1] ) : false
end