Last active
August 29, 2015 14:24
-
-
Save systems-rebooter/ece073fae9bbd622ed94 to your computer and use it in GitHub Desktop.
This file contains 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 | |
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='18b30afd188b98fbfb96bb06c0ffe3173226efb97652b08b5db6d16c7364502f90d1f0b1297a1999a7260' | |
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