Class: LyricsDownload

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



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

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



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

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

.site_nameObject



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

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

Instance Method Details

#build_lyrics_fetch_data(request) ⇒ Object



66
67
68
69
70
# File 'lib/lyrics/lyrics_LyricsDownload.rb', line 66

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



93
94
95
96
# File 'lib/lyrics/lyrics_LyricsDownload.rb', line 93

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



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

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



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

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



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

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



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

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