[GH-ISSUE #185] --onevent documentation #127

Closed
opened 2026-02-27 19:28:59 +03:00 by kerem · 11 comments
Owner

Originally created by @DavidM42 on GitHub (Mar 10, 2018).
Original GitHub issue: https://github.com/librespot-org/librespot/issues/185

The documentation describes how to use the apparently older --onstart and --onstop event handler but the help text and the project itself demand a --onevent handler.
Sorry for my Noob question but I couldn't find any info on how to use it in the wiki or anywhre else. I searched for it on the issue tracker,Google,.. but still got no results.
I know there is a help text but it's pretty shortly explained there.
So to break it down could you please instruct me how the explicit event type get's passed and I would be happy to try to update your wiki on this option.

Originally created by @DavidM42 on GitHub (Mar 10, 2018). Original GitHub issue: https://github.com/librespot-org/librespot/issues/185 The documentation describes how to use the apparently older --onstart and --onstop event handler but the help text and the project itself demand a --onevent handler. Sorry for my Noob question but I couldn't find any info on how to use it in the wiki or anywhre else. I searched for it on the issue tracker,Google,.. but still got no results. I know there is a help text but it's pretty shortly explained there. So to break it down could you please instruct me how the explicit event type get's passed and I would be happy to try to update your wiki on this option.
kerem 2026-02-27 19:28:59 +03:00
  • closed this issue
  • added the
    wiki
    label
Author
Owner

@2pl commented on GitHub (Mar 10, 2018):

As far as I could test, you get passed a "start" or "stop" value through environment variable PLAYER_EVENT. So you need to test the value of PLAYER_EVENT in your script and decide the appropriate action.

<!-- gh-comment-id:372041682 --> @2pl commented on GitHub (Mar 10, 2018): As far as I could test, you get passed a "start" or "stop" value through environment variable PLAYER_EVENT. So you need to test the value of PLAYER_EVENT in your script and decide the appropriate action.
Author
Owner

@kingosticks commented on GitHub (Mar 10, 2018):

You can find the code in question at github.com/librespot-org/librespot@aa880f8888/src/player_event_handler.rs and hopefully it's quite easy to follow. An updated wikipage would be great. We could also look at adding something (short) to the option's help text.

<!-- gh-comment-id:372042687 --> @kingosticks commented on GitHub (Mar 10, 2018): You can find the code in question at https://github.com/librespot-org/librespot/blob/aa880f8888226a8e5fc6e1e54dfb7cf58176ac95/src/player_event_handler.rs and hopefully it's quite easy to follow. An updated wikipage would be great. We could also look at adding something (short) to the option's help text.
Author
Owner

@DavidM42 commented on GitHub (Mar 10, 2018):

Yeah thx for the quick help. I worked it out but it was a hard thing to do. I couldn't test for the environment variables because they were in the "raspotify" user account because I use that project which has librespot as it's core.
I'm now able to get the playback state in a python script which is the onevent handler. Sadly I still gave up for now because my plan to switch rc mains switches vía this script did not work for some damn reason I can't find.
Pain in the ass to get the permissions of files,gpio and everything else when It's run as service in another user account.

Update of the wikipage as well as the help text should not be that much work and it's pretty easy to use if it's standalone librespot.
I would like to help but I don't know if I undestand it enough for now to write an understandable explanation.

<!-- gh-comment-id:372060588 --> @DavidM42 commented on GitHub (Mar 10, 2018): Yeah thx for the quick help. I worked it out but it was a hard thing to do. I couldn't test for the environment variables because they were in the "raspotify" user account because I use that project which has librespot as it's core. I'm now able to get the playback state in a python script which is the onevent handler. Sadly I still gave up for now because my plan to switch rc mains switches vía this script did not work for some damn reason I can't find. Pain in the ass to get the permissions of files,gpio and everything else when It's run as service in another user account. Update of the wikipage as well as the help text should not be that much work and it's pretty easy to use if it's standalone librespot. I would like to help but I don't know if I undestand it enough for now to write an understandable explanation.
Author
Owner

@madmodder123 commented on GitHub (Mar 11, 2018):

@DavidM42 Can you share the python code you use to grab the playback state? I am also using raspotify and I am trying to figure out a way to integrate Spotify functionality into my background music script.
Also... do you know if Raspotify uses this branch of librespot or the original one?

<!-- gh-comment-id:372080070 --> @madmodder123 commented on GitHub (Mar 11, 2018): @DavidM42 Can you share the python code you use to grab the playback state? I am also using raspotify and I am trying to figure out a way to integrate Spotify functionality into my background music script. Also... do you know if Raspotify uses this branch of librespot or the original one?
Author
Owner

@michaelherger commented on GitHub (Mar 11, 2018):

This is the very basic shell script I used for testing:

#!/bin/sh

echo Spotify event: ---------------------------------------------
echo PLAYER_EVENT: $PLAYER_EVENT
echo TRACK_ID:     $TRACK_ID
echo OLD_TRACK_ID: $OLD_TRACK_ID

I'm not a python guy, but the equivalent to echo $TRACK_ID seems to be:

import os
print(os.environ['TRACK_ID'])

The OLD_TRACK_ID variable would be set on change events: when a new track is being played.

<!-- gh-comment-id:372091918 --> @michaelherger commented on GitHub (Mar 11, 2018): This is the very basic shell script I used for testing: ``` #!/bin/sh echo Spotify event: --------------------------------------------- echo PLAYER_EVENT: $PLAYER_EVENT echo TRACK_ID: $TRACK_ID echo OLD_TRACK_ID: $OLD_TRACK_ID ``` I'm not a python guy, but the equivalent to `echo $TRACK_ID` seems to be: ``` import os print(os.environ['TRACK_ID']) ``` The `OLD_TRACK_ID` variable would be set on `change` events: when a new track is being played.
Author
Owner

@DavidM42 commented on GitHub (Mar 11, 2018):

@madmodder123 @michaelherger
Michael's info was totally correct aswell as his Python example but still here is my Python script.
(if you use raspotify keep in mind that the script needs to be executable for raspotify user. I am not very experienced with permissions so I gave it an unsafe chmod 777 rights so everyone can do everything)

import os

if os.environ['PLAYER_EVENT'] == "start":
    os.system("/home/pi/raspberry-remote/send ***** 1 1")
elif os.environ['PLAYER_EVENT'] == "change":
    os.system("/home/pi/raspberry-remote/send ***** 1 1")
elif os.environ['PLAYER_EVENT'] == "stop":
    os.system("/home/pi/raspberry-remote/send ***** 1 0")
<!-- gh-comment-id:372105394 --> @DavidM42 commented on GitHub (Mar 11, 2018): @madmodder123 @michaelherger Michael's info was totally correct aswell as his Python example but still here is my Python script. (if you use raspotify keep in mind that the script needs to be executable for raspotify user. I am not very experienced with permissions so I gave it an unsafe chmod 777 rights so everyone can do everything) ```python import os if os.environ['PLAYER_EVENT'] == "start": os.system("/home/pi/raspberry-remote/send ***** 1 1") elif os.environ['PLAYER_EVENT'] == "change": os.system("/home/pi/raspberry-remote/send ***** 1 1") elif os.environ['PLAYER_EVENT'] == "stop": os.system("/home/pi/raspberry-remote/send ***** 1 0") ```
Author
Owner

@ComlOnline commented on GitHub (Mar 11, 2018):

@DavidM42 File permissons 755 should work fine here (owner has full read write execute, all others have read and execute permissions).

The wiki does need some work, Feel free to update bits where you can :)

