Script: clemy.py

Control Clementine from WeeChat.
Author: Darth-O-Ring — Version: 0.1.2 — License: GPL-3.0-or-later
For WeeChat ≥ 0.3.0.
Tags: music, py2, py3
Added: 2014-04-12 — Updated: 2024-08-18

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
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# Author and licensing
__Author__ = "Darth-O-Ring"
__Email__ = "darthoring@gmail.com"
__License__ = """
Copyright (C) 2014-2016  Darth-O-Ring   <darthoring@gmail.com>

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

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, see <http://www.gnu.org/licenses/>.

"""

# Imports
try:
    import weechat
    IMPORT_OK = True
except ImportError:
    print('Error: Script must be run under Weechat.')
    IMPORT_OK = False

import dbus


err_message = '\nSomething silly just happend.  Make sure Clementine is running mah dude.'


def help():
    """
    Print help menu to main
    weechat buffer.

    """
    weechat.prnt('', '\n            --Start Help--                ')
    weechat.prnt('', '--Option--                     --Command--')
    weechat.prnt('', '-Play                          /clemy play')
    weechat.prnt('', '-Pause                         /clemy pause')
    weechat.prnt('', '-Next                          /clemy next')
    weechat.prnt('', '-Previous                      /clemy prev')
    weechat.prnt('', '-Stop                          /clemy stop')
    weechat.prnt('', '-Play Track <n>                /clemy playtrack <n>')
    weechat.prnt('', '-Volume up by 4%               /clemy vol+')
    weechat.prnt('', '-Volume down by 4%             /clemy vol-')
    weechat.prnt('', '-Volume up by <n>              /clemy vol+by <n>')
    weechat.prnt('', '-Volume down by <n>            /clemy vol-by <n>')
    weechat.prnt('', '-Now Playing                   /clemynp')
    weechat.prnt('', '-Info                          /clemy info')
    weechat.prnt('', '\n            --End Help--                  ')
    return ''



def np():
    """
    Gather artist, song, and album
    info through dbus.

    """
    bus         =       dbus.Bus(dbus.Bus.TYPE_SESSION)
    try:
        bus_object  =       bus.get_object('org.mpris.clementine', '/Player')
        artist_info =       bus_object.GetMetadata()
        artist      =       artist_info['performer'][:]
        album       =       artist_info['album'][:]
        song        =       artist_info['title'][:]
        now_playing =       '{0} - {1} (album: {2})'.format(artist, song, album)

    except (dbus.DBusException, Exception):
        weechat.prnt('', err_message)

        return weechat.WEECHAT_RC_OK

    return now_playing


def process_cb(data, command, rc, out, err):
    process_output      =       ''
    if out != '':
        process_output  +=      out

    if int(rc) > 0:
        weechat.prnt('', process_output)

    return weechat.WEECHAT_RC_OK


