diff --git a/lyrics/20-lyricwiki.rb b/lyrics/20-lyricwiki.rb
index db7b970307dd03d944eb76e971cedaf8698c72ce..fde22e44a835144efddc0fd7dfa55fe039a2497c 100755 (executable)
--- a/lyrics/20-lyricwiki.rb
+++ b/lyrics/20-lyricwiki.rb
#!/usr/bin/env ruby
#
-# (c) 2004-2008 The Music Player Daemon Project
+# (c) 2004-2017 The Music Player Daemon Project
# http://www.musicpd.org/
#
# This program is free software; you can redistribute it and/or modify
#
require 'uri'
-require 'net/http'
+require 'open-uri'
require 'cgi'
+# We need this because URI.escape doesn't escape ampersands.
+def escape(string)
+ new = URI.escape(string)
+ new.gsub(/&/, "%26")
+end
+
url = "http://lyrics.wikia.com/api.php?action=lyrics&fmt=xml&func=getSong" + \
- "&artist=#{URI.escape(ARGV[0])}&song=#{URI.escape(ARGV[1])}"
-response = Net::HTTP.get(URI.parse(url))
+ "&artist=#{escape(ARGV[0])}&song=#{escape(ARGV[1])}"
+response = open(URI.parse(url)).read
if not response =~ /<url>\s*(.*?)\s*<\/url>/im
$stderr.puts "No URL in response!"
url = $1
exit(69) if url =~ /action=edit$/
-response = Net::HTTP.get(URI.parse(url))
-if not response =~ /<div class='lyricbox'>\s*(.*?)\s*<!--/im
+response = open(URI.parse(url)).read
+if not response =~ /<div class='lyricbox'>\s*(.*?)\s*<div class='lyricsbreak'>/im
$stderr.puts "No <div class='lyricbox'> in lyrics page!\n"
exit(1)
end
-if not $1 =~ /^.*<\/div>(.*?)$/im
- $stderr.puts "Couldn't remove leading XML tags in lyricbox!\n"
- exit(1)
-end
+lyrics = $1.gsub(/<br \/>/, "\n")
-puts CGI::unescapeHTML($1.gsub(/<br \/>/, "\n"))
+if lyrics.respond_to?(:force_encoding)
+ puts CGI::unescapeHTML(lyrics.force_encoding(Encoding::UTF_8))
+else
+ $KCODE = 'U'
+ puts CGI::unescapeHTML(lyrics)
+end