Last active
July 22, 2020 04:59
-
-
Save vladkorotnev/f7ddf4a2d7372cef2b43b56eed291fb9 to your computer and use it in GitHub Desktop.
MPD notififcations and VK status updates
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
#### CONFIG #### | |
vk_token= # Oauth token with status scope | |
vk_prefix="✇" | |
notify_time=5000 | |
MPD_HOST="/mpd/socket" | |
LCD_HOST="127.0.0.1" | |
LCD_PORT="13666" | |
################ | |
mpd_playing=0 | |
mpd_volume=100 | |
mpd_title="" | |
lcd_enabled=0 | |
log="/dev/null" | |
for pid in $(pidof -x `basename $0`); do | |
if [ $pid != $$ ]; then | |
echo "[$(date)] : mpdwatchd : Process is already running with PID $pid" | |
exit 1 | |
fi | |
done | |
lcd_open() { | |
if pidof -x "LCDd" >/dev/null; then | |
lcd_enabled=1 | |
exec {LCD}<>"/dev/tcp/$LCD_HOST/$LCD_PORT" | |
logger "LCDd connected" | |
lcd_sendcmd hello | |
lcd_sendcmd client_set name "mpdwatchd" | |
fi | |
} | |
lcd_close() { | |
if (( $lcd_enabled > 0 )) | |
then | |
logger "Closing LCD" | |
exec {LCD}<&- | |
fi | |
} | |
lcd_sendcmd() { | |
if (( $lcd_enabled > 0 )) | |
then | |
echo $@ >&${LCD} | |
read lcdresponse <&${LCD} | |
fi | |
} | |
lcd_update() { | |
if (( $mpd_playing > 0 )) | |
then | |
if [ -z "$lcd_created" ]; | |
then | |
logger "Creating lcd" | |
lcd_sendcmd screen_add mpd | |
lcd_sendcmd screen_set mpd name "NowPlayingWidget" duration -1 | |
lcd_sendcmd widget_add mpd top_line title | |
lcd_sendcmd widget_add mpd bottom_line scroller | |
lcd_created=1 | |
fi | |
lcd_sendcmd widget_set mpd top_line "{Now playing}" | |
lcd_sendcmd widget_set mpd bottom_line 1 2 20 2 h 2 "{$mpd_title}" | |
# Topmost notifier | |
lcd_sendcmd screen_add mpd_alert | |
lcd_sendcmd screen_set mpd_alert priority alert name "MpdChangeAlert" timeout 40 | |
lcd_sendcmd widget_add mpd_alert top_line title | |
lcd_sendcmd widget_add mpd_alert bottom_line scroller | |
lcd_sendcmd widget_set mpd_alert top_line "{Now playing}" | |
lcd_sendcmd widget_set mpd_alert bottom_line 1 2 20 2 h 2 "{$mpd_title}" | |
else | |
if [ -n "$lcd_created" ]; | |
then | |
logger "Deleting lcd" | |
lcd_sendcmd screen_del mpd | |
unset lcd_created | |
fi | |
fi | |
} | |
logger() { | |
echo "$(date +'[ %d %b %Y %H:%M ]') :: $*" >> $log | |
} | |
update_mpd_status() { | |
mpc status | grep "\[playing\]" >/dev/null | |
mpd_notplaying=$? | |
if (( $mpd_notplaying > 0 )) | |
then | |
mpd_playing=0 | |
else | |
mpd_playing=1 | |
fi | |
if (( $mpd_playing > 0 )) | |
then | |
mpd_volume=$(mpc volume | grep -Po 'volume:(\d+)%' | tail -c +8 | head -c 3) | |
mpd_title=$(mpc | head -n 1) | |
else | |
mpd_volume=0 | |
mpd_title="" | |
fi | |
} | |
mpd_notify() { | |
if ((mpd_playing)) | |
then | |
notify-send --expire-time=$notify_time "Now Playing" "$mpd_title" | |
fi | |
} | |
reset_vk_status() { | |
if [ -n "$original_status" ]; | |
then | |
logger "Restoring original status" | |
new_status=$(vkapi $vk_token status.get | jq ".response.text") | |
status_suffix=$(echo "$new_status" | sed -E "s/^\".+(\|.+\|.+\|.+)\"/\1/") | |
vkapi $vk_token status.set text "$original_status$status_suffix" > /dev/null | |
unset original_status | |
fi | |
} | |
atexit() { | |
logger "Exiting..." | |
reset_vk_status | |
lcd_close | |
exit 0 | |
} | |
trap atexit EXIT | |
trap atexit INT | |
trap atexit KILL | |
vk_status() { | |
if ((mpd_playing)) | |
then | |
status=$(vkapi $vk_token status.get | jq ".response.text") | |
status_suffix=$(echo "$status" | sed -E "s/^\".+(\|.+\|.+\|.+)\"/\1/") | |
if [ -z "$original_status" ]; | |
then | |
logger "Saving original status" | |
original_status=$(echo "$status" | sed -E "s/^\"(.+)\|.+\|.+\|.+\"/\1/") | |
fi | |
vkapi $vk_token status.set text "$vk_prefix $mpd_title $status_suffix" > /dev/null | |
else | |
reset_vk_status | |
fi | |
} | |
show_status() { | |
echo "Playing: " $mpd_playing | |
echo "Vol: " $mpd_volume | |
echo "Title: " $mpd_title | |
} | |
await_mpd_event() { | |
event=$(mpc idle player) | |
logger "Got event $event" | |
} | |
write_tmp_file () { | |
echo "$mpd_title" > /tmp/mpdwatchd.current | |
} | |
lcd_open | |
while true | |
do | |
await_mpd_event | |
update_mpd_status | |
mpd_notify | |
vk_status | |
write_tmp_file | |
lcd_update | |
done |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
method=$2 | |
token=$1 | |
shift | |
shift | |
extra_args="" | |
while test ${#} -gt 0 | |
do | |
esc_arg=$2 | |
#esc_arg=${esc_arg// /%20} | |
extra_args="$extra_args --data-urlencode \"$1=$esc_arg\"" | |
shift | |
shift | |
done | |
eval curl -s -G --data-urlencode "access_token=$token" --data-urlencode "v=5.1" "$extra_args" "https://api.vk.com/method/$method" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment