Class: Lyriki

Inherits:
MediaWikiLyrics show all
Defined in:
lib/lyrics/lyrics_Lyriki.rb

Instance Attribute Summary

Attributes inherited from MediaWikiLyrics

#password, #username

Attributes inherited from Lyrics

#cleanup_lyrics

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from MediaWikiLyrics

#authorized?, #build_album_art_description, #build_album_art_name, build_album_edit_url, #build_album_edit_url, build_album_link, #build_album_link, #build_album_page, #build_album_rawdata_url, build_album_rawdata_url, #build_album_search_url, build_album_search_url, #build_album_url, build_album_url, build_date, build_link, #build_link, #build_lyrics_fetch_data, #build_rawdata_url, build_rawdata_url, build_song_add_url, #build_song_add_url, #build_song_edit_url, build_song_edit_url, build_song_link, #build_song_link, #build_song_page, #build_song_rawdata_url, build_song_rawdata_url, #build_song_search_url, build_song_search_url, #build_song_url, build_song_url, #build_suggestions_fetch_data, #build_tracks, #build_url, build_url, #cleanup_article, cleanup_article, cleanup_title_token, #cleanup_title_token, #cleanup_title_token!, #control_page, fetch_content_page, #fetch_content_page, #fetch_lyrics_page, #find_album_art_name, find_album_art_url, #find_album_art_url, find_album_page_url, #find_album_page_url, #find_song_page_url, find_song_page_url, #get_session_params, #get_sort_letter, get_sort_letter, get_sort_name, #get_sort_name, get_sort_name!, #get_sort_name!, #initialize, #logged_in?, #login, #logout, #lyrics_from_url, #lyrics_page_valid?, parse_album_link, #parse_album_link, #parse_album_url, parse_album_url, parse_date, #parse_link, parse_link, #parse_search_results, parse_search_results, #parse_song_link, parse_song_link, #parse_song_url, parse_song_url, #parse_suggestions, parse_template, #parse_template, #parse_url, parse_url, #prepare_image_file, prepare_image_file, #restore_session, #restore_session_params, #save_session, #submit_album_page, #submit_page, #submit_redirect_page, #submit_song_page, #upload_cover_image, #upload_file, #version, version

Methods inherited from Lyrics

build_google_feeling_lucky_url, #build_google_feeling_lucky_url, #build_lyrics_fetch_data, #build_song_add_url, build_song_add_url, #build_suggestions_fetch_data, #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, 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 MediaWikiLyrics

Class Method Details

.build_album_art_description(artist, album, year, cleanup = true) ⇒ Object



214
215
216
217
218
219
220
# File 'lib/lyrics/lyrics_Lyriki.rb', line 214

def Lyriki.build_album_art_description( artist, album, year, cleanup=true )
	if cleanup
		artist = cleanup_title_token( artist )
		album = cleanup_title_token( album )
	end
	return "#{artist}:#{album} (#{year})"
end

.build_album_art_name(artist, album, year, extension = "jpg", cleanup = true) ⇒ Object



205
206
207
208
209
210
211
212
# File 'lib/lyrics/lyrics_Lyriki.rb', line 205

def Lyriki.build_album_art_name( artist, album, year, extension="jpg", cleanup=true )
	if cleanup
		artist = cleanup_title_token( artist )
		album = cleanup_title_token( album )
	end
	album_art_name = "AlbumArt-#{artist}-#{album}_(#{year})#{Strings.empty?( extension ) ? "" : ".#{extension.strip()}"}".gsub( " ", "_" )
	return Strings.remove_invalid_filename_chars( album_art_name )
end

.build_album_page(reviewed, artist, album, year, month, day, tracks, album_art) ⇒ Object

Raises:

  • (ArgumentError)


140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/lyrics/lyrics_Lyriki.rb', line 140

def Lyriki.build_album_page( reviewed, artist, album, year, month, day, tracks, album_art )

	raise ArgumentError if Strings.empty?( artist ) || Strings.empty?( album ) || Strings.empty?( tracks )

	s_name = get_sort_name( album )
	s_letter = get_sort_letter( album )

	album_art = nil if ! year || album_art == build_album_art_name( artist, album, year, "jpg", false )

	contents = \
	"#{reviewed ? "" : "{{autoGenerated}}\n"}" \
	"{{Album\n" \
	"| album    = #{album}\n" \
	"| artist   = #{artist}\n" \
	"| released = #{build_date( year, month, day )}\n" \
	"#{album_art ? "| image    = #{album_art}\n" : ""}" \
	"| tracks   =\n"

	return \
	"#{contents}" \
	"#{tracks.strip()}\n" \
	"}}\n" \
	"\n" \
	"{{C:Album|#{s_letter}|#{s_name}}}"
