Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
VMusic - An advanced VK music finder/player in bash
#!/bin/bash
PLAYER="mpg123 --utf8 --long-tag"
VMPID="/tmp/vmusic_mainpid"
PLAYERPID="/tmp/vmusic_playerpid"
VMTRACK="/tmp/vmusic_trackurl"
VMINFO="/tmp/vmusic_trackinfo"
SELFNAME="$0"
fetch() {
ACCESS_TOKEN='8426e7c42b7136646ac0c655ea0c09ad97bd80919a04a28d2e5a7860ab10862c3ac4805f27a75e096a589'
ENDPOINT="https://api.vk.com/method/audio.search"
URLLIST=$(curl -s -G --data-urlencode "q=${@// /+}" -d "access_token=${ACCESS_TOKEN}&count=200" $ENDPOINT|grep -Po '"url":".+?"'|grep -Poi '(?<=:")http.+(?=\?)')
for url in $URLLIST; do echo $(eval "echo $url");done
}
playdaemon() {
for track in $1; do
echo $track>$VMTRACK
$PLAYER $track > $VMINFO 2>&1 &
echo $!>$PLAYERPID
wait $!
done </dev/null >/dev/null 2>&1
$SELFNAME stop >/dev/null 2>&1
}
case "$1" in
play)
[ -e "$VMPID" ] && $SELFNAME stop
echo "Playing the tracks for \"$2\" in VMusic..."
TRACKS=$(fetch "$2")
playdaemon "$TRACKS" & </dev/null >/dev/null 2>&1
disown -a
echo $!>$VMPID
;;
skip)
[ ! -e "$VMPID" ] && echo "VMusic is already stopped" && exit 1
echo "Skipping current track..."
[ -e "$PLAYERPID" ] && kill -9 $(<$PLAYERPID) >/dev/null 2>&1
sleep 0.5 && [ -n "$($SELFNAME info)" ] && echo "Playing $($SELFNAME info)"
;;
stop)
[ -e "$VMPID" ] && kill -9 $(<$VMPID)
[ -e "$PLAYERPID" ] && kill -9 $(<$PLAYERPID)
rm -f $VMPID $PLAYERPID $VMTRACK $VMINFO
echo "Stopped VMusic"
;;
info)
[ ! -e "$VMINFO" ] && echo "" && exit 1
RAWINFO=$(<$VMINFO)
TITLE=$(echo $(echo "$RAWINFO"|grep Title|grep -Po '(?<=:\s).*'))
ARTIST=$(echo $(echo "$RAWINFO"|grep Artist|grep -Po '(?<=:\s).*'))
[ -z "$ARTIST" ] && [ -z "$TITLE" ] && TRACKINFO="" || TRACKINFO="$ARTIST - $TITLE"
echo ${TRACKINFO}
;;
dl)
[ -z $2 ] && DLDIR=~ || DLDIR="$2"
echo "Downloading current track to ${DLDIR}..."
cd $DLDIR
CURTRACK="$($0 info)"
[ -z "$CURTRACK" ] && curl -s -O $(<$VMTRACK) || curl -s -o "${CURTRACK}.mp3" $(<$VMTRACK)
;;
*)
echo "Usage: $0 {play|skip|stop|info|dl}"
exit 1
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment