Waybar/resources/custom_modules/mediaplayer.py

121 lines
3.5 KiB
Python
Raw Normal View History

#!/usr/bin/env python3
2019-03-27 15:33:15 +00:00
import argparse
2019-03-27 15:33:47 +00:00
import logging
import sys
import signal
import gi
gi.require_version('Playerctl', '2.0')
from gi.repository import Playerctl, GLib
2019-03-27 15:33:47 +00:00
logger = logging.getLogger(__name__)
def on_play(player, status, manager):
2019-03-27 16:00:12 +00:00
logger.info('Received new playback status')
on_metadata(player, player.props.metadata, manager)
def on_metadata(player, metadata, manager):
2019-03-27 16:00:12 +00:00
logger.info("Received new metadata")
track_info = ''
2018-11-26 18:35:03 +00:00
if player.props.player_name == 'spotify' and \
'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())
else:
sys.stdout.write('\n')
sys.stdout.flush()
return
if player.props.status == 'Playing':
sys.stdout.write(track_info + '\n')
else:
sys.stdout.write('' + track_info + '\n')
sys.stdout.flush()
def on_player_appeared(manager, player, selected_player=None):
if player is not None and player.name == selected_player:
init_player(manager, player)
else:
logger.debug('New player appeared, but it\'s not the selected player, skipping')
def on_player_vanished(manager, player):
2019-03-27 16:00:12 +00:00
logger.info("Player has vanished")
sys.stdout.write("\n")
sys.stdout.flush()
def init_player(manager, name):
2019-03-27 16:00:12 +00:00
logger.debug("Initialize player: {player}".format(player=name.name))
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')
sys.stdout.write("\n")
sys.stdout.flush()
# loop.quit()
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
parser.add_argument('-v', '--verbose', action="count", default=0)
# Define for which player we're listening
parser.add_argument('--player')
2019-03-27 15:33:15 +00:00
return parser.parse_args()
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.
# With every occurance of -v it's lowered by one
logger.setLevel(max((3 - arguments.verbose) * 10, 0))
# Log the sent command line arguments
logger.debug('Arguments received {}'.format(vars(arguments)))
manager = Playerctl.PlayerManager()
loop = GLib.MainLoop()
manager.connect('name-appeared', lambda *args: on_player_appeared(*args, arguments.player))
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:
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
init_player(manager, player)
loop.run()
if __name__ == "__main__":
main()