end

.build_song_page(reviewed, artist, title, album, year, credits, lyricist, lyrics) ⇒ Object

Raises:

  • (ArgumentError)


166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
# File 'lib/lyrics/lyrics_Lyriki.rb', line 166

def Lyriki.build_song_page( reviewed, artist, title, album, year, credits, lyricist, lyrics )

	raise ArgumentError if artist == nil || title == nil

	s_name = get_sort_name( title )
	s_letter = get_sort_letter( title )
	year = year.to_i() <= 1900 ? "" : year.to_s()

	song_page = reviewed ? "": "{{autoGenerated}}\n"

	if (md = /^([^\s].*)\s+feat\.\s+([^\s].*)$/i.match( artist.strip() ))
		artist, fartist = md[1].strip(), md[2]
		song_page <<
		"{{Song\n" \
		"| song     = #{title}\n" \
		"| artists  = {{song artist|#{artist}}}<br />feat. {{song artist|#{fartist}}}\n" \
		"| albums   = {{song album|#{artist}|#{album}|#{year}}}\n" \
		"| credits  = #{credits.to_s().split( "; " ).join( "<br />" )}\n" \
		"| lyricist = #{lyricist.to_s().split( "; " ).join( "<br />" )}\n" \
		"}}\n" \
	else
		song_page <<
		"{{Song\n" \
		"| song     = #{title}\n" \
		"| artist   = #{artist}\n" \
		"| album    = #{album}\n" \
		"| year     = #{year}\n" \
		"| credits  = #{credits.to_s().split( "; " ).join( "<br />" )}\n" \
		"| lyricist = #{lyricist.to_s().split( "; " ).join( "<br />" )}\n" \
		"}}\n" \
	end

	return song_page <<
	"\n" \
	"<lyrics>#{Strings.empty?( lyrics ) ? "<tt>(Instrumental)</tt>" : lyrics}</lyrics>\n" \
	"\n" \
	"{{C:Song|#{s_letter}|#{s_name}}}"
end

.build_tracks(album_data) ⇒ Object



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/lyrics/lyrics_Lyriki.rb', line 123

def Lyriki.build_tracks( album_data )
	ret = ""
	album_data.tracks.each() do |track|
		track_length = track.length > 0 ?
			"|#{track.length / 60}:#{track.length % 60 < 10 ? "0#{track.length % 60}" : track.length % 60}" :
			""
		track_artist = cleanup_title_token( track.artist )
		track_title  = cleanup_title_token( track.title )
		if album_data.various_artists?
			ret += "# {{song link va|#{track_artist}|#{track_title}#{track_length}}}\n"
		else
			ret += "# {{song link|#{track_artist}|#{track_title}#{track_length}}}\n"
		end
	end
	return ret
end

.cleanup_title_token!(title, downcase = false) ⇒ Object



275
276
277
278
279
280
281
282
283
284
285
# File 'lib/lyrics/lyrics_Lyriki.rb', line 275

