Created
March 2, 2018 07:16
-
-
Save fritschy/e1e15f14535617bcf868b75ec8c3daa8 to your computer and use it in GitHub Desktop.
Control mpc or spotify, giving precedence to spotify
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python3 | |
| from glob import glob | |
| from os import readlink, getenv | |
| from sys import argv, exit | |
| def is_spotify(): | |
| for path in glob('/proc/*/exe'): | |
| try: | |
| if readlink(path) == '/usr/share/spotify/spotify': | |
| return True | |
| except: | |
| pass | |
| return False | |
| cmd = argv[1] if len(argv) >= 2 else 'status' | |
| current = {} | |
| player = {} | |
| if is_spotify(): | |
| from dbus import SessionBus, Interface | |
| bus = SessionBus() | |
| proxy = bus.get_object('org.mpris.MediaPlayer2.spotify','/org/mpris/MediaPlayer2') | |
| def get_method(n): | |
| return proxy.get_dbus_method(n, dbus_interface='org.mpris.MediaPlayer2.Player') | |
| player['next'] = get_method("Next") | |
| player['prev'] = get_method("Previous") | |
| player['toggle'] = get_method("PlayPause") | |
| player['pause'] = get_method("Pause") | |
| player['play'] = get_method("Play") | |
| props = Interface(proxy, 'org.freedesktop.DBus.Properties') | |
| metadata = props.Get('org.mpris.MediaPlayer2.Player', 'Metadata') | |
| current['title'] = str(metadata.get('xesam:title', '')) | |
| current['artist'] = ' + '.join(map(str, metadata.get('xesam:artist', ['']))) | |
| current['status'] = str(props.Get('org.mpris.MediaPlayer2.Player', 'PlaybackStatus')).lower() | |
| current['position'] = '' | |
| else: | |
| from mpd import MPDClient | |
| mpc = MPDClient() | |
| mpc.connect(getenv('MPD_HOST'), 0) | |
| s = mpc.status() | |
| c = mpc.currentsong() | |
| current['status'] = 'paused' if s['state'] == 'pause' else 'playing' | |
| current['artist'] = c['artist'] | |
| current['title'] = c['title'] | |
| current['position'] = '(%d%%) ' % int(100 * (float(s['elapsed']) / float(s['duration']))) | |
| player['next'] = mpc.next | |
| player['prev'] = mpc.previous | |
| player['toggle'] = mpc.play if current['status'] == 'paused' else mpc.pause | |
| player['pause'] = mpc.pause | |
| player['play'] = mpc.play | |
| short_statii = { 'stopped': '◼', 'playing': '▶', 'paused': 'Ⅱ' } | |
| shorten = lambda s, l: s[:l-3] + '...' if len(s) > l else s | |
| current['artist'] = shorten(current['artist'], 20) | |
| current['title'] = shorten(current['title'], 40) | |
| current['short_status'] = short_statii[current['status']] | |
| if cmd == 'current': | |
| print('%(short_status)s %(position)s%(artist)s - %(title)s' % current) | |
| elif cmd == 'status': | |
| print(current['status']) | |
| elif cmd in player: | |
| player[cmd]() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment