INTERACT FORUM

Please login or register.

Login with username, password and session length
Advanced search  
Pages: [1]   Go Down

Author Topic: MC_Notifier - a simple Linux system notification script for MC (using libnotify)  (Read 3639 times)

mwillems

  • MC Beta Team
  • Citizen of the Universe
  • *****
  • Posts: 5234
  • "Linux Merit Badge" Recipient

I cooked this up in another thread, but thought it might be generally useful, at least as a point of departure.

MC_Notifier is a bash script that submits a libnotify notification on track change in MC (which show as a floating window on many linux desktop environments).  It will show the Artist,  Album, and Track title.  By default it runs every two seconds, but you can change that easily by modifying the "sleep" parameter (if you want it to be more responsive or eat fewer cycles).  With a little more work, it might even be able to show album art, but that's a bit more work than I have time for at the moment.  It will run until you kill it, so is suitable to launch with your DE's startup.  

The script is kind of trivial, and it's not perfect; it performs a single very basic data validation step.  So far it seems to cope with some of the usual suspects (ampersands, single quotes, double quotes, and dollar signs) just fine but it may act strangely when encountering strange characters in album titles, etc.  Folks could easily make it more efficient, robust, and/or add more functionality, and I may add a few things myself over time (I have a few optimization ideas that I'll fold in myself time permitting).  This is intended as a proof of concept, and is provided as is with no warranty whatsoever.