def Lyriki.cleanup_title_token!( title, downcase=false )
	title.gsub!( /\[[^\]\[]*\]/, "" )
	title.gsub!( /[\[|\]].*$/, "" )
	title.gsub!( /`|´|’/, "'" )
	title.gsub!( /''|«|»/, "\"" )
	title.squeeze!( " " )
	title.strip!()
	title.gsub!( "+", "and" )
	Strings.titlecase!( title, true, downcase )
	return title
end

.control_pageObject



39
40
41
# File 'lib/lyrics/lyrics_Lyriki.rb', line 39

def Lyriki.control_page()
	return "Lyriki:Wiki-Lyrics"
end

.find_album_art_name(artist, album, year) ⇒ Object



222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
# File 'lib/lyrics/lyrics_Lyriki.rb', line 222

def Lyriki.find_album_art_name( artist, album, year )

	normalized_artist = cleanup_title_token( artist )
	Strings.remove_invalid_filename_chars!( normalized_artist )
	Strings.normalize!( normalized_artist )
	normalized_artist.gsub!( " ", "" )

	normalized_album = cleanup_title_token( album )
	Strings.remove_invalid_filename_chars!( normalized_album )
	Strings.normalize!( normalized_album )
	normalized_album.gsub!( " ", "" )

	year = year.to_s().strip()

	artist = cleanup_title_token( artist )
	Strings.remove_invalid_filename_chars!( artist )
	search_url = "http://#{site_host()}/index.php?ns6=1&search=#{CGI.escape( artist )}&searchx=Search&limit=500"
	response, search_url = HTTP.fetch_page_get( search_url )

	return nil if response == nil || response.body() == nil

	candidates = []
	parse_search_results( response.body(), true ).each() do |result|

		next if result[@@SEARCH_RESULT_TITLE].index( "Image:" ) != 0

		normalized_title = Strings.normalize( result[@@SEARCH_RESULT_TITLE] )
		normalized_title.gsub!( " ", "" )

		matches = 0
		idx1 = normalized_title.index( "albumart" )
		matches += 1 if idx1
		idx1 = idx1 ? idx1 + "albumart".size : 0
		idx2 = normalized_title.index( normalized_artist, idx1 )
		matches += 4 if idx2
		idx2 = idx2 ? idx2 + normalized_artist.size : idx1
		idx3 = normalized_title.index( normalized_album, idx2 )
		next if idx3 == nil
		idx3 = idx3 ? idx3 + normalized_album.size : idx2
		idx3 = normalized_title.index( year, idx3 )
		matches += 2 if idx3

		candidates.insert( -1, [ matches, result[@@SEARCH_RESULT_TITLE] ] )
	end

	if candidates.size > 0
		candidates.sort!() { |x,y| y[0] <=> x[0] }
		return URI.decode( candidates[0][1].slice( "Image:".size..-1 ).gsub( " ", "_" ) )
	else
		return nil
	end
end

.site_hostObject



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

def Lyriki.site_host()
	return "www.lyriki.com"
end

.site_nameObject



35
36
37
# File 'lib/lyrics/lyrics_Lyriki.rb', line 35

def Lyriki.site_name()
	return "Lyriki"
end

Instance Method Details

#parse_lyrics(response, page_body) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/lyrics/lyrics_Lyriki.rb', line 43

def parse_lyrics( response, page_body )

	custom_data = {}

	if (md = /\{\{\s*[Ss]ong\s*\|.*$/m.match( page_body ))

		template_data = parse_template( md[0] )
		template_data["params"].each() do |key, value|
			custom_data[key.to_s()] = value
			if value.is_a?( String )
				value.gsub!( /<br ?\/?>/i, "; " ) if key != "song"
				value.gsub!( /<\/?[^>]+\/?>/, " " )
				value.tr_s!( " \n\r\t", " " )
				value.strip!()
			end
		end

		response.title = custom_data["song"] if custom_data.include?( "song" )

		if custom_data["artist"].is_a?( String )
			response.artist = custom_data["artist"]
		elsif custom_data["artists"].is_a?( Array )
			artist = ""
			custom_data["artists"].each() do |token|
				if token.is_a?( String )
					artist << token.gsub( /\[\[|\]\]/, "" )
				elsif token.is_a?( Hash ) && token["name"].downcase() == "song artist" && token["params"][1]
					artist << token["params"][1]
				end
			end
			artist.gsub!( /\s*<br *\/?>\s*/i, " " )
			response.artist = artist
		elsif custom_data["artists"].is_a?( String ) # this case is DEPRECATED by Lyriki guides
			response.artist = custom_data["artists"].gsub( /\[\[|\]\]/, "" )
		end

		if custom_data["album"].is_a?( String )
			response.album, response.year = custom_data["album"], custom_data["year"]
		elsif custom_data["albums"].is_a?( Array )
			custom_data["albums"].each() do |token|
				if token.is_a?( String )
					if (md = /([^:]+):(.+) \(([0-9]{4,4})\)/.match( token ))
						response.album, response.year = md[2], md[3]
						break
					end
				elsif token.is_a?( Hash )
					if token["name"].downcase() == "song album" && token["params"][2] && token["params"][3]
						response.album, response.year = token["params"][2], token["params"][3]
						break
					end
				end
			end
		end

	end

	custom_data["reviewed"] = (/\{\{[Aa]utoGenerated\}\}/.match( page_body ) == nil)

	if (md = /<lyrics>(.*)<\/lyrics>/im.match( page_body ))
 		page_body = md[1]
		page_body.gsub!( /[ \t]*[\r\n][ \t]*/m, "\n" )
	else
		page_body.gsub!( /\{\{.*\}\}\n?/, "" )
		page_body.gsub!( /\[\[Category:.*\]\]\n?/, "" )
		page_body.gsub!( /\ *== *(External *Links|Links) *==.*$/im, "" )
		page_body = page_body.split( "\n" ).collect() do |line|
			if line.index( /\s/ ) == 0
				"\n" + line
			else
				line
			end
		end.join( "" )
		page_body.gsub!( /\s*<br ?\/?>\s*/i, "\n" )
	end

	response.lyrics = page_body if ! Strings.empty?( page_body )
	response.custom_data = custom_data

end