<!-- gh-comment-id:372124110 --> @ComlOnline commented on GitHub (Mar 11, 2018): @DavidM42 File permissons 755 should work fine here (owner has full read write execute, all others have read and execute permissions). The wiki does need some work, Feel free to update bits where you can :)
Author
Owner

@madmodder123 commented on GitHub (Mar 11, 2018):

@DavidM42 @michaelherger
I am decent at working with scripts but I have never had to deal with environmental variables, so for some reason I cannot figure out how to get either of these scripts to work.
I try to run them by doing the following...

sudo runuser -l raspotify -c 'cd /home/pi/scripts && ./test.sh'
or
sudo runuser -l raspotify -c 'python /home/pi/scripts/test.py'

But nothing gets displayed in the terminal.
I also tried this command to see if the variables are attached to the user:
sudo runuser -u raspotify 'printenv'
But none of the relevant variables are displayed.

I feel stupid, and i'm sure it is something simple I am overlooking, help would be appreciated. :)

<!-- gh-comment-id:372155753 --> @madmodder123 commented on GitHub (Mar 11, 2018): @DavidM42 @michaelherger I am decent at working with scripts but I have never had to deal with environmental variables, so for some reason I cannot figure out how to get either of these scripts to work. I try to run them by doing the following... ``` sudo runuser -l raspotify -c 'cd /home/pi/scripts && ./test.sh' or sudo runuser -l raspotify -c 'python /home/pi/scripts/test.py' ``` But nothing gets displayed in the terminal. I also tried this command to see if the variables are attached to the user: `sudo runuser -u raspotify 'printenv'` But none of the relevant variables are displayed. I feel stupid, and i'm sure it is something simple I am overlooking, help would be appreciated. :)
Author
Owner

