Class: LyricsMania

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



50
51
52
53
54
# File 'lib/wiki_lyrics/lyrics_LyricsMania.rb', line 50

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



46
47
48
# File 'lib/wiki_lyrics/lyrics_LyricsMania.rb', line 46

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

.lyrics_test_dataObject



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

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



29
30
31
# File 'lib/wiki_lyrics/lyrics_LyricsMania.rb', line 29

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

.site_nameObject



33
34
35
# File 'lib/wiki_lyrics/lyrics_LyricsMania.rb', line 33

def LyricsMania.site_name()
	return "LyricsMania"
end

Instance Method Details

#build_lyrics_fetch_data(request) ⇒ Object



56
57
58
# File 'lib/wiki_lyrics/lyrics_LyricsMania.rb', line 56

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



108
109
110
# File 'lib/wiki_lyrics/lyrics_LyricsMania.rb', line 108

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)


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

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



68
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
# File 'lib/wiki_lyrics/lyrics_LyricsMania.rb', line 68

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



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

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)


112
113
114
115
# File 'lib/wiki_lyrics/lyrics_LyricsMania.rb', line 112

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