mirror of
https://github.com/CoolnsX/repos_scripts.git
synced 2025-12-20 07:15:20 +05:30
258 lines
10 KiB
Bash
Executable File
258 lines
10 KiB
Bash
Executable File
#!/bin/sh
|
|
|
|
help() {
|
|
case $1 in
|
|
search*)
|
|
name="search_play"
|
|
args='"search_query"'
|
|
description="this function first searches the $args, then opens the menu(fzf,[d|be]menu,[t|r|w]ofi) with the results,then calls the play function to play the music"
|
|
;;
|
|
play)
|
|
name="play"
|
|
args='"valid_youtube_url_or_id"'
|
|
description="immediately plays the $args in mpv(audio only)"
|
|
;;
|
|
loop)
|
|
name="loop"
|
|
args='"print"'
|
|
description="this function runs in a loop to play the next music if the current music is successfully finished
|
|
this function should be put as background process or put it in startup
|
|
passing print argument will show the progress to stdout"
|
|
;;
|
|
play*)
|
|
name="play_next"
|
|
args='"menu"'
|
|
description="immediately plays the next song stored in $logdir/next file in mpv if argument is empty
|
|
passing $args argument will show the $args(fzf,[d|be]menu,[t|r|w]ofi) for selecting what should be played"
|
|
;;
|
|
*)
|
|
name="[ <function_name> ]"
|
|
args="[ <function_arg> ]"
|
|
description="
|
|
<function_name> <function_arg> <function_description>
|
|
|
|
search_play search_query searches first then plays the music in mpv
|
|
play youtube_url plays the music in mpv
|
|
play_next menu(optional) plays the next music in $logdir/next file
|
|
loop print(optional) plays the next music after the current is finished (run it as background process)
|
|
|
|
tip:
|
|
type ${0##*/} help <function_name> to get individual help
|
|
"
|
|
;;
|
|
esac
|
|
while read -r line; do
|
|
printf "%s\n" "$line"
|
|
done <<-EOF
|
|
|
|
Usage :
|
|
${0##*/} $name $args
|
|
|
|
Description :
|
|
$description
|
|
EOF
|
|
exit 0
|
|
}
|
|
|
|
cleanup_shit(){
|
|
rm -rdf "$logdir"
|
|
exit 0
|
|
}
|
|
|
|
get_cookies() {
|
|
for i in $(sqlite3 "$HOME/.config/google-chrome/Default/Cookies" "SELECT name,REPLACE(base64(SUBSTR(encrypted_value,4)),CHAR(10),'') FROM cookies WHERE host_key='.youtube.com';")
|
|
do
|
|
printf "%s=%s; " "$(printf '%s' "$i" | cut -d'|' -f1)" "$(printf '%s' "$i" | cut -d'|' -f2 | base64 -d | openssl enc -d -aes-128-cbc -K fd621fe5a2b402539dfa147ca9272778 -iv 20202020202020202020202020202020)"
|
|
done > "$cookie"
|
|
}
|
|
|
|
get_data() {
|
|
grep -q "SAPISID" "$logdir/cookies" 2>/dev/null && sapisid_header="Authorization: SAPISIDHASH $(printf '%s_%s' "$(date +%s)" "$(printf '%s %s %s' "$(date +%s)" "$(sed -nE 's|.*SAPISID=([^;]*);.*|\1|p' "$logdir/cookies")" "$base_url" | sha1sum | cut -d' ' -f1)")" || sapisid_header=""
|
|
curl -X POST -A "${3:-$agent}" -s "$base_url/youtubei/v1/$1?key=${4:-AIzaSyC9XL3ZjWddXya6X74dJoCTL-WEYFDNX30}&prettyPrint=false" -H "content-type:application/json" -d "$2" -b "$(cat "$cookie")" -e "$base_url" -H "$sapisid_header"
|
|
}
|
|
|
|
get_music_list(){
|
|
json_next="{
|
|
\"enablePersistentPlaylistPanel\": true,
|
|
\"tunerSettingValue\": \"AUTOMIX_SETTING_NORMAL\",
|
|
\"playlistId\": \"RDAMVM$(cat "$logdir/start")\",
|
|
\"index\": $(cat "$logdir/counter"),
|
|
\"params\": \"wAEB\",
|
|
$([ -e "$logdir/continue_token" ] && cat "$logdir/continue_token")
|
|
\"isAudioOnly\": true,
|
|
\"context\": $(cat "$logdir/context")
|
|
}"
|
|
get_data "next" "$json_next" | tee "$logdir/extract_continuation" | sed 's/playlistPanelVideoRenderer/\n/g' | sed -nE 's_.*text":"(.*)"}.*longBylineText":\{"runs":\[\{"text":"([^"]*)","navigationEndpoint.*videoId":"([^"]*)".*_\1 - \2\t\3_p' | sed '$d' >> "$logdir/next"
|
|
sed -nE 's|.*nextRadioContinuationData":\{([^,]*).*|\1,|p' "$logdir/extract_continuation" > "$logdir/continue_token"
|
|
}
|
|
|
|
get_song_lyrics() {
|
|
json_next="{
|
|
\"enablePersistentPlaylistPanel\": true,
|
|
\"tunerSettingValue\": \"AUTOMIX_SETTING_NORMAL\",
|
|
\"playlistId\": \"RDAMVM$(cat "$logdir/start")\",
|
|
\"index\": $(cat "$logdir/counter"),
|
|
\"videoId\": \"$1\",
|
|
\"isAudioOnly\": true,
|
|
\"context\": $(cat "$logdir/context")
|
|
}"
|
|
|
|
browseId=$(get_data "next" "$json_next" | sed -nE 's|.*"browseId":"(MPLYt[^"]*)".*TRACK_LYRICS.*|\1|p')
|
|
if [ -n "$browseId" ]; then
|
|
json_lyrics="{
|
|
\"context\": {
|
|
\"client\": {
|
|
\"clientName\": \"ANDROID_MUSIC\",
|
|
\"clientVersion\": \"$andy_yt_ver\",
|
|
\"androidSdkVersion\": $(( random_no + extra_up + 29 )),
|
|
\"userAgent\": \"$droid_agent\",
|
|
\"hl\": \"en\",
|
|
\"timeZone\": \"UTC\",
|
|
\"utcOffsetMinutes\": 0
|
|
}
|
|
},
|
|
\"browseId\": \"$browseId\"
|
|
}"
|
|
get_data "browse" "$json_lyrics" "$droid_agent" "AIzaSyAOghZGza2MQSZkY_zfZ370N-PUdXEo8AI" | sed 's/metadata"/\n/g' | sed -nE 's|.*lyricLine":"([^"]*)","cueRange".*"endTimeMilliseconds":"([^"]*)".*|\2\t\1|p' > "$logdir/lyrics"
|
|
fi
|
|
}
|
|
|
|
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
|
|
trap cleanup_shit INT HUP TERM
|
|
while :; do
|
|
sleep 4
|
|
pos=$(echo '{ "command": ["get_property", "time-remaining"]}' | socat - /tmp/mpvsocket | sed -nE 's_.*data":([^\.]*).*_\1_p')
|
|
while [ -n "$pos" ]; do
|
|
pos=$(echo '{ "command": ["get_property", "time-remaining"]}' | socat - /tmp/mpvsocket | sed -nE 's_.*data":([^\.]*).*_\1_p')
|
|
[ -z "$1" ] || printf "\033[2K\rnext music in $pos second(s)%s" "$(echo '{ "command": ["get_property", "pause"]}' | socat - /tmp/mpvsocket | sed -nE 's_.*data":([^,]*).*_\1_p' | sh && printf " (paused)")"
|
|
[ -z "$pos" ] && printf "\033[2K\r\n"
|
|
sleep 2
|
|
done
|
|
pos=$(echo '{ "command": ["get_property", "time-remaining"]}' | socat - /tmp/mpvsocket | sed -nE 's_.*data":([^\.]*).*_\1_p')
|
|
pgrep -f "mpvsocket" >/dev/null || cleanup_shit
|
|
#shellcheck disable=SC2030,SC2031
|
|
[ -z "$pos" ] && i=$(cat "$logdir/counter") && : $((i+=1)) && pgrep -f "mpvsocket" >/dev/null && [ -n "$(cat "$logdir/next")" ] && play "$(sed -n "$((i+=1))p" "$logdir/next")" "$1" && printf '%s' "$i" > "$logdir/counter"
|
|
tail -1 "$logdir/next" | grep -q "$(cut -d'>' -f2 < "$logdir/current")" && get_music_list
|
|
done
|
|
}
|
|
|
|
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"
|
|
title=$(printf "%s" "$1" | cut -f1)
|
|
id=$(printf "%s" "$1" | cut -f2 | cut -d"=" -f2 | cut -d"/" -f4 | cut -d'&' -f1)
|
|
[ -z "$id" ] && printf "[ youtube ] Invalid link\n" && exit 1
|
|
|
|
#get song's audio url
|
|
random_no=$(head /dev/urandom | tr -dc '0-4' | cut -c1)
|
|
andy_yt_ver="6.16.52"
|
|
extra_up=$((random_no >= 2))
|
|
droid_agent="com.google.android.apps.youtube.music/$andy_yt_ver (Linux; U; Android 1$random_no) gzip"
|
|
json="{
|
|
\"context\": {
|
|
\"client\": {
|
|
\"clientName\": \"ANDROID_MUSIC\",
|
|
\"clientVersion\": \"$andy_yt_ver\",
|
|
\"androidSdkVersion\": $(( random_no + extra_up + 29 )),
|
|
\"userAgent\": \"$droid_agent\",
|
|
\"hl\": \"en\",
|
|
\"timeZone\": \"UTC\",
|
|
\"utcOffsetMinutes\": 0
|
|
}
|
|
},
|
|
\"videoId\": \"$id\",
|
|
\"playbackContext\": {
|
|
\"contentPlaybackContext\": {
|
|
\"html5Preference\": \"HTML5_PREF_WANTS\"
|
|
}
|
|
},
|
|
\"contentCheckOk\": true,
|
|
\"racyCheckOk\": true
|
|
}"
|
|
|
|
audio_url=$(get_data "player" "$json" "$droid_agent" "AIzaSyAOghZGza2MQSZkY_zfZ370N-PUdXEo8AI" | sed -nE 's_.*itag":251,"url":"([^"]*)".*_\1_p')
|
|
[ -z "$audio_url" ] && return 0
|
|
if [ -n "$2" ]; then
|
|
printf "Name >> %s\n" "$title"
|
|
printf "videoID >> %s\n" "$id"
|
|
printf "Audio URL >> %s\n" "$audio_url"
|
|
fi
|
|
|
|
curl -s "https://i.ytimg.com/vi/$id/hqdefault.jpg" -o - | convert - -crop 270x270+105+45 "$logdir/default.jpg" && notify-send -h "string:x-canonical-private-synchronous:${0##*/}" -i "$logdir/default.jpg" "Now Playing" "$title" -t 5000
|
|
pgrep -f "mpvsocket" >/dev/null || (setsid -f mpv --really-quiet --input-ipc-server=/tmp/mpvsocket --idle --quiet >/dev/null && sleep 1)
|
|
echo "{ \"command\": [\"loadfile\", \"$audio_url\", \"replace\"] }" | socat - /tmp/mpvsocket
|
|
printf "currently playing : %s >%s\n" "$title" "$id" >"$logdir/current"
|
|
|
|
#next songs data
|
|
[ -n "$3" ] && get_music_list &
|
|
get_song_lyrics "$id" &
|
|
pgrep -f "yt-music loop" >/dev/null || setsid -f "$0" loop
|
|
}
|
|
|
|
search_play() {
|
|
#run this if u r starting the script first time like this
|
|
#call this by "script-name" "search_play" "search_query"
|
|
[ -z "$1" ] && query=$(: | menu "Yt-music [Search]:") || query="$1"
|
|
[ -z "$query" ] && notify-send "Err.. Search query empty" -u critical -h "string:x-canonical-private-synchronous:${0##*/}" && exit 1
|
|
|
|
#storing context
|
|
printf '{"client":{"clientName":"WEB_REMIX","clientVersion":"1.20230731.00.00"}}' >"$logdir/context"
|
|
|
|
#json for song search
|
|
json_search="{
|
|
\"context\" : $(cat "$logdir/context"),
|
|
\"query\": \"$query\",
|
|
\"params\": \"EgWKAQIIAWoKEAMQBBAJEAoQBQ%3D%3D\"
|
|
}"
|
|
|
|
#extracting your cookies so that the song list are according to your taste
|
|
get_cookies
|
|
printf "0" > "$logdir/counter"
|
|
printf '' > "$logdir/next"
|
|
rm -f "$logdir/continue_token"
|
|
res=$(get_data "search" "$json_search" "$agent" | sed 's/watchEndpoint"/\n/g' | sed -nE 's_.*videoId":"([^"]*)",.*label":"Play ([^"]*)".*_\2\t\1_p' | menu "Yt-music [Play]:")
|
|
printf "%s\n" "$res" >> "$logdir/next"
|
|
printf "%s" "$res" | cut -f2 > "$logdir/start"
|
|
[ -z "$res" ] || play "$res" "verbose" "1"
|
|
}
|
|
|
|
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-name "play_next" "menu"
|
|
pgrep -f "mpvsocket" || return 0
|
|
i=$(cat "$logdir/counter")
|
|
if [ -z "$1" ]; then
|
|
: $((i+=1))
|
|
#shellcheck disable=SC2030
|
|
play "$(sed -n "$((i+=1))p" "$logdir/next")" "$1"
|
|
else
|
|
notify-send -h "string:x-canonical-private-synchronous:${0##*/}" -i "$logdir/default.jpg" "$(cut -d">" -f1 "$logdir/current" | tr ':' '\n')"
|
|
#shellcheck disable=SC2031
|
|
next=$(nl -n'ln' -v0 "$logdir/next" | sed "s/^$i /& /" | menu "YT-music [play-next]: ")
|
|
[ -z "$next" ] && return 0
|
|
i=$(printf '%s' "$next" | sed 's///g' | cut -f1 | tr -d ' ')
|
|
play "$(printf '%s' "$next" | cut -f2-)" "verbose"
|
|
fi
|
|
printf '%s' "$i" > "$logdir/counter"
|
|
}
|
|
|
|
menu() {
|
|
bemenu --fn 'IBM Plex Sans 15' -i -c -W 0.5 -B 3 -p "$1" -l 26
|
|
}
|
|
|
|
logdir="/tmp/yt-music"
|
|
base_url="https://music.youtube.com"
|
|
[ -d "$logdir" ] || mkdir "$logdir"
|
|
cookie="$logdir/cookies"
|
|
agent="Mozilla/5.0 (X11; Linux x86_64; rv:109.0) Gecko/20100101 Firefox/116.0"
|
|
|
|
#call this script by script-name "function_name" "query"
|
|
[ -z "$1" ] && help "$@"
|
|
$1 "$2" "$3"
|