Class: LoudSongs

Inherits:
Lyrics show all
Defined in:
lib/lyrics/lyrics_LoudSongs.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/lyrics/lyrics_LoudSongs.rb', line 50

def LoudSongs.build_google_feeling_lucky_url( artist, title=nil )
	query = Strings.google_search_quote( title ? artist : artist + " lyrics" )
	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/lyrics/lyrics_LoudSongs.rb', line 46

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

.lyrics_test_dataObject



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

def LoudSongs.lyrics_test_data()
	return [
		Request.new( "Radiohead", "Optimistic", "Kid A" ),
		Request.new( "Nirvana", "About a Girl", "Bleach" ),
		Request.new( "Placebo", "Taste in Men", "Black Market Music" ),
		Request.new( "A Perfect Circle", "The Noose", "Thirteen Step" ),
	]
end

.site_hostObject



29
30
31
# File 'lib/lyrics/lyrics_LoudSongs.rb', line 29

def LoudSongs.site_host()
	return "www.loudson.gs"
end

.site_nameObject



33
34
35
# File 'lib/lyrics/lyrics_LoudSongs.rb', line 33

def LoudSongs.site_name()
	return "LoudSongs"
end

Instance Method Details

#build_lyrics_fetch_data(request) ⇒ Object



56
57
58
# File 'lib/lyrics/lyrics_LoudSongs.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



91
92
93
# File 'lib/lyrics/lyrics_LoudSongs.rb', line 91

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

def lyrics_page_valid?( request, page_body, page_url )
	md = /<title>([^<]+) - [^<]+ - ([^<]+) lyrics<\/title>/i.match( page_body )
	return false if ! md
	return	Strings.normalize( md[1] ) == Strings.normalize( request.artist ) &&
			Strings.normalize( md[2] ) == Strings.normalize( request.title )
end

#parse_lyrics(response, page_body) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/lyrics/lyrics_LoudSongs.rb', line 67

def parse_lyrics( response, page_body )

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

	if (md = /<h1>Lyrics for: ([^<]+) - ([^<]+)<\/h1> <h1>([0-9])+\) ([^<]+)<\/h1>/.match( page_body ))
		response.artist = Strings.titlecase( md[1].strip() )
		response.album = Strings.titlecase( md[2].strip() )
		response.title = Strings.titlecase( md[4].strip() )
		response.custom_data = { "track" => md[3] }
	end

	if (md = /<em>Release Year:<\/em> ?([0-9]{4}) ?<br \/>/.match( page_body ))
		response.year = md[1]
	end

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

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

	response.lyrics = page_body

end

#parse_suggestions(request, page_body, page_url) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/lyrics/lyrics_LoudSongs.rb', line 99

def parse_suggestions( request, page_body, page_url )

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

	suggestions = []

	if page_url.count( "/" ) == 4 # ARTIST PAGE

		return suggestions if ! page_body.sub!( /^.*<ul>/i, "" )
		return suggestions if ! page_body.sub!( /<\/ul>.*$/i, "" )

		page_body.split( /<\/li> ?<li>/ ).each() do |album_entry|
			if (md = /<a href="([^"]+)">/.match( album_entry ))
				suggestions << FetchPageData.new( md[1].downcase() )
			end
		end

	else # page_url.count( "/" ) == 3 # ALBUM PAGE

		return suggestions if ! page_body.sub!( /^.*<ul>/i, "" )
		return suggestions if ! page_body.sub!( /<\/ul>.*$/i, "" )

		HTMLEntities.decode!( page_body )

		page_body.split( /<\/li> ?<li>/ ).each() do |song_entry|
			if (md = /<a href="([^"]+)">[0-9]+. ([^<]+)<\/a>/.match( song_entry ))
				suggestions << Suggestion.new( request.artist, md[2], md[1].downcase() )
			end
		end

	end

	return suggestions

end

#suggestions_page_valid?(request, page_body, page_url) ⇒ Boolean

Returns:

  • (Boolean)


95
96
97
# File 'lib/lyrics/lyrics_LoudSongs.rb', line 95

def suggestions_page_valid?( request, page_body, page_url )
	return page_url.index( "http://#{site_host()}/" ) == 0 # TODO
end