The script assumes the MC instance you want to monitor is running locally and that the server is available on the default port; you need to enable media network for it to work (I'm pretty sure the web interface depends on it). I tested on Gnome and Cinnamon, and it ran nicely. If you're running XFCE or LXDE or any of the other DE's without integrated libnotify support, you'll need a plugin/additional package to show the notifications.  See here for more info: https://wiki.archlinux.org/index.php/Desktop_notifications.  The only dependency, other than basic UNIX utilities like sed, wget, and diff (which are likely to be found on any modern Linux installation) is libnotify.

Code: [Select]
#!/bin/bash

mkdir -p ~/.MC_Notifier
touch ~/.MC_Notifier/Filekey.Current

while true; do
wget -q localhost\:52199/MCWS/v1/Playback/Info\?Zone\=\-1 -O ~/.MC_Notifier/Info
mv ~/.MC_Notifier/Filekey.Current ~/.MC_Notifier/Filekey.Old
sed -n 's:.*<Item Name="FileKey">\(.*\)</Item>.*:\1:p' ~/.MC_Notifier/Info > ~/.MC_Notifier/Filekey.Current
diff ~/.MC_Notifier/Filekey.Current ~/.MC_Notifier/Filekey.Old > /dev/null
if [[ $? -ne 0 ]]; then
       sed -- 's/\&amp;/\&/g' ~/.MC_Notifier/Info > ~/.MC_Notifier/Info2
   mv ~/.MC_Notifier/Info2 ~/.MC_Notifier/Info
ALBUM=$(sed -n 's:.*<Item Name="Album">\(.*\)</Item>.*:\1:p' ~/.MC_Notifier/Info)
ARTIST=$(sed -n 's:.*<Item Name="Artist">\(.*\)</Item>.*:\1:p' ~/.MC_Notifier/Info)
TRACK=$(sed -n 's:.*<Item Name="Name">\(.*\)</Item>.*:\1:p' ~/.MC_Notifier/Info)
notify-send -i "/usr/lib/jriver/Media Center 21/Data/Default Art/Logo.png" -t 2000 "$ALBUM" "$ARTIST - $TRACK"
fi
sleep 2
done


Update 08/09/2015 - The code has been updated to now show Mediacenter's Icon; I took a stab at getting it to display cover art instead , but the notification system in Gnome seems to do some kind of aggressive caching so that it won't recheck an icon file once it's loaded once (so it would only ever show the album art from the first loaded album).  I'll keep tinkering with that and see if I can work it out, but for now the Mediacenter icon is better than nothing or a stock icon.  If you're using MC 20, you'll need to change the "21" in the notify-send line to "20"
Logged

geier22

  • Regular Member
  • Galactic Citizen
  • ****
  • Posts: 427

@mwillems :
Thanks for the Logo - Looks nice  :D :D
Logged
Debian Testing x64 (multiarch) Xfce
TEAC UD-H01 - Yamaha A-S1000 /
Midrange- Studiomonitore by FÖÖN
AsRock Z390 Extreme4- Intel Core i9 9900/ 32 GB Ram

geier22

  • Regular Member
  • Galactic Citizen
  • ****
  • Posts: 427

@mwillems :

the script works with MC 20 well. But on the same machine with MC 21, nothing happens.
I guess that the error in this line :

Code: [Select]
wget -q localhost\:52199/MCWS/v1/Playback/Info\?Zone\=\-1 -O ~/.MC_Notifier/Info
How needs to be changed this line to work with MC 21?
Logged
Debian Testing x64 (multiarch) Xfce
TEAC UD-H01 - Yamaha A-S1000 /
Midrange- Studiomonitore by FÖÖN
AsRock Z390 Extreme4- Intel Core i9 9900/ 32 GB Ram

BryanC

  • MC Beta Team
  • Citizen of the Universe
  • *****
  • Posts: 2665

@mwillems :

the script works with MC 20 well. But on the same machine with MC 21, nothing happens.
I guess that the error in this line :

Code: [Select]
wget -q localhost\:52199/MCWS/v1/Playback/Info\?Zone\=\-1 -O ~/.MC_Notifier/Info
How needs to be changed this line to work with MC 21?

Code: [Select]
notify-send -i "/usr/lib/jriver/Media Center 21/Data/Default Art/Logo.png" -t 2000 "$ALBUM" "$ARTIST - $TRACK"
Probably should change 21 to 22 here. Not sure if it would cause the whole thing to error out but since I don't see any if checking, it likely could.

Edit: Derp. This isn't the first time I've gotten the current Fedora and MC versions mixed up.
Logged

mwillems

  • MC Beta Team
  • Citizen of the Universe
  • *****
  • Posts: 5234
  • "Linux Merit Badge" Recipient

Code: [Select]
notify-send -i "/usr/lib/jriver/Media Center 21/Data/Default Art/Logo.png" -t 2000 "$ALBUM" "$ARTIST - $TRACK"
Probably should change 21 to 22 here. Not sure if it would cause the whole thing to error out but since I don't see any if checking, it likely could.

It's supposed to be 21, not 22; if he had MC 20 there he may need to change it to 21 (is that what you meant)?  The icon won't hang the notification (at least it doesn't when I test it here) you just get a notification with no icon.

It's working fine on MC 21 for me so I'm betting on a configuration issue.  If it's erroring on the wget line that means that JRiver is not responding to the web service call.  Did you enable media network after upgrading to MC21?

What happens if you go to this address in a browser?:
Code: [Select]
http://localhost:52199/Gizmo/library.html
Logged

geier22

  • Regular Member
  • Galactic Citizen
  • ****
  • Posts: 427

I had changed the icon of MC20 in the main menu, because in the Systray the icon "unknown" appeared. After I had done that, no longer "notify-send" worked,. I think, this has nothing to do with the path in the script.
The actions of KDE are inscrutable. :o
Now I have MC 20 and 21 completely erased, including all directories in / home / and all desktop-files
Reinstalling MC 20 - it works again.
MC 21.0.3. says again that trial period expired. I'll wait with MC 21, until a license is available. I have no desire to argue with MC  ::) :-[

What happens if you go to this address in a browser?:
Code: [Select]
http://localhost:52199/Gizmo/library.html ---> this works (but I had there certainly changed nothing)
Logged
Debian Testing x64 (multiarch) Xfce
TEAC UD-H01 - Yamaha A-S1000 /
Midrange- Studiomonitore by FÖÖN
AsRock Z390 Extreme4- Intel Core i9 9900/ 32 GB Ram
Pages: [1]   Go Up