Feat: No more Discord Rich presence in yt-music, bois

This commit is contained in:
coolnsx
2024-07-22 13:49:49 +05:30
parent f4ee1b8859
commit eb327b465c
5 changed files with 30 additions and 82 deletions

View File

@@ -12,7 +12,7 @@ help() {
play)
name="play"
args='"valid_youtube_url_or_id"'
description="immediately plays the $args in mpv(audio only)"
description="immediately plays the $args in mpv(audio only), for only id prefix it with ID:"
;;
loop)
name="loop"
@@ -57,8 +57,6 @@ help() {
}
cleanup_shit() {
pkill -f "nc -U $discord_ipc" >/dev/null
pkill -f "tail -f $presence" >/dev/null
rm -rdf "$logdir"
exit 0
}
@@ -72,12 +70,14 @@ get_cookies() {
return 0
fi
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" "$i" | grep -qE "VISITOR_INFO1_LIVE|VISITOR_PRIVACY_METADATA" && continue
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=""
lol=$(date +%s)
grep -q "SAPISID" "$logdir/cookies" 2>/dev/null && sapisid_hash=$(printf '%s_%s' "$lol" "$(printf '%s %s %s' "$lol" "$(sed -nE 's|.*SAPISID=([^;]*);.*|\1|p' "$logdir/cookies")" "$base_url" | sha1sum | cut -d' ' -f1)") && sapisid_header="Authorization: SAPISIDHASH $sapisid_hash" || 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"
}
@@ -151,44 +151,6 @@ loop() {
cleanup_shit
}
#discord rich presence written in a nut-shell code
set_activity() {
#length of payload
len=${#1}
#outputs opcode in little endian encoding
printf "\\001\\000\\000\\000"
#loop for length to encode in little endian encoding
for i in 0 8 16 24; do
len=$((len >> i))
#shellcheck disable=SC2059
printf "\\$(printf "%03o" "$len")"
done
#print the payload that need to be sent
printf "%s" "$1"
}
start_rich_presence() {
rm -f "$handshook" >/dev/null
tail -f "$presence" | nc -U "$discord_ipc" >"/tmp/ipclog"
}
update_rich_presence() {
title=$1
id=$2
#dur=$3
#start=$(date +%s)
#payload='{"cmd": "SET_ACTIVITY", "args": {"activity": {"details": "'$(printf "%s" "$title" | sed 's|[^-]*$||;s|-$||;s| $||;s|^ ||')'", "state": "'$(printf "%s" "$title" | sed 's_.* - __;s| $||')'", "timestamps": {"start": '"$start"',"end": '"$((start + dur))"'}, "assets": {"large_image": "https://i.ytimg.com/vi/'$id'/mqdefault.jpg","large_text":"Checkmate Premium Users"},"buttons": [{"label":"Listen Song", "url": "https://music.youtube.com/watch?v='$id'"}]},"pid":786}, "nonce": "1"}' #pid needs to be set and constant so discord knows which application to update
payload='{"cmd": "SET_ACTIVITY", "args": {"activity": {"details": "'$(printf "%s" "$title" | sed 's|[^-]*$||;s|-$||;s| $||;s|^ ||')'", "state": "'$(printf "%s" "$title" | sed 's_.* - __;s| $||')'", "assets": {"large_image": "https://i.ytimg.com/vi/'$id'/mqdefault.jpg","large_text":"Checkmate Premium Users","small_image":"https://upload.wikimedia.org/wikipedia/commons/7/73/Mpv_logo_%28official%29.png"},"buttons": [{"label":"Listen Song", "url": "https://music.youtube.com/watch?v='$id'"}]},"pid":786}, "nonce": "'$(date +%s%N)'"}' #pid needs to be set and constant so discord knows which application to update
if [ ! -e "$handshook" ]; then
handshake='{"v":1,"client_id":"'$presence_client_id'"}'
# handshake with opcode
printf "\\000\\000\\000\\000\\$(printf "%03o" "${#handshake}")\\000\\000\\000%s" "$handshake" >"$presence"
sleep 2
touch "$handshook"
fi
set_activity "$payload" >"$presence"
}
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
@@ -198,7 +160,7 @@ play() {
#get song's audio url
random_no=$(head /dev/urandom | tr -dc '0-4' | cut -c1)
andy_yt_ver="7.05.54"
andy_yt_ver="7.08.53"
extra_up=$((random_no > 1))
droid_agent="com.google.android.apps.youtube.music/$andy_yt_ver (Linux; U; Android 1$random_no) gzip"
json="{
@@ -231,23 +193,14 @@ play() {
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 -e -h "string:x-canonical-private-synchronous:${0##*/}" -i "$logdir/default.jpg" "Now Playing" "$title" -t 5000
curl -s "https://i.ytimg.com/vi/$id/hqdefault.jpg" -o - | magick convert - -crop 270x270+105+45 "$logdir/default.jpg" && notify-send -e -h "string:x-canonical-private-synchronous:${0##*/}" -i "$logdir/default.jpg" "Now Playing" "$title" -t 5000
pgrep -f "$socket" >/dev/null || (setsid -f mpv --really-quiet --input-ipc-server="$socket" --idle --quiet >/dev/null && sleep 1)
printf '{"command":["loadfile","%s","replace"]}\n' "$audio_url" | socat - "$socket"
printf "SONG='%s'\nARTIST='%s'\nID='%s'" "$(printf '%s' "$title" | sed 's|[^-]*$||g;s|-$||g;s| $||g;s|^ ||g')" "$(printf '%s' "$title" | sed 's_.* - __;s| $||')" "$id" >"$logdir/current"
#required for discord rich presence to set end timestamp
duration=$(printf '%s' "$audio_url" | sed -nE 's|.*&dur=([^\.]*).*|\1|p')
#self explainatory
get_song_lyrics "$id" &
#start the presence
pgrep -f "nc -U $discord_ipc" >/dev/null || setsid -f "$0" start_rich_presence
#update the discord rich presence, requires client id
update_rich_presence "$title" "$id" "$duration" &
#next songs data
[ -n "$3" ] && get_music_list &
@@ -261,7 +214,7 @@ search_play() {
[ -z "$query" ] && notify-send -e "Err.. Search query empty" -u critical -h "string:x-canonical-private-synchronous:${0##*/}" && exit 1
#storing context
printf '{"client":{"clientName":"WEB_REMIX","clientVersion":"1.20240501.01.00"}}' >"$logdir/context"
printf '{"client":{"clientName":"WEB_REMIX","clientVersion":"1.20240717.01.00"}}' >"$logdir/context"
#extracting your cookies so that the song list are according to your taste
get_cookies
@@ -321,13 +274,8 @@ menu() {
logdir="/tmp/${0##*/}"
socket="$logdir/${0##*/}-mpvsocket"
discord_ipc="${XDG_RUNTIME_DIR}/discord-ipc-0"
handshook="$logdir/handshook"
presence="$logdir/${0##*/}-presence"
presence_client_id="1165951947245879316"
base_url="https://music.youtube.com"
[ -d "$logdir" ] || mkdir "$logdir"
[ -p "$presence" ] || mkfifo "$presence"
cookie="$logdir/cookies"
agent="Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36"