Control and now-playing script for moc.
Author: SuperTux88
— Version: 1.9
— License: GPL-2.0-or-later
For WeeChat ≥ 0.3.0, requires: moc.
Tags: music, py2, py3
Added: 2009-09-09
— Updated: 2020-06-21
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 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 | # # Copyright (c) 2009 by Benjamin Neff <info@benjaminneff.ch> # # 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 # moc control and now playing script for Weechat. # (this script requires WeeChat 0.3.0 (or newer) and moc) # # History: # # 2020-06-21, Sebastien Helleu <flashcode@flashtux.org>: # version 1.9: make call to bar_new compatible with WeeChat >= 2.9 # 2019-10-16, Benjamin Neff <info@benjaminneff.ch>: # version 1.8: - add python 3 support # 2009-10-26, Benjamin Neff <info@benjaminneff.ch>: # version 1.7.3: - Bugfix ( "/me" --> "output_nothing" ) 2 # 2009-10-25, Benjamin Neff <info@benjaminneff.ch>: # version 1.7.2: - Fix if moc isn't running # 2009-10-24, Benjamin Neff <info@benjaminneff.ch>: # version 1.7.1: - Bugfix ( "/me" --> "output_nothing" ) # 2009-10-16, Benjamin Neff <info@benjaminneff.ch>: # version 1.7: - AvgBitrate bug # - format Time # 2009-09-15, Benjamin Neff <info@benjaminneff.ch>: # version 1.6: - bugfixing ;-) # - hook_config # - remove i|o|ot (color-bug) # 2009-09-08, Benjamin Neff <info@benjaminneff.ch>: # version 1.5: initial release / port to weechat 0.3.0 # from __future__ import print_function SCRIPT_NAME = "moc_control" SCRIPT_AUTHOR = "SuperTux88 (Benjamin Neff) <info@benjaminneff.ch>" SCRIPT_VERSION = "1.9" SCRIPT_LICENSE = "GPL2" SCRIPT_DESC = "moc control and now playing script for Weechat" SCRIPT_COMMAND = "moc" import_ok = True try: import weechat except ImportError: print("This script must be run under WeeChat.") print("Get WeeChat now at: https://weechat.org/") import_ok = False try: import os import subprocess import traceback except ImportError as message: print('Missing package(s) for {}: {}'.format(SCRIPT_NAME, message)) import_ok = False # =================================[ config ]================================= infobar = {} output = {} STATUS_NOT_RUNNING = 'NOT_RUNNING' STATUS_PLAYING = 'PLAY' STATUS_PAUSED = 'PAUSE' STATUS_STOPPED = 'STOP' def load_settings(data, option, value): """load all settings""" infobar['enabled'] = _load_setting('infobar_enabled', 'off', 'bool') infobar['format'] = _load_setting('infobar_format', 'Now Playing: %mocTitle%') infobar['update'] = _load_setting('infobar_update', '10', 'int') output['format'] = _load_setting('output_format', '/me is listening to %C04%title%%C %artist%%album%::: %C07%file%%C ::: %cTime%/%tTime% @ %bitrate%') output['artist'] = _load_setting('output_format_artist', '- %C03%artist%%C ') output['album'] = _load_setting('output_format_album', '(%C12%album%%C) ') output['nothing'] = _load_setting('output_nothing', '/me is listening to nothing') #update config for 1.6 (i|o|ot -> +/me) if weechat.config_is_set_plugin('output_type'): if weechat.config_get_plugin('output_type') == 'ot': weechat.config_set_plugin('output_format', "/me " + output['format']) weechat.config_set_plugin('output_nothing', "/me " + output['nothing']) output['format'] = "/me " + output['format'] output['nothing'] = "/me " + output['nothing'] weechat.config_unset_plugin('output_type') return weechat.WEECHAT_RC_OK def _load_setting(setting, default=None, type=None): """load setting or create it""" value = weechat.config_get_plugin(setting) if value == '' and default != None: weechat.config_set_plugin(setting, default) value = default if type == 'int': value = int(value) if type == 'bool': if value == "on": value = True elif value == "off": value = False else: weechat.config_set_plugin(setting, default) if default == "on": value = True elif default == "off": value = False return value # ================================[ command ]================================= def moc_command(data, buffer, args): """run command""" args = args.split(' ') if args[0] == '': return moc_now_playing(buffer) elif args[0] == 'pause' or args[0] == 'pp': if _get_status() == STATUS_STOPPED: weechat.prnt(buffer, 'moc: Not playing') return weechat.WEECHAT_RC_OK else: _execute_command('mocp -G') weechat.prnt(buffer, 'moc: Song paused / Continue playing') return weechat.WEECHAT_RC_OK elif args[0] == 'play': if _get_status() == STATUS_PLAYING: weechat.prnt(buffer, 'moc: Already playing') return weechat.WEECHAT_RC_OK elif _get_status() == STATUS_PAUSED: _execute_command('mocp -U') weechat.prnt(buffer, 'moc: Continue playing.') return weechat.WEECHAT_RC_OK else: _execute_command('mocp -p') weechat.prnt(buffer, 'moc: Started playing.') return weechat.WEECHAT_RC_OK elif args[0] == 'stop': _execute_command('mocp -s') weechat.prnt(buffer, 'moc: Stop playing.') return weechat.WEECHAT_RC_OK elif args[0] == 'prev': if _get_status() == STATUS_STOPPED: weechat.prnt(buffer, 'moc: Not playing, cannot go to previous song.') return weechat.WEECHAT_RC_OK else: _execute_command('mocp -r') weechat.prnt(buffer, 'moc: Playing previous song.') return weechat.WEECHAT_RC_OK elif args[0] == 'next': if _get_status() == STATUS_STOPPED: weechat.prnt(buffer, 'moc: Not playing, cannot go to next song.') return weechat.WEECHAT_RC_OK else: _execute_command('mocp -f') weechat.prnt(buffer, 'moc: Playing next song.') return weechat.WEECHAT_RC_OK elif args[0] == 'infobar': if infobar['enabled']: infobar['enabled'] = False weechat.config_set_plugin('infobar_enabled', 'off') _remove_infobar() else: infobar['enabled'] = True weechat.config_set_plugin('infobar_enabled', 'on') _add_infobar() return weechat.WEECHAT_RC_OK elif args[0] == 'help': weechat.command(buffer, '/help moc') return weechat.WEECHAT_RC_OK else: weechat.prnt(buffer, 'moc: Unknown command %s' % (args[0])) return weechat.WEECHAT_RC_OK # ================================[ infobar ]================================= def moc_infobar_update(data, buffer, args): """Callback for the bar item""" if _get_status() == STATUS_NOT_RUNNING: return 'moc is not running' if _get_status() == STATUS_STOPPED: return 'moc is not currently playing' else: song = _get_song_info() return _format_np(infobar['format'], song, 'infobar') def moc_infobar_updater(data,cals): """Update the bar item""" if infobar['enabled']: weechat.bar_item_update('moc_infobar') return weechat.WEECHAT_RC_OK def _add_infobar(): """add the infobar for moc_control""" version = int(weechat.info_get('version_number', '')) or 0 if version >= 0x02090000: weechat.bar_new(SCRIPT_NAME, "off", "750", "window", "", "bottom", "horizontal", "vertical", "1", "0", "default", "blue", "cyan", "cyan", "off", "[moc_infobar]") else: weechat.bar_new(SCRIPT_NAME, "off", "750", "window", "", "bottom", "horizontal", "vertical", "1", "0", "default", "blue", "cyan", "off", "[moc_infobar]") def _remove_infobar(): """remove the infobar for moc_control""" weechat.bar_remove(weechat.bar_search(SCRIPT_NAME)) # ==============================[ now playing ]=============================== def moc_now_playing(buffer): """print now playing""" format = '' status = _get_status() if status == STATUS_STOPPED or status == STATUS_NOT_RUNNING: format = output['nothing'] else: song = _get_song_info() format = _format_np(output['format'], song, 'chat') weechat.command(buffer, format) return weechat.WEECHAT_RC_OK def _format_np(np, song, npType): """format the 'now Playing'-String""" np = np.replace('%mocTitle%', song['Title']) np = np.replace('%title%', song['SongTitle']) if npType == 'chat': if song['Artist'] != 'unknown': np = np.replace('%artist%', output['artist']) np = np.replace('%artist%', song['Artist']) else: np = np.replace('%artist%', '') if song['Album'] != 'unknown': np = np.replace('%album%', output['album']) np = np.replace('%album%', song['Album']) else: np = np.replace('%album%', '') else: np = np.replace('%artist%', song['Artist']) np = np.replace('%album%', song['Album']) np = np.replace('%cTime%', song['CurrentTime']) np = np.replace('%cSec%', song['CurrentSec']) np = np.replace('%tTime%', song['TotalTime']) np = np.replace('%tSec%', song['TotalSec']) np = np.replace('%bitrate%', song['Bitrate']) np = np.replace('%avgBitrate%', song['AvgBitrate']) np = np.replace('%rate%', song['Rate']) np = np.replace('%file%', song['File']) np = np.replace('%C', chr(3)) if _get_status() == STATUS_PAUSED: np = np + " - [PAUSED]" return np def _get_song_info(): """Get the song information from moc""" song = {} song['TotalSec'] = '??' song['AvgBitrate'] = '???Kbps' info = _execute_command('mocp -i') for line in info.split('\n'): if line != '': index = line.find(': ') name = line[:index] value = line[index+2:] if value == '': value = 'unknown' song[name] = value.strip() if song['File'].find("://") < 0: song['File'] = os.path.basename(song['File']) if song['TotalSec'] == '??': song['TotalTime'] = '?:??' else: if song['TotalTime'].find("m") > 0: song['TotalTime'] = _format_seconds(song['TotalSec']) if song['CurrentTime'].find("m") > 0: song['CurrentTime'] = _format_seconds(song['CurrentSec']) return song def _format_seconds(s): """return the formated time""" s = int(s) temp = float() temp = float(s) / (60 * 60 * 24) d = int(temp) temp = (temp - d) * 24 h = int(temp) temp = (temp - h) * 60 m = int(temp) temp = (temp - m) * 60 sec = temp if d > 0: return "%id %i:%02i:%02i" % (d, h, m, sec) else: return "%i:%02i:%02i" % (h, m, sec) def _get_status(): """return the Status of moc""" return _execute_command('mocp -i | grep "State:" | cut -d " " -f 2').strip() def _execute_command(cmd): """execute a command""" from subprocess import PIPE proc = subprocess.Popen(cmd, shell = True, stderr = PIPE, stdout = PIPE, close_fds = True) error = proc.stderr.read().decode('utf-8') if error != '': for line in error.split('\n'): if line == 'FATAL_ERROR: The server is not running': return STATUS_NOT_RUNNING output = proc.stdout.read().decode('utf-8') proc.wait() return output # ==================================[ main ]================================== if __name__ == "__main__" and import_ok: if weechat.register(SCRIPT_NAME, SCRIPT_AUTHOR, SCRIPT_VERSION, SCRIPT_LICENSE, SCRIPT_DESC, "moc_unload", ""): load_settings('', '', '') weechat.hook_config('plugins.var.python.moc_control.*', 'load_settings', '') weechat.bar_item_new('moc_infobar', 'moc_infobar_update', '') weechat.hook_timer(infobar['update']*1000,0,0,'moc_infobar_updater','') if infobar['enabled']: _add_infobar() weechat.hook_command( SCRIPT_COMMAND, 'Control moc or display now playing information.', 'play|pause|pp|stop|prev|next|infobar|help', 'Commands Available\n' ' /moc - Display currently playing song.\n' ' /moc play - Start playing music.\n' ' /moc pause - Toggle between pause/playing.\n' ' /moc pp - Toggle between pause/playing.\n' ' /moc stop - Stop playing music.\n' ' /moc prev - Move to the previous song in the playlist.\n' ' /moc next - Move to the next song in the playlist.\n' ' /moc infobar - Toggle the infobar display.\n' '\n' 'Formatting\n' ' %mocTitle% - Replaced with the title from moc.\n' ' %artist% - Replaced with the song artist.\n' ' %title% - Replaced with the song title.\n' ' %album% - Replaced with the song album.\n' ' %file% - Replaced with the filename/url of the song.\n' ' %cTime% - Replaced with how long the song has been playing.\n' ' %cSec% - Replaced with how long the song has been playing (seconcs).\n' ' %tTime% - Replaced with the length of the song.\n' ' %tSec% - Replaced with the length of the song (seconcs).\n' ' %bitrate% - Replaced with the bitrate of the song.\n' ' %avgBitrate% - Replaced with the AvgBitrate of the song.\n' ' %rate% - Replaced with the rate of the song.\n' ' %C## - Make ## the number code of the color you want to use. Use %C by itself to end the color.\n' '\n' 'To see all available settings, please check /set *moc_control*\n', 'play|pause|pp|stop|prev|next|infobar|help', 'moc_command', '' ) # ==================================[ end ]=================================== def moc_unload(): """Unload the plugin from weechat""" if infobar['enabled']: _remove_infobar() return weechat.WEECHAT_RC_OK |