Class: LyricWiki

Inherits:
MediaWikiLyrics show all
Defined in:
lib/wiki_lyrics/lyrics_LyricWiki.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_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



249
250
251
252
253
254
255
256
257
258
259
260
# File 'lib/wiki_lyrics/lyrics_LyricWiki.rb', line 249

def LyricWiki.build_album_art_description( artist, album, year, cleanup=true )
	if cleanup
		artist = cleanup_title_token( artist )
		album = cleanup_title_token( album )
	end
	return \
	"{{Albumcover/Upload|\n" \
	"|artist = #{artist}\n" \
	"|album  = #{album}\n" \
	"|year   = #{year}\n" \
	"}}\n"
end

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



240
241
242
243
244
245
246
247
# File 'lib/wiki_lyrics/lyrics_LyricWiki.rb', line 240

def LyricWiki.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 = "#{artist} - #{album}#{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)


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/wiki_lyrics/lyrics_LyricWiki.rb', line 178

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

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

	f_letter = get_first_letter( album )

	contents = \
	"{{Album\n" \
	"|Artist   = #{artist}\n" \
	"|Album    = #{album}\n" \
	"|fLetter  = #{f_letter}\n" \
	"|Released = #{year}\n" \
	"|Length   = #{year}\n" \
	"|Cover    = #{album_art}\n" \
	"|star     = #{reviewed ? "Black" : "Green"}\n" \
	"}}\n"

	return \
	"#{contents}\n" \
	"#{tracks.strip()}\n" \
	"\n" \
	"{{AlbumFooter\n" \
	"|artist=#{artist}\n" \
	"|album=#{album}\n" \
	"}}\n"
end

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

Raises:

  • (ArgumentError)


205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
# File 'lib/wiki_lyrics/lyrics_LyricWiki.rb', line 205

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

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

	f_letter = get_first_letter( title )
	year = year.to_i() > 1900 ? year.to_s() : "????"

	lyrics = lyrics.gsub(
		/<ruby><rb>([^<]*)<\/rb><rp>\(<\/rp><rt>([^<]*)<\/rt><rp>\)<\/rp><\/ruby>/,
		"{{ruby|\1|\2}}"
	) if lyrics

	song_album = Strings.empty?( album ) ? "" : "#{album} (#{year})"
	song_star = reviewed ? "Black" : "Green"

	song_credits = ""
	song_credits << "==Credits==\n" if ! Strings.empty?( credits ) || ! Strings.empty?( lyricist )
	song_credits << "*Composer: #{credits}\n" if ! Strings.empty?( credits )
	song_credits << "*Lyrics by: #{lyricist}\n" if ! Strings.empty?( lyricist )

	return \
	"{{Song|#{song_album}|#{artist}|star=#{song_star}}\n\n" \
	"<lyric>\n#{Strings.empty?( lyrics ) ? "{{instrumental}}" : lyrics}\n</lyric>\n\n" \
	"#{song_credits}\n" \
	"{{SongFooter\n" \
	"|fLetter=#{f_letter}\n" \
	"|artist=#{artist}\n" \
	"|song=#{title}\n" \
	"|album=#{album}\n" \
	"|language=\n" \
	"|iTunes=\n" \
	"}}\n"

end

.build_tracks(album_data) ⇒ Object



164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/wiki_lyrics/lyrics_LyricWiki.rb', line 164

def LyricWiki.build_tracks( album_data )
	ret = ""
	tracks.each() do |track|
		track_artist = cleanup_title_token( track.artist )
		track_title  = cleanup_title_token( track.title )
		tc_track_title = Strings.titlecase( track_title )
		# artist_param = (album_artist == "(Various Artists)" || album_artist == "Various Artists") ? "by" : "artist"
		# ret += "# {{track|title=#{track_title}|#{artist_param}=#{track_artist}}}\n"
		# ret += "# {{track|title=#{track_title}|#{artist_param}=#{track_artist}|display=#{tc_track_title}}}\n"
		ret += "# '''[[#{track_artist}:#{track_title}|#{tc_track_title}]]'''\n"
	end
	return ret
end

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



317
318
319
320
321
322
323
324
325
326
327
# File 'lib/wiki_lyrics/lyrics_LyricWiki.rb', line 317

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

.control_pageObject



39
40
41
# File 'lib/wiki_lyrics/lyrics_LyricWiki.rb', line 39

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

.find_album_art_name(artist, album, year) ⇒ Object



262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
# File 'lib/wiki_lyrics/lyrics_LyricWiki.rb', line 262

def LyricWiki.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!( " ", "" )

	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["title"].index( "Image:" ) != 0

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

		matches = 0
		idx1 = normalized_title.index( normalized_artist )
		matches += 1 if idx1
		idx1 = idx1 ? idx1 + normalized_artist.size : 0
		idx2 = normalized_title.index( normalized_album, idx1 )
		matches += 2 if idx2

		candidates.insert( -1, [ matches, result["title"] ] ) if matches > 1
	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

.get_first_letter(title) ⇒ Object



307
308
309
310
311
312
313
314
315
# File 'lib/wiki_lyrics/lyrics_LyricWiki.rb', line 307

def LyricWiki.get_first_letter( title )
	if title.index( /^[a-zA-Z]/ ) == 0
		return title.slice( 0, 1 )
	elsif title.index( /^[0-9]/ ) == 0
		return "0-9"
	else
		return "Symbol"
	end
