Class: LyricsDownload

Inherits:
Lyrics
  • Object
show all
Defined in:
lib/wiki_lyrics/lyrics_LyricsDownload.rb

Constant Summary collapse

@@white_chars =
"'\"¿?¡!()[].,;:-/& "

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_google_feeling_lucky_url, build_song_add_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_page_valid?, #lyrics_test_data, #notify, #site_host, #site_name, #suggestions, #suggestions_page_valid?, #suggestions_test_data, suggestions_test_data, #verbose_log?

Constructor Details

This class inherits a constructor from Lyrics

Class Method Details

.lyrics_test_dataObject



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

def LyricsDownload.lyrics_test_data()
	return [
		Request.new( "Nirvana", "Smells Like Teen Spirit", "Nevermind" ),
		Request.new( "Radiohead", "Optimistic", "Kid A" ),
		Request.new( "Massive Attack", "Protection", "Protection" ),
		Request.new( "Portishead", "Wandering Star", "Dummy" ),
	]
end

.site_hostObject



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

def LyricsDownload.site_host()
	return "www.lyricsdownload.com"
end

.site_nameObject



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

def LyricsDownload.site_name()
	return "Lyrics Download"
end

Instance Method Details

#build_lyrics_fetch_data(request) ⇒ Object



65
66
67
68
69
# File 'lib/wiki_lyrics/lyrics_LyricsDownload.rb', line 65

def build_lyrics_fetch_data( request )
	artist = cleanup_title( request.artist )
	title = cleanup_title( request.title )
	return FetchPageData.new( "http://#{site_host()}/#{artist}-#{title}-lyrics.html" )
end

#build_suggestions_fetch_data(request) ⇒ Object



92
93
94
95
# File 'lib/wiki_lyrics/lyrics_LyricsDownload.rb', line 92

def build_suggestions_fetch_data( request )
	artist = cleanup_artist( request.artist )
	return FetchPageData.new( "http://#{site_host()}/#{artist}-lyrics.html" )
end

#cleanup_artist(artist) ⇒ Object



46
47
48
49
50
51
52
53
54
# File 'lib/wiki_lyrics/lyrics_LyricsDownload.rb', line 46

def cleanup_artist( artist )
	artist = artist.gsub( /^the /i, "" )
	Strings.remove_vocal_accents!( artist )
	artist.gsub!( "&", "and" )
	artist.tr!( @@white_chars, " " )
	artist.strip!()
	artist.tr!( " ", "-" )
	return artist
end

#cleanup_title(title) ⇒ Object



56
57
58
59
60
61
62
63
# File 'lib/wiki_lyrics/lyrics_LyricsDownload.rb', line 56

def cleanup_title( title )
	title = Strings.remove_vocal_accents( title )
	title.gsub!( "&", "and" )
	title.tr!( @@white_chars, " " )
	title.strip!()
	title.tr!( " ", "-" )
	return title
end

#parse_lyrics(response, page_body) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/wiki_lyrics/lyrics_LyricsDownload.rb', line 71

def parse_lyrics( response, page_body )

	page_body = Strings.latin12utf8( page_body )
	page_body.tr_s!( " \n\r\t", " " )
	page_body.tr_s!( "’", "'" )
# 		page_body.tr_s!( "‘", "'" )

	if (md = /<title>([^<]+) - ([^<]+) LYRICS ?<\/title>/i.match( page_body ))
		response.artist, response.title = md[1].strip(), md[2].strip()
	end

	return if ! page_body.gsub!( /^.*<div class="KonaBody" ><div id="div_customCSS">/, "" )
	return if ! page_body.gsub!( /<\/div> ?<\/div>.*$/, "" )

	page_body.gsub!( /\ ?<br ?\/?> ?/i, "\n" )
	page_body.strip!()

	response.lyrics = page_body

end

#parse_suggestions(request, page_body, page_url) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/wiki_lyrics/lyrics_LyricsDownload.rb', line 97

def parse_suggestions( request, page_body, page_url )

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

	suggestions = []

	return suggestions if ! page_body.sub!( /^.*Lyrics list aplhabetically:<\/font><\/td>/, "" )
	return suggestions if ! page_body.sub!( /<\/ul> ?<\/td> ?<\/tr> ?<\/table> ?<center><div>.*$/, "" )

	page_body.split( "</li>" ).each() do |entry|
		if (md = /<a class="txt_1" href="([^"]+)"><font size=2>([^<]+) Lyrics<\/font><\/a>/.match( entry ))
			suggestions << Suggestion.new( request.artist, md[2], "http://#{site_host()}/#{md[1]}" )
		end
	end

	return suggestions

end