basic functionality, echos status for notify-send and i3blocks

This commit is contained in:
simon 2021-01-23 17:32:15 +07:00
parent 9acffaa658
commit e7f3cf6170
4 changed files with 97 additions and 20 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
__pycache__

40
mpd_controller/mpd_main.py Executable file
View File

@ -0,0 +1,40 @@
#!/usr/bin/env python3
# https://python-mpd2.readthedocs.io/en/latest
# https://dunst-project.org/documentation/
import sys
from mpd import MPDClient
import mpd_playback
import mpd_state
cover_art_temp = '/tmp/mpd_cover_art_temp.jpg'
arguments = sys.argv
if len(arguments) == 1:
mpc_command = None
elif len(arguments) == 2:
mpc_command = arguments[1]
def main():
# connect
try:
client = MPDClient()
client.connect("localhost", 6600)
except ConnectionRefusedError:
# mpd is not running, stop here
return
# follow mpc_command
if mpc_command == 'toggle':
mpd_playback.toggle(client)
# show status
mpd_state.get_state(client, cover_art_temp)
# close connection
client.close()
client.disconnect()
if __name__ == '__main__':
main()

View File

@ -1,11 +1,5 @@
#!/usr/bin/env python3
# https://python-mpd2.readthedocs.io/en/latest
import sys
from mpd import MPDClient
from time import sleep
def fade(way, current_vol, client):
""" gracefull fading """
if way == 'out':
@ -19,10 +13,9 @@ def fade(way, current_vol, client):
sleep(0.03)
def mpc_toggle():
def toggle(client):
""" toggles play status """
client = MPDClient()
client.connect("localhost", 6600)
# setup
client_status = client.status()
# switch
current_vol = int(client_status['volume'])
@ -33,14 +26,4 @@ def mpc_toggle():
client.setvol(0)
client.play()
fade('in', current_vol, client)
def main():
""" main parser for args """
mpc_command = sys.argv[1]
if mpc_command == 'toggle':
mpc_toggle()
if __name__ == '__main__':
main()

53
mpd_controller/mpd_state.py Executable file
View File

@ -0,0 +1,53 @@
import subprocess
def print_state(artist, album, song_title, mpd_status):
""" print three lines for i3blocks """
# setup based on mpd_status
if mpd_status == 'play':
print_main = f'{artist} - {album} - {song_title}'
print_small = f'{artist} - {song_title}'
print_color = '#FFFFFF'
elif mpd_status == 'pause':
print_main = f'{artist} - {song_title}'
print_small = f'{song_title}'
print_color = '#404040'
# print
print(print_main)
print(print_small)
print(print_color)
return
def get_state(client, cover_art_temp):
""" sends a notification of the current status """
# read out status from client
now_playing = client.currentsong()
client_status = client.status()
# parse status
music_file = now_playing['file']
artist = now_playing['artist']
album = now_playing['album']
song_title = now_playing['title']
# try to write cover art to temp file
try:
cover_art = client.readpicture(music_file)
with open(cover_art_temp, 'w+b') as f:
f.write(cover_art['binary'])
except:
cover_art = False
# set player status icon
mpd_status = client_status['state']
if mpd_status == 'play':
icon = ""
elif mpd_status == 'pause':
icon = ""
# print for i3blocks
print_state(artist, album, song_title, mpd_status)
# send message
title = icon + '\t' + artist
message = album + "-" + song_title
subprocess.call(['notify-send', title, message, '-i', cover_art_temp])
return