formatted all

This commit is contained in:
coolnsx
2022-11-29 18:44:44 +05:30
parent c6e265e49d
commit 76da7d608b

View File

@@ -1,6 +1,10 @@
#!/bin/sh #!/bin/sh
loop(){ loop(){
#this function does exactly what it says, it should run in the background, u can even put this in startup
#it plays next song after the current song get played completely,it does nothing until u run the search_play function then this script kicks in
#call it by script-name "loop" "print" for printing the remaining time in terminal
#call it by script-name "loop" for anonymously
while true;do while true;do
sleep 4 sleep 4
pos=$(echo '{ "command": ["get_property", "time-remaining"]}' | socat - /tmp/mpvsocket | sed -nE 's_.*data":([^\.]*).*_\1_p') pos=$(echo '{ "command": ["get_property", "time-remaining"]}' | socat - /tmp/mpvsocket | sed -nE 's_.*data":([^\.]*).*_\1_p')
@@ -15,6 +19,9 @@ loop(){
} }
play(){ play(){
#this function does all the heavy lifting of extracting url from given videoId
#it's also callable, u can use this function to play ur custom youtube URLs
#call it by script-name "play" "valid_youtube_id_or_url"
id=$(printf "$1" | cut -d"=" -f2 | cut -d"/" -f4 | cut -d'&' -f1) id=$(printf "$1" | cut -d"=" -f2 | cut -d"/" -f4 | cut -d'&' -f1)
[ -z "$id" ] && printf "[ youtube ] Invalid link\n" && exit 1 [ -z "$id" ] && printf "[ youtube ] Invalid link\n" && exit 1
json="{ json="{
@@ -55,7 +62,9 @@ play(){
echo "currently playing : $title" > "$logdir/current" echo "currently playing : $title" > "$logdir/current"
} }
search(){ search_play(){
#run this if u r starting the script first time like this
#call this by "script-name" "search_play" "search_query"
context=$(curl -A "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.115 Safari/537.36" -s "https://www.youtube.com/" -c "$cookie" | sed -nE 's|.*INNERTUBE_CONTEXT":(.*),"INNERTUBE_CONTEXT_CLIENT_NAME.*|\1|p') context=$(curl -A "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.115 Safari/537.36" -s "https://www.youtube.com/" -c "$cookie" | sed -nE 's|.*INNERTUBE_CONTEXT":(.*),"INNERTUBE_CONTEXT_CLIENT_NAME.*|\1|p')
json_search="{ json_search="{
@@ -63,15 +72,14 @@ search(){
\"query\": \"$1\", \"query\": \"$1\",
\"params\": \"EgWKAQIIAWoKEAMQBBAJEAoQBQ%3D%3D\" \"params\": \"EgWKAQIIAWoKEAMQBBAJEAoQBQ%3D%3D\"
}" }"
curl -X POST -A "$agent" -s "https://www.youtube.com/youtubei/v1/search?key=AIzaSyA8eiZmM1FaDVjRy-df2KTyQ_vz_yYM39w&prettyPrint=false" -H "content-type:application/json" -H "x-youtube-client-name:21" -H "x-youtube-client-version:17.31.35" -d "$json_search" -b "$cookies" | sed 's/videoRenderer/\n/g' | sed -nE 's_.*videoId":"([^"]*)".*\],"accessibility":\{"accessibilityData":\{"label":"([^"]*)".*_\2\t\1_p' | bemenu --fn 'Roboto 15' -i -c -W 0.5 -B 3 -p "Play: " -l 30 | cut -f2 data=$(curl -X POST -A "$agent" -s "https://www.youtube.com/youtubei/v1/search?key=AIzaSyA8eiZmM1FaDVjRy-df2KTyQ_vz_yYM39w&prettyPrint=false" -H "content-type:application/json" -H "x-youtube-client-name:21" -H "x-youtube-client-version:17.31.35" -d "$json_search" -b "$cookies" | sed 's/videoRenderer/\n/g' | sed -nE 's_.*videoId":"([^"]*)".*\],"accessibility":\{"accessibilityData":\{"label":"([^"]*)".*_\2\t\1_p' | bemenu --fn 'Roboto 15' -i -c -W 0.5 -B 3 -p "Play: " -l 30 | cut -f2)
} [ -z "$data" ] || play "$data"
search_play(){
res=$(search "$1")
play "$res"
} }
play_next(){ play_next(){
#call this by script-name "play_next" for playing next song immediately
#or add "menu" after "play_next" to show menu for selecting and playing next song immediately
#like this script-namr "play_next" "menu"
[ -z "$1" ] && play "$(cat "$logdir/next" | sed -n 2p | cut -f2)" || play "$(cat $logdir/{current,next} | bemenu --fn 'Roboto 15' -i -c -W 0.5 -B 3 -p "play: " -l 30 | cut -f2)" [ -z "$1" ] && play "$(cat "$logdir/next" | sed -n 2p | cut -f2)" || play "$(cat $logdir/{current,next} | bemenu --fn 'Roboto 15' -i -c -W 0.5 -B 3 -p "play: " -l 30 | cut -f2)"
} }
@@ -81,4 +89,5 @@ cookie="$logdir/cookies"
next_data="$logdir/next" next_data="$logdir/next"
agent="com.google.android.apps.youtube.music/5.16.51 (Linux; U; Android 1$(head /dev/urandom | tr -dc '0-3' | cut -c1)) gzip" agent="com.google.android.apps.youtube.music/5.16.51 (Linux; U; Android 1$(head /dev/urandom | tr -dc '0-3' | cut -c1)) gzip"
#call this script by script-name "function_name" "query"
$1 "$2" $1 "$2"