Script: xmms2.rb

Display which song xmms2 is currently playing.
Author: Łukasz Michalik — Version: 0.2 — License: GPL2
For WeeChat ≥ 0.3.0, requires: xmmsclient.
Tags: music
Added: 2011-02-13

Download GitHub Repository

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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
# -*- coding: utf-8 -*-
#
# Copyright (c) 2009, 2011 Łukasz P. Michalik <lmi@ift.uni.wroc.pl>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2, as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
# USA.
#

#
# This plugin requires Xmms2 ruby bindings to be installed.  'format'
# configuration variable specifies how metadata is displayed.
# Default_format is a good example, but don't abuse the syntax as you
# might get garbage.  It works with any key found in medialib for a
# current ID (see `xmms2 info` for a list); non-existing keys are
# ignored.
#

require 'xmmsclient'

Default_format = "[np] ${title} - ${artist}"

$xmms = nil

def weechat_init
    Weechat.register('xmms2', 'Łukasz P. Michalik <lmi@ift.uni.wroc.pl>', '0.2', 'GPL2', 'Xmms2 interaction plugin', '', '')
    Weechat.hook_command('xmms2', 'Trigger a /me currently playing info', '', '', '', 'xmms2', '')
    if (Weechat.config_get_plugin("format").empty?)
        Weechat.config_set_plugin("format", Default_format)
    end
    return Weechat::WEECHAT_RC_OK
end

def xmms2(data, buffer, args)
    if not $xmms
        $xmms = Xmms::Client.new('weechat')

        begin
            $xmms.connect(ENV['XMMS_PATH'])
        rescue Xmms::Client::ClientError
            Weechat.print("", "Failed to connect to XMMS2 daemon.")
            return Weechat::WEECHAT_RC_ERROR
        end
    end

    begin
        id = $xmms.playback_current_id.wait.value

        if (id == 0)
            Weechat.print(buffer, "Nothing is played!")
            return Weechat::WEECHAT_RC_OK
        end
    rescue Xmms::Rescue::ValueError
        Weechat.print(buffer, "Error retrieving current ID!")
        return Weechat::WEECHAT_RC_ERROR
    end

    begin
        info = $xmms.medialib_get_info(id).wait.value.to_propdict
        reg = /\$\{(\w+)\}/
        format = Weechat.config_get_plugin("format")
        out = ""
        while m = reg.match(format)
            out << m.pre_match
            format = m.post_match
            key = m[1].intern
            if info.has_key? key
                out << info[key]
            end
        end
    rescue Xmms::Result::ValueError
        puts 'There was an error retrieving mediainfo for the current ID.'
    end

    Weechat.command(buffer, "/me #{out}")
    return Weechat::WEECHAT_RC_OK
end