#!/usr/bin/env ruby # # Amarok Script for fetching song lyrics from http://lyrc.com.ar. # Ported from Amarok's contextbrowser.cpp. # # (c) 2006 Mark Kretschmann # (c) 2004 Christian Muehlhaeuser # (c) 2005 Reigo Reinmets # (c) 2005 Alexandre Pereira de Oliveira # # License: GNU General Public License V2 require "net/http" require "rexml/document" # require File.dirname( File.expand_path( __FILE__ ) ) + "/../ruby_debug/debug.rb" require "uri" @app_name = "Lyrics_Lyrc" class String def shellquote return "'" + self.gsub("'", "'\\\\''") + "'" end end def showLyrics( lyrics ) system("dcop", "amarok", "contextbrowser", "showLyrics", lyrics) end def parseLyrics( lyrics ) if lyrics.include?( "


" )] end lyrics.gsub!( /<[fF][oO][nN][tT][^>]*>/, "" ) # doc = REXML::Document.new() doc = REXML::Document.new( "" ) root = doc.add_element( "lyrics" ) root.add_attribute( "page_url", @page_url ) title = /()([^<]*)/.match( lyrics )[2].to_s() root.add_attribute( "title", title.unpack("C*").pack("U*") ) if title artist = /()([^<]*)/.match( lyrics )[2] root.add_attribute( "artist", artist.to_s().unpack("C*").pack("U*") ) if artist lyrics = /(<\/u><\/font>)(.*)/.match( lyrics )[2].to_s() lyrics.gsub!( /<[Bb][Rr][^>]*>/, "\n" ) # HTML -> Plaintext root.text = lyrics.unpack("C*").pack("U*") if lyrics #Convert to UTF-8 xml = "" doc.write( xml ) # debug xml showLyrics( xml ) end def notFound() doc = REXML::Document.new( "" ) root = doc.add_element( "suggestions" ) root.add_attribute( "page_url", @page_url ) xml = "" doc.write( xml ) showLyrics( xml ) end def parseSuggestions( lyrics ) lyrics = lyrics[lyrics.index( "Suggestions : " )..lyrics.index( "

" )] lyrics.gsub!( "", "" ) lyrics.gsub!( "", "" ) lyrics.gsub!( "

", "" ) doc = REXML::Document.new( "" ) root = doc.add_element( "suggestions" ) root.add_attribute( "page_url", @page_url ) entries = lyrics.split( "
" ) entries.delete_at( 0 ) entries.each() do |entry| url = /()([^<]*)/.match( entry )[2].to_s() artist = artist_title.split( " - " )[0] title = artist_title.split( " - " )[1] suggestion = root.add_element( "suggestion" ) suggestion.add_attribute( "url", url ) suggestion.add_attribute( "artist", artist.unpack("C*").pack("U*") ) if artist suggestion.add_attribute( "title", title.unpack("C*").pack("U*") ) if title end xml = "" doc.write( xml ) # debug xml showLyrics( xml ) end def fetchLyrics( artist, title, url ) host = "lyrc.com.ar" path = url.empty? ? "/en/tema1en.php?artist=#{artist}&songname=#{title}" : "/en/#{url}" @page_url = "http://" + host + path proxy_host = nil proxy_port = nil proxy_user = nil proxy_pass = nil if ( @proxy == nil ) @proxy = `dcop amarok script proxyForUrl #{@page_url.shellquote}` end proxy_uri = URI.parse( @proxy ) if ( proxy_uri.class != URI::Generic ) proxy_host = proxy_uri.host proxy_port = proxy_uri.port proxy_user, proxy_pass = proxy_uri.userinfo.split(':') unless proxy_uri.userinfo.nil? end h = Net::HTTP.new( host, 80, proxy_host, proxy_port, proxy_user, proxy_pass ) response = h.get( path ) unless response.code == "200" # error "HTTP Error: #{response.message}" `dcop amarok contextbrowser showLyrics ""` return end lyrics = response.body() # puts( lyrics ) lyrics.gsub!( "\n", "" ) # No need for LF, just complicates our RegExps lyrics.gsub!( "\r", "" ) # No need for CR, just complicates our RegExps # lyrics.gsub!( '', "'" ) # Lyrc has weird encodings # Remove images, links, scripts, styles, fonts and tables lyrics.gsub!( /<[iI][mM][gG][^>]*>/, "" ) lyrics.gsub!( /<[aA][^>]*>[^<]*<\/[aA]>/, "" ) lyrics.gsub!( /<[sS][cC][rR][iI][pP][tT][^>]*>[^<]*(