From e7f3cf6170307549f79adf5eb564ee094a3d0e0e Mon Sep 17 00:00:00 2001 From: simon Date: Sat, 23 Jan 2021 17:32:15 +0700 Subject: [PATCH] basic functionality, echos status for notify-send and i3blocks --- .gitignore | 1 + mpd_controller/mpd_main.py | 40 ++++++++++++++++ mpd_controller/{mpc.py => mpd_playback.py} | 23 ++-------- mpd_controller/mpd_state.py | 53 ++++++++++++++++++++++ 4 files changed, 97 insertions(+), 20 deletions(-) create mode 100644 .gitignore create mode 100755 mpd_controller/mpd_main.py rename mpd_controller/{mpc.py => mpd_playback.py} (65%) create mode 100755 mpd_controller/mpd_state.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..bee8a64 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +__pycache__ diff --git a/mpd_controller/mpd_main.py b/mpd_controller/mpd_main.py new file mode 100755 index 0000000..55f9cff --- /dev/null +++ b/mpd_controller/mpd_main.py @@ -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() diff --git a/mpd_controller/mpc.py b/mpd_controller/mpd_playback.py similarity index 65% rename from mpd_controller/mpc.py rename to mpd_controller/mpd_playback.py index 75f48de..b54ba13 100755 --- a/mpd_controller/mpc.py +++ b/mpd_controller/mpd_playback.py @@ -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() + diff --git a/mpd_controller/mpd_state.py b/mpd_controller/mpd_state.py new file mode 100755 index 0000000..33aac21 --- /dev/null +++ b/mpd_controller/mpd_state.py @@ -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 +