Class: AZLyrics

Inherits:
Lyrics show all
Defined in:
lib/wiki_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



47
48
49
50
51
52
# File 'lib/wiki_lyrics/lyrics_AZLyrics.rb', line 47

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



43
44
45
# File 'lib/wiki_lyrics/lyrics_AZLyrics.rb', line 43

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

.lyrics_test_dataObject



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

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



26
27
28
# File 'lib/wiki_lyrics/lyrics_AZLyrics.rb', line 26

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

.site_nameObject



30
31
32
# File 'lib/wiki_lyrics/lyrics_AZLyrics.rb', line 30

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

Instance Method Details

#build_lyrics_fetch_data(request) ⇒ Object



54
55
56
# File 'lib/wiki_lyrics/lyrics_AZLyrics.rb', line 54

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



85
86
87
# File 'lib/wiki_lyrics/lyrics_AZLyrics.rb', line 85

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)


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

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



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

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



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

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)


89
90
91
92
# File 'lib/wiki_lyrics/lyrics_AZLyrics.rb', line 89

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