Class: DarkLyrics

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



48
49
50
51
52
53
# File 'lib/lyrics/lyrics_DarkLyrics.rb', line 48

def DarkLyrics.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



44
45
46
# File 'lib/lyrics/lyrics_DarkLyrics.rb', line 44

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

.lyrics_test_dataObject



35
36
37
38
39
40
41
42
# File 'lib/lyrics/lyrics_DarkLyrics.rb', line 35

def DarkLyrics.lyrics_test_data()
	return [
		Request.new( "Opeth", "Hope Leaves", "Damnation" ),
		Request.new( "Megadeth", "À tout le monde", "Youthanasia" ),
		Request.new( "Metallica", "Master of Puppets", "Master of Puppets" ),
		Request.new( "Lacuna Coil", "Heaven's a Lie", "Comalies" ),
	]
end

.site_hostObject



27
28
29
# File 'lib/lyrics/lyrics_DarkLyrics.rb', line 27

def DarkLyrics.site_host()
	return "www.darklyrics.com"
end

.site_nameObject



31
32
33
# File 'lib/lyrics/lyrics_DarkLyrics.rb', line 31

def DarkLyrics.site_name()
	return "Dark Lyrics"
end

Instance Method Details

#build_lyrics_fetch_data(request) ⇒ Object



55
56
57
# File 'lib/lyrics/lyrics_DarkLyrics.rb', line 55

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



96
97
98
# File 'lib/lyrics/lyrics_DarkLyrics.rb', line 96

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)


59
60
61
62
# File 'lib/lyrics/lyrics_DarkLyrics.rb', line 59

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] ) : false
end

#parse_lyrics(response, page_body) ⇒ Object



64
65
66
67
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
# File 'lib/lyrics/lyrics_DarkLyrics.rb', line 64

def parse_lyrics( response, page_body )

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

	if (md = /<FONT size=5 color=#FFFFCC>([^<]+) LYRICS<\/FONT><br>/.match( page_body ))
		response.artist = Strings.titlecase( md[1], true, true )
	end

	if (md = /<FONT size=3 color=white><b>([^<]+) \(([0-9]{4,4})( [^\)]+|)\)<\/b><\/FONT><br>/.match( page_body ))
		response.album, response.year = md[1], md[2]
	end

	return if ! page_body.sub!( /^.*<SCRIPT LANGUAGE="javascript" src="\.\.\/\.\.\/recban\.js"><\/SCRIPT><BR>/i, "" )
	return if ! page_body.sub!( /<FONT [^>]*size=.*$/i, "" )

	md = /.*#([0-9]+)$/.match( response.url )
	track = md ? md[1] : nil
	normalized_title = Strings.normalize( response.request.title )
	page_body.split( /<a name=[0-9]+><FONT color=#DDDDDD>/i ).each() do |song_content|
		md = /^<b>([0-9]+)\. ([^<]+)<\/b><\/font><br>(.*)$/.match( song_content )
		next if ! md || (track && track != md[1]) || (! track && normalized_title != Strings.normalize( md[2] ))
		response.title = md[2]
		page_body = md[3]
		page_body.gsub!( /\ ?<br ?\/?> ?/i, "\n" )
		page_body.gsub!( /\n{3,}/, "\n\n" )
		page_body.strip!()
		response.lyrics = page_body
	end

end

#parse_suggestions(request, page_body, page_url) ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/lyrics/lyrics_DarkLyrics.rb', line 105

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.sub!(/^.*<FONT FACE="Helvetica" COLOR=#FFFFCC SIZE=4><br>[^<]+ LYRICS<BR><\/FONT>/i,"")
	return suggestions if ! page_body.sub!( /<SCRIPT LANGUAGE="javascript".*$/i, "" )

	page_body.split( /<br>/i ).each() do |entry|
		if (md = /<a href="\.\.(\/lyrics\/[^"]+)" target="_blank"><FONT [^>]+>([^"]+)<\/FONT><\/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)


100
101
102
103
# File 'lib/lyrics/lyrics_DarkLyrics.rb', line 100

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