@michaelherger commented on GitHub (Mar 12, 2018):

Those environment variables are only exposed when one of the events is triggered. You don't run those scripts stand-alone, but tell librespot to call it when an event fires. librespot --onevent /path/to/your/script.

<!-- gh-comment-id:372201190 --> @michaelherger commented on GitHub (Mar 12, 2018): Those environment variables are only exposed when one of the events is triggered. You don't run those scripts stand-alone, but tell librespot to call it when an event fires. `librespot --onevent /path/to/your/script`.
Author
Owner

@madmodder123 commented on GitHub (Mar 13, 2018):

@michaelherger @DavidM42
Thanks, I knew it was something simple. Anyways I seem to have run into another problem.
When I start playing a song I look at the console output and it displays

PLAYER_EVENT: start
PLAYER_EVENT: stop
PLAYER_EVENT: start
PLAYER_EVENT: stop

Over and over...
Looking at my running processes it seems like librespot runs two processes and I imagine one of them is active and the other one isn't, which is causing this issue. Further proof of this is that I set the delay between each echo to 5s and like clockwork the echo messages come in pairs of start/stop.
I'm going to work with the code some more and figure out a way to only run the script once

<!-- gh-comment-id:372521681 --> @madmodder123 commented on GitHub (Mar 13, 2018): @michaelherger @DavidM42 Thanks, I knew it was something simple. Anyways I seem to have run into another problem. When I start playing a song I look at the console output and it displays ``` PLAYER_EVENT: start PLAYER_EVENT: stop PLAYER_EVENT: start PLAYER_EVENT: stop ``` Over and over... Looking at my running processes it seems like librespot runs two processes and I imagine one of them is active and the other one isn't, which is causing this issue. Further proof of this is that I set the delay between each echo to 5s and like clockwork the echo messages come in pairs of start/stop. I'm going to work with the code some more and figure out a way to only run the script once
Author
Owner

@sashahilton00 commented on GitHub (Mar 22, 2018):

This is definitely an issue with the event system, as I'm seeing exactly the same thing when testing out #190. Will try and pinpoint what is causing it.

<!-- gh-comment-id:375160625 --> @sashahilton00 commented on GitHub (Mar 22, 2018): This is definitely an issue with the event system, as I'm seeing exactly the same thing when testing out #190. Will try and pinpoint what is causing it.
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/librespot#127
No description provided.