Class: TerraLetras

Inherits:
Lyrics show all
Defined in:
lib/lyrics/lyrics_TerraLetras.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?, #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

.build_google_feeling_lucky_url(artist, title = nil) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/lyrics/lyrics_TerraLetras.rb', line 55

def TerraLetras.build_google_feeling_lucky_url( artist, title=nil )
	if title
		query = Strings.google_search_quote( "#{title} letra" )
		query << " "
		query << Strings.google_search_quote( artist )
	else
		query = Strings.google_search_quote( "letras de #{artist}" )
		query << " "
		query << Strings.google_search_quote( "#{artist} letras" )
	end
	return Strings.build_google_feeling_lucky_url( query, site_host() )
end

.build_song_add_url(request) ⇒ Object



51
52
53
# File 'lib/lyrics/lyrics_TerraLetras.rb', line 51

def TerraLetras.build_song_add_url( request )
	return "http://#{site_host()}/envie_artista.php"
end

.known_url?(url) ⇒ Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/lyrics/lyrics_TerraLetras.rb', line 38

def TerraLetras.known_url?( url )
	return url.index( "http://#{site_host}" ) == 0 || /^http:\/\/.*\.letras\.terra\.com\.br\/letras\/[0-9]+/.match( url )
end

.lyrics_test_dataObject



42
43
44
45
46
47
48
49
# File 'lib/lyrics/lyrics_TerraLetras.rb', line 42

def TerraLetras.lyrics_test_data()
	return [
		Request.new( "The Cranberries", "Linger", "Everybody Else Is Doing It, So Why Can't We?" ),
		Request.new( "Radiohead", "Optimistic", "Kid A" ),
		Request.new( "Mark Lanegan", "One Way Street", "Field Songs" ),
		Request.new( "U2", "One", "Achtung Baby" ),
	]
end

.site_hostObject



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

def TerraLetras.site_host()
	return "letras.terra.com.br"
end

.site_nameObject



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

def TerraLetras.site_name()
	return "Terra Letras"
end

Instance Method Details

#build_lyrics_fetch_data(request) ⇒ Object



68
69
70
71
72
# File 'lib/lyrics/lyrics_TerraLetras.rb', line 68

def build_lyrics_fetch_data( request )
	artist = Strings.utf82latin1( request.artist )
	title  = Strings.utf82latin1( request.title )
	return FetchPageData.new( "http://#{site_host()}/winamp.php?musica=#{CGI.escape(title)}&artista=#{CGI.escape(artist)}" )
end

#build_suggestions_fetch_data(request) ⇒ Object



102
103
104
# File 'lib/lyrics/lyrics_TerraLetras.rb', line 102

def build_suggestions_fetch_data( request )
	return FetchPageData.new( build_google_feeling_lucky_url( request.artist ) )
end

#parse_lyrics(response, page_body) ⇒ Object



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
# File 'lib/lyrics/lyrics_TerraLetras.rb', line 74

def parse_lyrics( response, page_body )

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

	if response.url.index( "http://#{site_host}/winamp.php?" ) == 0 # direct fetch
		if (md = /<h1><a href='[^']+' target='_blank'>([^<]+)<\/a><\/h1>/.match( page_body ))
			response.title = md[1]
		end
		if (md = /<h2><a href='[^']+' target='_blank'>([^<]+)<\/a><\/h2>/.match( page_body ))
			response.artist = md[1]
		end
		return if ! page_body.gsub!( /^.*corrigir letra<\/a><\/p><p>/, "" )
		return if ! page_body.gsub!( /<\/p>.*$/, "" )
	else # fetched from suggestions/url
		if (md = /<h2>([^<]+)<\/h2> <h2 id='sz'>([^<]+)<\/h2>/.match( page_body ))
			response.title, response.artist = md[1], md[2]
		end
		return if ! page_body.sub!( /^.*<p id='cmp'>[^<]*<\/p> <p>/, "" )
		return if ! page_body.sub!( /<\/p>.*/, "" )
	end

	page_body.gsub!( /\ ?<br ?\/?> ?/i, "\n" )
	page_body.gsub!( /\n{3,}/, "\n\n" )
	response.lyrics = page_body

end

#parse_suggestions(request, page_body, page_url) ⇒ Object



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

def parse_suggestions( request, page_body, page_url )

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

	suggestions = []

	return suggestions if ! page_body.gsub!( /^.*<ul class='top' id='bgn'>/, "" )
	return suggestions if ! page_body.gsub!( /<\/ul>.*$/, "" )

	page_body.split( /<\/li> ?<li>/ ).each do |entry|
		if (md = /<a href="([^"]+)">([^<]+) - ([^<]+)<\/a>/.match( entry ))
			suggestions << Suggestion.new( md[2], md[3], "http://#{site_host()}#{md[1]}" )
		end
	end

	return suggestions

end