2018-11-22 14:48:52 +00:00
|
|
|
#!/usr/bin/env python3
|
2019-03-27 15:33:15 +00:00
|
|
|
import argparse
|
2019-03-27 15:33:47 +00:00
|
|
|
import logging
|
2018-11-22 14:48:52 +00:00
|
|
|
import sys
|
|
|
|
import signal
|
|
|
|
import gi
|
2019-03-31 14:12:38 +00:00
|
|
|
import json
|
2018-11-22 14:48:52 +00:00
|
|
|
gi.require_version('Playerctl', '2.0')
|
|
|
|
from gi.repository import Playerctl, GLib
|
|
|
|
|
2019-03-27 15:33:47 +00:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
2018-11-22 14:48:52 +00:00
|
|
|
|
2019-03-31 14:12:38 +00:00
|
|
|
def write_output(text, player):
|
|
|
|
logger.info('Writing output')
|
|
|
|
|
|
|
|
output = {'text': text,
|
|
|
|
'class': 'custom-' + player.props.player_name,
|
|
|
|
'alt': player.props.player_name}
|
|
|
|
|
|
|
|
sys.stdout.write(json.dumps(output) + '\n')
|
|
|
|
sys.stdout.flush()
|
|
|
|
|
|
|
|
|
2018-11-22 14:48:52 +00:00
|
|
|
def on_play(player, status, manager):
|
2019-03-27 16:00:12 +00:00
|
|
|
logger.info('Received new playback status')
|
2018-11-22 14:48:52 +00:00
|
|
|
on_metadata(player, player.props.metadata, manager)
|
|
|
|
|
|
|
|
|
|
|
|
def on_metadata(player, metadata, manager):
|
2019-03-31 14:10:42 +00:00
|
|
|
logger.info('Received new metadata')
|
2018-11-22 14:48:52 +00:00
|
|
|
track_info = ''
|
|
|
|
|
2018-11-26 18:35:03 +00:00
|
|
|
if player.props.player_name == 'spotify' and \
|
2018-11-22 14:48:52 +00:00
|
|
|
'mpris:trackid' in metadata.keys() and \
|
|
|
|
':ad:' in player.props.metadata['mpris:trackid']:
|
|
|
|
track_info = 'AD PLAYING'
|
|
|
|
elif player.get_artist() != '' and player.get_title() != '':
|
|
|
|
track_info = '{artist} - {title}'.format(artist=player.get_artist(),
|
|
|
|
title=player.get_title())
|
|
|
|
|
2019-05-26 22:05:21 +00:00
|
|
|
if player.props.status != 'Playing' and track_info:
|
2019-03-31 14:12:38 +00:00
|
|
|
track_info = ' ' + track_info
|
|
|
|
write_output(track_info, player)
|
2018-11-22 14:48:52 +00:00
|
|
|
|
|
|
|
|
2019-03-27 16:30:14 +00:00
|
|
|
def on_player_appeared(manager, player, selected_player=None):
|
2019-07-31 09:49:18 +00:00
|
|
|
if player is not None and (selected_player is None or player.name == selected_player):
|
2019-03-27 16:30:14 +00:00
|
|
|
init_player(manager, player)
|
|
|
|
else:
|
2019-03-31 14:10:42 +00:00
|
|
|
logger.debug("New player appeared, but it's not the selected player, skipping")
|
2019-03-27 16:30:14 +00:00
|
|
|
|
|
|
|
|
2018-11-22 14:48:52 +00:00
|
|
|
def on_player_vanished(manager, player):
|
2019-03-31 14:10:42 +00:00
|
|
|
logger.info('Player has vanished')
|
|
|
|
sys.stdout.write('\n')
|
2018-11-22 14:48:52 +00:00
|
|
|
sys.stdout.flush()
|
|
|
|
|
|
|
|
|
2019-03-27 15:10:36 +00:00
|
|
|
def init_player(manager, name):
|
2019-03-31 14:10:42 +00:00
|
|
|
logger.debug('Initialize player: {player}'.format(player=name.name))
|
2018-11-22 14:48:52 +00:00
|
|
|
player = Playerctl.Player.new_from_name(name)
|
|
|
|
player.connect('playback-status', on_play, manager)
|
|
|
|
player.connect('metadata', on_metadata, manager)
|
|
|
|
manager.manage_player(player)
|
|
|
|
on_metadata(player, player.props.metadata, manager)
|
|
|
|
|
|
|
|
|
|
|
|
def signal_handler(sig, frame):
|
2019-03-27 16:00:12 +00:00
|
|
|
logger.debug('Received signal to stop, exiting')
|
2019-03-31 14:10:42 +00:00
|
|
|
sys.stdout.write('\n')
|
2018-11-22 14:48:52 +00:00
|
|
|
sys.stdout.flush()
|
2019-03-27 15:10:36 +00:00
|
|
|
# loop.quit()
|
2018-11-22 14:48:52 +00:00
|
|
|
sys.exit(0)
|
|
|
|
|
|
|
|
|
2019-03-27 15:33:15 +00:00
|
|
|
def parse_arguments():
|
|
|
|
parser = argparse.ArgumentParser()
|
|
|
|
|
2019-03-27 15:33:47 +00:00
|
|
|
# Increase verbosity with every occurance of -v
|
2019-03-31 14:10:42 +00:00
|
|
|
parser.add_argument('-v', '--verbose', action='count', default=0)
|
2019-03-27 15:33:47 +00:00
|
|
|
|
2019-03-27 16:30:14 +00:00
|
|
|
# Define for which player we're listening
|
|
|
|
parser.add_argument('--player')
|
|
|
|
|
2019-03-27 15:33:15 +00:00
|
|
|
return parser.parse_args()
|
|
|
|
|
|
|
|
|
2019-03-27 15:10:36 +00:00
|
|
|
def main():
|
2019-03-27 15:33:15 +00:00
|
|
|
arguments = parse_arguments()
|
2019-03-27 15:33:47 +00:00
|
|
|
|
|
|
|
# Initialize logging
|
|
|
|
logging.basicConfig(stream=sys.stderr, level=logging.DEBUG,
|
|
|
|
format='%(name)s %(levelname)s %(message)s')
|
|
|
|
|
|
|
|
# Logging is set by default to WARN and higher.
|
2019-03-31 14:10:42 +00:00
|
|
|
# With every occurrence of -v it's lowered by one
|
2019-03-27 15:33:47 +00:00
|
|
|
logger.setLevel(max((3 - arguments.verbose) * 10, 0))
|
|
|
|
|
|
|
|
# Log the sent command line arguments
|
|
|
|
logger.debug('Arguments received {}'.format(vars(arguments)))
|
|
|
|
|
2019-03-27 15:10:36 +00:00
|
|
|
manager = Playerctl.PlayerManager()
|
|
|
|
loop = GLib.MainLoop()
|
|
|
|
|
2019-03-27 16:30:14 +00:00
|
|
|
manager.connect('name-appeared', lambda *args: on_player_appeared(*args, arguments.player))
|
2019-03-27 15:10:36 +00:00
|
|
|
manager.connect('player-vanished', on_player_vanished)
|
|
|
|
|
|
|
|
signal.signal(signal.SIGINT, signal_handler)
|
|
|
|
signal.signal(signal.SIGTERM, signal_handler)
|
|
|
|
|
|
|
|
for player in manager.props.player_names:
|
2019-03-27 16:30:14 +00:00
|
|
|
if arguments.player is not None and arguments.player != player.name:
|
|
|
|
logger.debug('{player} is not the filtered player, skipping it'
|
|
|
|
.format(player=player.name)
|
|
|
|
)
|
|
|
|
continue
|
|
|
|
|
2019-03-27 15:10:36 +00:00
|
|
|
init_player(manager, player)
|
|
|
|
|
|
|
|
loop.run()
|
2018-11-22 14:48:52 +00:00
|
|
|
|
|
|
|
|
2019-03-31 14:10:42 +00:00
|
|
|
if __name__ == '__main__':
|
2019-03-27 15:10:36 +00:00
|
|
|
main()
|
2018-11-22 14:48:52 +00:00
|
|
|
|