File indexing completed on 2024-04-28 04:48:05

0001 #!/usr/bin/env ruby
0002 ###########################################################################
0003 #   Now playing script for IRC. Supports both Amarok 1 and Amarok 2.      #
0004 #                                                                         #
0005 #   Use with the "/exec -out" command of your client.                     #
0006 #   You can bind an alias like this (tested with irssi):                  #
0007 #   /alias np exec -out - /home/myself/amaroknowplaying.rb                #
0008 #                                                                         #
0009 #   Copyright                                                             #
0010 #   (C) 2005-2008 Mark Kretschmann <kretschmann@kde.org>                  #
0011 #                                                                         #
0012 #   This program is free software; you can redistribute it and/or modify  #
0013 #   it under the terms of the GNU General Public License as published by  #
0014 #   the Free Software Foundation; either version 2 of the License, or     #
0015 #   (at your option) any later version.                                   #
0016 #                                                                         #
0017 #   This program is distributed in the hope that it will be useful,       #
0018 #   but WITHOUT ANY WARRANTY; without even the implied warranty of        #
0019 #   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         #
0020 #   GNU General Public License for more details.                          #
0021 #                                                                         #
0022 #   You should have received a copy of the GNU General Public License     #
0023 #   along with this program; if not, write to the                         #
0024 #   Free Software Foundation, Inc.,                                       #
0025 #   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.         #
0026 ###########################################################################
0027 
0028 require 'cgi'
0029 
0030 def amarok_1
0031     title  = `dcop amarok player title 2> /dev/null`.chomp
0032     artist = `dcop amarok player artist`.chomp
0033     album  = `dcop amarok player album`.chomp
0034     year   = `dcop amarok player year`.chomp
0035     lastfm = `dcop amarok player lastfmStation`.chomp
0036 
0037     output = ""
0038 
0039     if title.empty?
0040         output += `dcop amarok player nowPlaying`.chomp
0041     else
0042         # Strip file extension
0043         extensions = ".ogg", ".mp3", ".wav", ".flac", ".fla", ".wma", ".mpc"
0044         ext = File.extname( title ).downcase
0045 
0046         if extensions.include?( ext )
0047             title = title[0, title.length - ext.length]
0048         end
0049 
0050         if artist.empty?
0051             output += "#{title}"
0052         else
0053             output += "#{artist} - #{title}"
0054         end
0055 
0056         unless album.empty?
0057             output += " [#{album}"
0058             output += ", #{year}" unless year == "0"
0059             output += "]"
0060         end
0061 
0062         unless lastfm.empty?
0063             output += " (Last.fm #{lastfm})"
0064         end
0065     end
0066 
0067     puts( "np: #{output}" ) unless output.empty?
0068 end
0069 
0070 
0071 def amarok_2
0072     metadata  = `qdbus org.mpris.amarok /Player GetMetadata 2> /dev/null`.chomp
0073 
0074     hash = Hash.new
0075     metadata = metadata.split("\n")
0076     metadata.each do |line|
0077       key = line.partition(":")[0] # head
0078       value = line.partition(":")[2].lstrip() # tail
0079       hash[key] = value 
0080     end
0081 
0082     title  = hash["title"]
0083     artist = hash["artist"] 
0084     album  = hash["album"] 
0085     year   = hash["year"] 
0086     streamName = "" #CGI.unescape(`qdbus org.mpris.amarok /Player streamName`.chomp)
0087     version = "" #`qdbus org.mpris.amarok /Player version`.chomp 
0088 
0089     output = ""
0090 
0091     if title.empty?
0092         output += `qdbus org.mpris.amarok /Player nowPlaying`.chomp
0093     else
0094         # Strip file extension
0095         extensions = ".ogg", ".mp3", ".wav", ".flac", ".fla", ".wma", ".mpc", ".oga"
0096         ext = File.extname( title ).downcase
0097 
0098         if extensions.include?( ext )
0099             title = title[0, title.length - ext.length]
0100         end
0101 
0102         if artist.empty?
0103             output += "#{title}"
0104         else
0105             output += "#{artist} - #{title}"
0106         end
0107 
0108         unless album.empty?
0109             output += " [#{album}"
0110             output += ", #{year}" unless year == "0" or year.empty?
0111             output += "]"
0112         end
0113 
0114         unless streamName.empty?
0115             output += " (#{streamName})"
0116         end
0117         
0118         unless version.empty?
0119             output += " (Amarok #{version})"
0120         end
0121     end
0122 
0123     puts( "np: #{output}" ) unless output.empty?
0124 end
0125 
0126 
0127 system("export DBUS_SESSION_BUS_ADDRESS=")  # Hack for making qdbus work in a screen session
0128 
0129 test = `qdbus org.mpris.amarok /Player GetStatus 2> /dev/null`.chomp
0130 if $?.success?
0131   amarok_2
0132   exit
0133 end
0134 test = `dcop amarok player title 2> /dev/null`.chomp
0135 if $?.success?
0136   amarok_1
0137   exit
0138 end
0139 
0140 exit( 1 ) # Abort if Amarok isn't running