Class: LyricsMania

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



51
52
53
54
55
# File 'lib/lyrics/lyrics_LyricsMania.rb', line 51

def LyricsMania.build_google_feeling_lucky_url( artist, title=nil )
	query =  Strings.google_search_quote( artist )
	query << " " << Strings.google_search_quote( title + " lyrics" ) if title
	return Strings.build_google_feeling_lucky_url( query, site_host() )
end

.build_song_add_url(request) ⇒ Object



47
48
49
# File 'lib/lyrics/lyrics_LyricsMania.rb', line 47

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

.lyrics_test_dataObject



38
39
40
41
42
43
44
45
# File 'lib/lyrics/lyrics_LyricsMania.rb', line 38

def LyricsMania.lyrics_test_data()
	return [
		Request.new( "Nirvana", "Lounge Act", "Nevermind" ),
		Request.new( "Radiohead", "Idioteque", "Kid A" ),
		Request.new( "Pearl Jam", "Porch", "Ten" ),
		Request.new( "The Smashing Pumpkins", "Mayonaise", "Siamese Dream" ),
	]
end

.site_hostObject



30
31
32
# File 'lib/lyrics/lyrics_LyricsMania.rb', line 30

def LyricsMania.site_host()
	return "www.lyricsmania.com"
end

.site_nameObject



34
35
36
# File 'lib/lyrics/lyrics_LyricsMania.rb', line 34

def LyricsMania.site_name()
	return "LyricsMania"
end

Instance Method Details

#build_lyrics_fetch_data(request) ⇒ Object



57
58
59
# File 'lib/lyrics/lyrics_LyricsMania.rb', line 57

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



109
110
111
# File 'lib/lyrics/lyrics_LyricsMania.rb', line 109

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)


61
62
63
64
65
66
67
# File 'lib/lyrics/lyrics_LyricsMania.rb', line 61

def lyrics_page_valid?( request, page_body, page_url )
	md = /<title>([^<]+) Lyrics<\/title>/i.match( page_body )
	return false if ! md
	page_title = Strings.normalize( md[1] )
	return	page_title.index( Strings.normalize( request.artist ) ) &&
			page_title.index( Strings.normalize( request.title ) )
end

#parse_lyrics(response, page_body) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/lyrics/lyrics_LyricsMania.rb', line 69

def parse_lyrics( response, page_body )

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

	return if ! page_body.sub!( /^.* lyrics<\/h3>/, "" ) # metadata

	 = {}
	["artist", "album"].each() do |key|
		if (md =/#{key}: <b><a href=[^>]+>([^<]+)<\/a><\/b>/i.match( page_body ))
			[key.downcase()] = md[1].strip().sub( /\ *lyrics$/, "" )
		end
	end
	["year", "title"].each() do |key|
		if (md =/#{key}: ([^<]+)<(br|\/td)>/i.match( page_body ))
			[key.downcase()] = md[1].strip()
		end
	end

	response.artist = ["artist"] if .include?( "artist" )
	response.title = ["title"] if .include?( "title" )
	response.album = ["album"] if .include?( "album" )
	response.year = ["year"] if .include?( "year" )

	md = /<\/span> ?<\/center>(.*)<center> ?<span style/.match( page_body )
	return if ! md

	page_body = md[1]
	page_body.sub!( /&#91;.+ Lyrics on http:\/\/#{site_host()}\/ &#93;/, "" )
	page_body.sub!( /^.*<\/a>/, "" ) # additional (optional) crap at the beginning
	page_body.gsub!( /<u>&lt;a[^<]+&lt;\/a&gt;<\/u>/, "" ) # yet more crap
	page_body.gsub!( /\ ?<br ?\/?> ?/i, "\n" )
	page_body.sub!( /^\ ?<strong>Lyrics to [^<]+<\/strong> :<\/?br> */i, "" )

	page_body.strip!()

	response.lyrics = page_body

end

#parse_suggestions(request, page_body, page_url) ⇒ Object

returns an array of maps with following keys: url, artist, title



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/lyrics/lyrics_LyricsMania.rb', line 119

def parse_suggestions( request, page_body, page_url )

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

	suggestions = []

	# remove table with other artists at the bottom
	return suggestions if ! page_body.sub!( /(.*)<table.*/, "\\1" )

	md = /<table width=100%>(.*)<\/table>/.match( page_body )
	return suggestions if ! md

	md[1].split( /<a href=/ ).each() do |entry|
		if (md = /"(\/lyrics\/[^"]+)" title="[^"]+"> ?([^>]+) lyrics<\/a><br>/.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)


113
114
115
116
# File 'lib/lyrics/lyrics_LyricsMania.rb', line 113

def suggestions_page_valid?( request, page_body, page_url )
	md = /<title>([^<]+) Lyrics<\/title>/i.match( page_body )
	return md ? Strings.normalize( md[1] ).index( Strings.normalize( request.artist ) ) : nil
end