Code

m4/ax_append*: update to serial 4
[ncmpc.git] / lyrics / 20-lyricwiki.rb
index 3873e7a8120cec248817391c8cf8d8f0c8189623..56f61aa7943cd4487f9d996090ec6a222747f20f 100755 (executable)
 require 'uri'
 require 'net/http'
 require 'cgi'
-require 'iconv'
+
+# 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])}"
+    "&artist=#{escape(ARGV[0])}&song=#{escape(ARGV[1])}"
 response = Net::HTTP.get(URI.parse(url))
 
 if not response =~ /<url>\s*(.*?)\s*<\/url>/im
@@ -49,6 +54,11 @@ if not $1 =~ /^.*<\/div>(.*?)$/im
        exit(1)
 end
 
-# lyrics come in Latin1, but we need UTF-8
-lyrics_latin1 = CGI::unescapeHTML($1.gsub(/<br \/>/, "\n"))
-puts Iconv.conv('UTF-8//TRANSLIT//IGNORE', 'Latin1', lyrics_latin1)
+lyrics = $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