Author Topic: python advice please?  (Read 1971 times)

python advice please?
« on: 17 March, 2023, 11:58:56 am »
I am in the process of upgrading our kitchen audio player (moOde on a Rpi)
A button on the remote needs to be allocated to output this command:
Quote
mpc clear; mpc add http://a.files.bbci.co.uk/media/live/manifesto/audio/simulcast/hls/uk/sbr_high/ak/bbc_radio_three.m3u8; mpc play
as other buttons will be similar e.g. <'mpc clear; mpc add http://a.files.bbci.co.uk/media/live/manifesto/audio/simulcast/hls/uk/sbr_high/ak'><'bbc_radio_four'><'m3u8; mpc play'>
I'm thinking I could combine some declarations and text using lists?
should I 'just send it' or include some checks?
Suppose I have half answered my own question but I'm struggling to get to the right answer

here is an example of what I am using ATM
Quote
  elif((data.keycode[1])=="KEY_MUTE" and data.keystate==data.key_down):
                        print("Detected KEY_MUTE")
                        cmd = ['/var/www/vol.sh', 'mute']
                        subprocess.Popen(cmd).wait()




python advice please?
« Reply #1 on: 17 March, 2023, 01:12:13 pm »
Caveat, I don’t know the domain but would something like this work…
Quote
key_commands = {
    “KEY_MUTE”: (“/var/www/vol.sh”, “mute”),

}

if data.keycode[1] in key_commands and data.keystate == “key_down”:
    subprocess.popen(key_commands[data.keycode[1]]).wait()

What checks would you make?

Re: python advice please?
« Reply #2 on: 17 March, 2023, 02:20:56 pm »
subprocess.run() is probably a better API for your situation, unless you are stuck on Python 2, it will make error checking easier.
https://docs.python.org/3/library/subprocess.html

Re: python advice please?
« Reply #3 on: 17 March, 2023, 03:56:49 pm »
thanks for the advice, it works, although l will as suggested substitute subprocess.run()

Re: python advice please?
« Reply #4 on: 17 March, 2023, 06:30:46 pm »
Just ask chatGPT, it is a little awesome may not be exactly right but (if you are anything like me) it will be a better first stab at the code than I could do. All you have to do is ask

Re: python advice please?
« Reply #5 on: 17 March, 2023, 06:50:52 pm »
Looking at https://github.com/moode-player/moode/blob/develop/www/vol.sh it could just be calling mpc to control mpd, and Python has mpd bindings https://github.com/Mic92/python-mpd2 If you could use these then you would not need to run an external process, but just call MPDClient.setvol(0).