-
Notifications
You must be signed in to change notification settings - Fork 1
/
shazam-notif
executable file
·58 lines (47 loc) · 1.75 KB
/
shazam-notif
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
#!/usr/bin/env bash
# Small script to run shazam, with notifications.
# Requires shazam, jq, notify-send, dunstify(optional).
set -e
err_msg_exit() {
# Print an error to stderr and exit.
printf "%s\n" "$*" >&2
exit 1
}
type shazam >/dev/null || err_msg_exit "'shazam' not found."
type jq >/dev/null || err_msg_exit "'jq' not found."
COVER_ART_FILE="/tmp/shazam/coverart.jpg"
# set the source to the monitor of the current output sink
SOURCE="$(ffmpeg -sinks pulse 2>/dev/null | grep '\*' | cut -d' ' -f2).monitor"
if ! ffmpeg -sources pulse 2>/dev/null | grep -q "$SOURCE"; then
# exit if doesn't exist
echo "Source monitor \"$SOURCE\" not found." >&2
exit 1
fi
notify-send -i "audio-input-microphone" -t 5000 "Shazam" "Recording audio for 5s."
RESPONSE="$(shazam -s "$SOURCE")"
N_MATCHES="$(echo "$RESPONSE" | jq -r '.matches | length')"
if [ "$N_MATCHES" = 0 ]; then
notify-send -i "music" "Shazam" "Failed to identify song."
exit 2
fi
ARTIST="$(echo "$RESPONSE" | jq -r ".track.subtitle")"
TITLE="$(echo "$RESPONSE" | jq -r ".track.title")"
COVER_ART_URL="$(echo "$RESPONSE" | jq -r ".track.images.coverart")"
curl --silent "$COVER_ART_URL" -o "$COVER_ART_FILE"
if type dunstify >/dev/null; then
case "$(dunstify -i "$COVER_ART_FILE" \
--action="apple music,apple music" \
--action="youtube,youtube" \
"$ARTIST" "$TITLE")" in
"apple music")
AM_LINK="$(echo "$RESPONSE" | jq -r ".track.hub.options[0].actions[0].uri")"]
xdg-open "$AM_LINK"
;;
"youtube")
YT_LINK="$(echo "$RESPONSE" | jq -r '.track.sections | map(select(.type == "VIDEO"))[0].youtubeurl.actions[0].uri')"
xdg-open "$YT_LINK"
;;
esac
else
notify-send -i "$COVER_ART_FILE" "$ARTIST" "$TITLE"
fi