end

.parse_search_results(page_body, content_matches = true) ⇒ Object



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

def LyricWiki.parse_search_results( page_body, content_matches=true )

	results = []
	return results if page_body == nil || ! content_matches

	page_body.tr_s!( " \n\r\t", " " )
	page_body.gsub!( /\ ?<\/?span[^>]*> ?/, "" )

	return results if ! page_body.sub!( /^.*<h1 class="firstHeading">Search results<\/h1>/, "" )

	return results if ! page_body.gsub!( /<form id="powersearch" method="get" action="[^"]+">.*$/, "" )
	page_body.gsub!( /<\/[uo]l> ?<p( [^>]*|)>View \(previous .*$/, "" )

	page_body.split( "<li>" ).each() do |entry|
		if (md = /<a href="([^"]*index\.php\/|[^"]*index\.php\?title=|\/)([^"]*)" title="([^"]+)"/.match( entry ))
			result = {
				@@SEARCH_RESULT_URL => "http://#{site_host()}/index.php?title=#{md[2]}",
				@@SEARCH_RESULT_TITLE => md[3]
			}
			results << result if ! content_matches || ! results.include?( result )
		end
	end

	return results
end

.site_hostObject



31
32
33
# File 'lib/wiki_lyrics/lyrics_LyricWiki.rb', line 31

def LyricWiki.site_host()
	return "lyricwiki.org"
end

.site_nameObject



35
36
37
# File 'lib/wiki_lyrics/lyrics_LyricWiki.rb', line 35

def LyricWiki.site_name()
	return "LyricWiki"
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/wiki_lyrics/lyrics_LyricWiki.rb', line 43

def parse_lyrics( response, page_body )

	page_body.gsub!( /’|�/, "'" ) # replace bizarre characters with apostrophes

	custom_data = {}
	custom_data["reviewed"] = (/\[\[Category:[Rr]eview[_ ]Me\]\]/.match( page_body ) == nil)

	# search album, year and artist information
	if (md = /\{\{\s*[Ss]ong\s*\|.*$/.match( page_body ))
		unnamed_params_names = { 1 => "album_and_year", 2 => "artist" }
		template_data = parse_template( md[0] )
		template_data["params"].each() do |key, value|
			if (key = unnamed_params_names[key]) && value.is_a?( String )
				custom_data[key] = value
			end
		end
		if custom_data["reviewed"] && template_data["params"]["star"] == "Green"
			custom_data["reviewed"] = false
		end
	elsif (md = /On (''')?\[\[[^\|]+\|([^\|]+)\]\](''')? by (''')?\[\[([^\]]+)\]\](''')?/.match( page_body ))
		custom_data["album_and_year"] = md[2]
		custom_data["artist"] = md[5]
	end

	if custom_data.include?( "album_and_year" )
		if (md = /^(.+) \(([\?0-9]{4,4})\)$/.match( custom_data["album_and_year"] ))
			response.album = md[1]
			response.year = md[2] if md[2].to_i() > 1900
		end
	end

	# search title information (and other information that hasn't been found yet)
	if (md = /\{\{\s*[Ss]ongFooter\s*\|.*$/.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 )
		end
	elsif (md = /\[[^\s\]]+ ([^\]]+)\] on Amazon$/.match( page_body ))
		custom_data["song"] = md[1].strip()
	end

	if (md = /\*?\s*Composer: *([^\n]+)/.match( page_body ))
		custom_data["credits"] = md[1].strip()
	end

	if (md = /\*?\s*Lyrics by: *([^\n]+)/.match( page_body ))
		custom_data["lyricist"] = md[1].strip()
	end

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

	if (md = /<lyrics?>(.*)<\/lyrics?>/im.match( page_body ))
		page_body = md[1]
		if /\s*\{\{[Ii]nstrumental\}\}\s*/.match( page_body )
			page_body = "<tt>(Instrumental)</tt>"
		else
			page_body.gsub!( /[ \t]*[\r\n][ \t]*/m, "\n" )
		end
	else
		if /\s*\{\{[Ii]nstrumental\}\}\s*/.match( page_body )
			page_body = "<tt>(Instrumental)</tt>"
		else
			page_body.gsub!( /\{\{.*\}\}\n?/, "" )
			page_body.gsub!( /\[\[Category:.*\]\]\n?/, "" )
			page_body.gsub!( /On '''\[\[.*?\n/i, "" )
			page_body.gsub!( /By '''\[\[.*?\n/i, "" )
			page_body.gsub!( /\ *== *Credits *==.*$/im, "" )
			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
	end

	response.custom_data = custom_data

	if ! Strings.empty?( page_body )
		# expand ruby tags:
		page_body.gsub!( /\{\{ruby\|([^\|]*)\|([^\}]*)\}\}/, "<ruby><rb>\1</rb><rp>(</rp><rt>\2</rt><rp>)</rp></ruby>" )
		# take care of multiple lyrics tags:
		page_body.gsub!( /(\{\|\s*\|-\s*\||\|\|)\s*==\s*([^=]+)\s*==/, "<br/><b>\2</b>" )
		page_body.gsub!( /\s*==\s*([^=]+)\s*==/, "\n<br/><b>\\1</b>" )
		page_body.gsub!( /<\/?lyric>/i, "" )
		response.lyrics = page_body
	end

end