-
Notifications
You must be signed in to change notification settings - Fork 0
/
fb2k-notif.sh
executable file
·229 lines (191 loc) · 7 KB
/
fb2k-notif.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
#!/bin/bash
# Author : Anonymous
# Version : 1.0
# License : n/a
# Date : 2010 04/15
# Requires : foobar2000, foo_np_simple, notify-send
# Script Name : fb2000_notify.sh
# Description : Script to display Now Playing' info via 'foo_np_simple'
# component and 'notify-send'.
#
# Notes : n/a
#
# Acknowledge : thanks to original authors
# Resources : n/a
#==============
export DISPLAY=:0
export DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/1000/bus
# -- User Constants - User can specify these variables as necessary -----------
# Location (full path) of 'foo_np_simple' file:
NPFILE='/home/julian/foobar2000/now_playing/np.txt'
# -
# Directory where you will store cover art thumbnail image used by notify-send.
# Note: using the same directory where the 'foo_np_simple' file is kept might
# be a good place for the thunmail.
IMGDIR='/home/julian/foobar2000/now_playing/'
#-
# Full path to alternate image (if no cover art exists).
IMGALT='/home/julian/foobar2000/default.jpg'
#-
# Specify replacement string for '&' character in message body. See function,
# 'f_fix_notify_bug', notes below.
AMPALT='And'
#-
# Strings to add to image thumbnail file. For example, if original image file
# name 'cover' (excluding extension), then new file will become
# 'cover_thumbnail', assumimg default values are used.
SEP_NAME='_'
IMGFILE_XTRA_NAME='thumbnail'
#-
# Size in pixels of thumbnail image used in 'notify-send' pop-up. This values
# is the height dimension and will automatically generate the width to maintain
# aspect ratio.
THUMB_SIZE=96
#-
# User name for controlling xhost, etc.
NOTIFUNAME='user_name'
# Also see the 'f_convert_image' function to modify image boder, filter,
# unsharp mask, etc. Make a backup copy and play around.
# -- Other Constants - DO NOT CHANGE ------------------------------------------
# Separator between file name and extension
SEP_EXT='.'
# Path separator
SEP_PATH='/'
# Extension for thumbnail image. This must be 'png' as it supports
# transparency necessary for proper display of image in notify-send pop-up.
IMGFILE_CONVERT_EXT='png'
# -- Functions ----------------------------------------------------------------
function f_get_npinfo {
# Get 'Now Playing' info from user-specified file and set some variables
# required by the script. The variables are assigned values based on
# specific lines from the file:
# Line 1 - used for 'notify-send' message summary
# Line 2 to Line n-2 - used for 'notify-send' message body
# Line n-1 - used as the directory to search for 'Now Playing' cover art
count=0
while read -r line; do
line_array[$count]="$line\n"
let count=$count+1
done <"$NPFILE"
msg_line_summary=0
let msg_lines_body=${#line_array[@]}-7
let msg_line_dir=${#line_array[@]}-1
msg_summary=$(echo -e "${line_array[$msg_line_summary]}")
msg_body=$(echo -e "${line_array[@]:2:$msg_lines_body}")
track_path=$(echo -n "${line_array[$msg_line_dir]}")
}
function f_fix_notify_bug {
# Notify send fails to print the message body if it contains an ampersand
# (&). It is possible there are other characters that introduce odd
# behavior, but no others have been noted yet. Also, the same behavior did
# not seem to apply to the message summary.
# Remove '&'s from message body and replace with user-specified
# alternative.
msg_body=$(echo "$msg_body" | sed "s/&/${AMPALT}/g")
}
function f_get_image {
# Search directory of currently playing audio file for image file. Image
# file in this directory is assumed to be the cover art image. The image
# will be used to create the thumbnail for the 'notify-send' pop-up.
IFS=$''
cover_dir=$(echo $track_path | sed -e 's/[a-zA-Z]://' -e 's/\\n//g' -e 's/\\/\//g')
cover_dir=$(echo $cover_dir | sed -E 's/(.*)\/(.*)/\/home\/julian\1/g')
for item in "${cover_dir}/"*; do
if [ -f "$item" ]; then
mime=$(file -ib "$item")
result=$(echo $mime | grep 'image')
if [ $result ]; then
echo $item
imgfile="$item"
break
else
track_path=$(echo $track_path | sed -e 's/[a-zA-Z]://' -e 's/\\n//g' -e 's/\\/\//g' | sed -E 's/(.*)/\/home\/julian\1/g')
$(ffmpeg -y -i "$track_path" -map 0:artwork? -c copy "/tmp/artwork.jpeg" >/dev/null 2>&1)
imgfile="/tmp/artwork.jpeg"
break
fi
fi
done
unset IFS
}
function f_convert_image {
# Convert cover art:
# Step 1: create thumbnail
# Step 2: apply drop shadow
if [ -n "$imgfile" ]; then
# Resize image
magick "$imgfile" \
-depth 24 \
-background transparent \
-quality 100 \
-filter Lanczos \
-unsharp 0x1.5+1+0 \
-resize ${THUMB_SIZE} \
"$imgfile_resized" #-thumbnail x${THUMB_SIZE} \
#-bordercolor '#000000' \
#-border 2x2 \
# Create drop shadow
#magick convert "$imgfile_resized" \
# \( +clone -background black -shadow 60x5+10+10 \) \
# +swap \
# -background transparent \
# -layers merge \
# +repage \
# "$imgfile_resized"
fi
}
function f_notify {
# Create the 'notify-send' pop-up.
# Set some vars
notifuname="$NOTIFUNAME"
namecheck=$(who | grep -E "\b${notifuname}\b")
#notificon="user-info"
notificon="$imgfile_resized"
# # Set X display (needed when terminal app launches GUI app)
# export DISPLAY=:0.0
# # Test whether specified user is logged in
# if [ -n "$namecheck" ]; then
# # Test whether X is running
# isx=$(xdpyinfo | head -20 | grep "X.Org")
# # Test whether screen has been blanked by xscreensaver
# if [ -n "$isx" ]; then
# # Add user to xhost (will be removed before script exits)
# # otherwise xscreensaver-command will not work properly.
# # [url=http://www.leidinger.net/X/xhost.html]http://www.leidinger.net/X/xhost.html[/url]
# xhost +nis:${notifuname}@ >/dev/null
# is_scrblank=$(xscreensaver-command -time 2>/dev/null | grep "screen non-blanked")
# # Above command issues an error message ('no saver status on
# # root window.') if screensaver has not been invoked since daemon
# # was started. Therefore, if error status (1), it means the screen
# # is not blanked and notifier should be issued to desktop.
# is_err=$?
# # Debug
# # echo $is_scrblank
# # echo $is_err
# if [ -n "$is_scrblank" ] || [ !"$is_err" ]; then
# Send desktop notification
notify-send \
--icon="$notificon" \
--expire-time=6000 \
--urgency=normal \
-r 3662 \
"${msg_summary}" "${msg_body}"
# fi
# fi
# fi
# Remove user from xhost
# xhost -nis:${notifuname}@ >/dev/null
}
# -- Main ---------------------------------------------------------------------
# Call functions and assign values to additional variables.
f_get_npinfo
f_fix_notify_bug
f_get_image
imgfile_base=$(basename "$imgfile")
#imgfile_base_noext="${imgfile_base%.*}"
#imgfile_thumbnail="${imgfile_base_noext}${SEP_NAME}${IMGFILE_XTRA_NAME}${SEP_EXT}${IMGFILE_CONVERT_EXT}"
#imgfile_resized="${IMGDIR}${SEP_PATH}${imgfile_thumbnail}"
imgfile_resized="${IMGDIR}${SEP_PATH}now_playing.png"
f_convert_image
f_notify
exit