def control(data, buffer, args):
    """
    Parse buffer for valid commands
    and build dictionary mapping from
    valid args to system commands.

    """
    args            =       args.split(' ')
    commands        =       {
                            'play'      :   'dbus-send --type=method_call --dest=org.mpris.MediaPlayer2.clementine /org/mpris/MediaPlayer2 org.mpris.MediaPlayer2.Player.PlayPause',
                            'next'      :   'dbus-send --type=method_call --dest=org.mpris.MediaPlayer2.clementine /org/mpris/MediaPlayer2 org.mpris.MediaPlayer2.Player.Next',
                            'prev'      :   'dbus-send --type=method_call --dest=org.mpris.MediaPlayer2.clementine /org/mpris/MediaPlayer2 org.mpris.MediaPlayer2.Player.Previous',
                            'stop'      :   'dbus-send --type=method_call --dest=org.mpris.MediaPlayer2.clementine /org/mpris/MediaPlayer2 org.mpris.MediaPlayer2.Player.Stop',
                            'vol+'      :   'clementine --volume-up',
                            'vol-'      :   'clementine --volume-down',
                            'vol+by'    :   'clementine --volume-increase-by {0}',
                            'vol-by'    :   'clementine --volume-decrease-by {0}',
                            'playtrack' :   'clementine --play-track {0}'
                            }

    try:
        if args[0].lower() == 'play':
            weechat.hook_process(commands['play'], 10 * 1000, 'process_cb', '')
            weechat.prnt('', 'Playing: {0}'.format(np()))

        elif args[0].lower() == 'pause':
            weechat.hook_process(commands['play'], 10 * 1000, 'process_cb', '')
            weechat.prnt('', 'Paused')

        elif args[0].lower() == 'next':
            weechat.hook_process(commands['next'], 10 * 1000, 'process_cb', '')
            weechat.prnt('', 'Playing Next Track Mah Dude...')

        elif args[0].lower() == 'prev':
            weechat.hook_process(commands['prev'], 10 * 1000, 'process_cb', '')
            weechat.prnt('', 'Playing Previous Track Mah Dude...')

        elif args[0].lower() == 'stop':
            weechat.hook_process(commands['stop'], 10 * 1000, 'process_cb', '')
            weechat.prnt('', 'Stopped Playback')

        elif args[0].lower() == 'playtrack':
            weechat.hook_process(commands['playtrack'].format(args[1]), 10 * 1000, 'process_cb', '')
            weechat.prnt('', 'Playing track {0}: {1}'.format(args[1], np()))

        elif args[0].lower() == 'vol+':
            weechat.hook_process(commands['vol+'], 10 * 1000, 'process_cb', '')
            weechat.prnt('', 'Volume Increased By 4%')

        elif args[0].lower() == 'vol-':
            weechat.hook_process(commands['vol-'], 10 * 1000, 'process_cb', '')
            weechat.prnt('', 'Volume Decreased By 4%')

        elif args[0].lower() == 'vol+by':
            weechat.hook_process(commands['vol+by'].format(args[1]), 10 * 1000, 'process_cb', '')
            weechat.prnt('', 'Volume Increased By {0}%'.format(args[1]))

        elif args[0].lower() == 'vol-by':
            weechat.hook_process(commands['vol-by'].format(args[1]), 10 * 1000, 'process_cb', '')
            weechat.prnt('', 'Volume Decreased By {0}%'.format(args[1]))

        elif args[0].lower() == 'help':
            weechat.prnt('', '{0}'.format(help()))

        elif args[0].lower() == 'info':
            weechat.prnt('', 'Currently listenting to: {0}'.format(np()))

        else:
            weechat.prnt('', "\nWhat are you doing dawg?  That's not a valid command!\n")
            help()

    except:
        weechat.prnt('', err_message)

        return ''

    return weechat.WEECHAT_RC_OK


def weechat_np(data, buffer, args):
    """
    Callback function for
    hooked clemynp command.

    """
    weechat.command(buffer, '/me is currently listening to: {0}'.format(np()))

    return weechat.WEECHAT_RC_OK


if __name__ == "__main__" and IMPORT_OK:
    weechat.register('clemy', "Your mommy's boyfriend", '0.1.2', 'GPLv3', 'Control yo Clementine like boom-blaka!', '', '')
    weechat.hook_command('clemynp', 'Get/output now playing info', '', '', '', 'weechat_np', '')
    weechat.hook_command('clemy', 'Control Clementine', "[play] | [pause] | [next] | [prev] | [stop] | [vol+] | [vol-] | [vol+by <n>] | [vol-by <n>] | [playtrack <n>] | [help]",
                         """
play:           Play song.
pause:          Pause song.
next:           Play next song.
prev:           Play previous song.
stop:           Stop playback
vol+:           Increase volume by 4%
vol-:           Decrease volume by 4%
vol+by <n>:     Increase volume by n%
vol-by <n>:     Decrease volume by n%
playtrack <n>:  Play track number n
help:  OG help information.

use: /clemynp if you want to print current song to the buffer that the command is launched from.
Get silly with it.

""", '', 'control', '')