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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
|
#
# Copyright (c) 2006 by Eric Gach <eric.gach@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 2 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, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#
import weechat
import os
import subprocess
import traceback
__desc__ = 'Amarok control and now playing script for Weechat.'
__version__ = '1.0.2'
__author__ = 'Eric Gach <eric.gach@gmail.com>'
# With changes by Leonid Evdokimov (weechat at darkk dot net another dot ru):
# http://darkk.net.ru/weechat/amarok.py
# v 1.0.1 - added %year%
# v 1.0.2 - fixed bug with dead zombie-childs
# fixed bug when loading second instance of the script
# added better default for dcop_user setting
dcop = {}
debug = {}
infobar = {}
output = {}
ssh = {}
class amarokException(Exception):
pass
def amarokCommand(server, args):
try:
args = args.split(' ')
if args[0] == 'infobar':
if infobar['enabled']:
infobar['enabled'] = False
weechat.set_plugin_config('infobar_enabled', '0')
weechat.remove_timer_handler('amarokInfobarUpdate')
weechat.remove_infobar(0)
weechat.prnt('Infobar disabled')
else:
infobar['enabled'] = True
weechat.set_plugin_config('infobar_enabled', '1')
amarokInfobarUpdate()
weechat.add_timer_handler(infobar['update'], 'amarokInfobarUpdate')
weechat.prnt('Infobar enabled')
return weechat.PLUGIN_RC_OK
elif args[0] == 'next':
__executeCommand('next')
weechat.prnt('Amarok: Playing next song.')
return weechat.PLUGIN_RC_OK
elif args[0] == 'np':
return amarokNowPlaying(server)
elif args[0] == 'pause':
__executeCommand('pause')
weechat.prnt('Amarok: Song paused.')
return weechat.PLUGIN_RC_OK
elif args[0] == 'play':
__executeCommand('play')
weechat.prnt('Amarok: Started playing.')
return weechat.PLUGIN_RC_OK
elif args[0] == 'prev':
__executeCommand('prev')
weechat.prnt('Amarok: Playing previous song.')
return weechat.PLUGIN_RC_OK
elif args[0] == 'stop':
__executeCommand('stop')
weechat.prnt('Amarok: Stop playing.')
return weechat.PLUGIN_RC_OK
elif args[0] == '':
return amarokDisplayHelp(server)
else:
weechat.prnt('Amarok: Unknown command %s' % (args[0]), '', server)
return weechat.PLUGIN_RC_OK
except amarokException, ex:
return weechat.PLUGIN_RC_KO
except:
file = open(debug['file'], 'w')
traceback.print_exc(None, file)
weechat.prnt('Unknown Exception encountered. Stack dumped to %s' % (debug['file']), '', server)
return weechat.PLUGIN_RC_KO
def amarokDisplayHelp(server):
weechat.prnt('%s - Version: %s' % (__desc__, __version__), '', server)
weechat.prnt('Author: %s' % (__author__), '', server)
weechat.prnt('', '', server)
weechat.prnt('Commands Available', '', server)
weechat.prnt(' /amarok next - Move to the next song in the playlist.', '', server)
weechat.prnt(' /amarok np - Display currently playing song.', '', server)
weechat.prnt(' /amarok play - Start playing music.', '', server)
weechat.prnt(' /amarok pause - Toggle between pause/playing.', '', server)
weechat.prnt(' /amarok prev - Move to the previous song in the playlist.', '', server)
weechat.prnt(' /amarok stop - Stop playing music.', '', server)
weechat.prnt(' /amarok infobar - Toggle the infobar display.', '', server)
weechat.prnt('', '', server)
weechat.prnt('Formatting', '', server)
weechat.prnt(' %artist% - Replaced with the song artist.', '', server)
weechat.prnt(' %title% - Replaced with the song title.', '', server)
weechat.prnt(' %album% - Replaced with the song album.', '', server)
weechat.prnt(' %year% - Replaced with the song year tag.', '', server)
weechat.prnt(' %cTime% - Replaced with how long the song has been playing.', '', server)
weechat.prnt(' %tTime% - Replaced with the length of the song.', '', server)
weechat.prnt(' %bitrate% - Replaced with the bitrate of the song.', '', server)
return weechat.PLUGIN_RC_OK
def amarokInfobarUpdate():
__loadSettings()
if infobar['enabled'] == False:
return weechat.PLUGIN_RC_OK
isPlaying = __executeCommands((__dcopCommand('isPlaying'),))
if isPlaying.strip() == 'false':
weechat.print_infobar(infobar['update'], 'Amarok is not currently playing')
return weechat.PLUGIN_RC_OK
else:
song = __getSongInfo()
format = __formatNP(infobar['format'], song)
weechat.print_infobar(infobar['update'], format)
return weechat.PLUGIN_RC_OK
def amarokNowPlaying(server):
__loadSettings()
isPlaying = __executeCommands((__dcopCommand('isPlaying'),))
if isPlaying.strip() == 'false':
weechat.prnt('Amarok is not playing.', '', server)
return weechat.PLUGIN_RC_KO
else:
song = __getSongInfo()
format = __formatNP(output['format'], song)
weechat.command(format)
return weechat.PLUGIN_RC_OK
def amarokUnload():
"""Unload the plugin from weechat"""
if infobar['enabled']:
weechat.remove_infobar(0)
weechat.remove_timer_handler('amarokInfobarUpdate')
return weechat.PLUGIN_RC_OK
def __formatNP(template, song):
np = template.replace('%artist%', song['artist'])
np = np.replace('%title%', song['title'])
np = np.replace('%album%', song['album'])
np = np.replace('%cTime%', song['cTime'])
np = np.replace('%tTime%', song['tTime'])
np = np.replace('%bitrate%', song['bitrate'])
np = np.replace('%year%', song['year'])
return np
def __dcopCommand(cmd):
if dcop['user'] == ':':
return 'dcop amarok player %s' % (cmd)
else:
return 'dcop --user %s amarok player %s' % (dcop['user'], cmd)
def __executeCommands(cmds):
from subprocess import PIPE
cmds = " && ".join(cmds)
if ssh['enabled']:
cmds = 'ssh -p %d %s@%s "%s"' % (ssh['port'], ssh['user'], ssh['host'], cmds)
proc = subprocess.Popen(cmds, shell = True, stderr = PIPE, stdout = PIPE, close_fds = True)
error = proc.stderr.read()
if error != '':
weechat.prnt(error)
output = proc.stdout.read()
proc.wait()
return output
def __getSongInfo():
"""Get the song information from amarok"""
song = {}
songs = __executeCommands(
(
__dcopCommand('artist'),
__dcopCommand('title'),
__dcopCommand('album'),
__dcopCommand('currentTime'),
__dcopCommand('totalTime'),
__dcopCommand('bitrate'),
__dcopCommand('year')
)
)
song['artist'], song['title'], song['album'], song['cTime'], song['tTime'], song['bitrate'], song['year'], empty = songs.split("\n")
return song
def __loadSettings():
dcop['user'] = __loadSetting('dcop_user', ':')
debug['file'] = os.path.expanduser(__loadSetting('debug_file', '~/amarok_debug.txt'))
infobar['enabled'] = __loadSetting('infobar_enabled', '0', 'bool')
infobar['format'] = __loadSetting('infobar_format', 'Now Playing: %title% by %artist%')
infobar['update'] = __loadSetting('infobar_update', '10', 'int')
output['format'] = __loadSetting('output_format', '/me is listening to %C04%title%%C by %C03%artist%%C from %C12%album%%C [%cTime% of %tTime% @ %bitrate%kbps]')
ssh['enabled'] = __loadSetting('ssh_enabled', '0', 'bool')
ssh['host'] = __loadSetting('ssh_host', 'localhost')
ssh['port'] = __loadSetting('ssh_port', '22', 'int')
ssh['user'] = __loadSetting('ssh_user', 'user')
def __loadSetting(setting, default=None, type=None):
value = weechat.get_plugin_config(setting)
if value == '' and default != None:
weechat.set_plugin_config(setting, default)
value = default
if type == 'int' or type == 'bool':
value = int(value)
if type == 'bool':
value = bool(value)
return value
if weechat.register('amarok', __version__, 'amarokUnload', __desc__):
__loadSettings()
if infobar['enabled']:
amarokInfobarUpdate()
weechat.add_timer_handler(infobar['update'], 'amarokInfobarUpdate')
weechat.add_command_handler('amarok', 'amarokCommand', 'Manage amarok or display now playing information.', 'next|np|play|pause|prev|stop|infobar')
|