diff options
Diffstat (limited to 'sh/curlncm')
-rwxr-xr-x | sh/curlncm | 75 |
1 files changed, 75 insertions, 0 deletions
diff --git a/sh/curlncm b/sh/curlncm new file mode 100755 index 0000000..25f39aa --- /dev/null +++ b/sh/curlncm @@ -0,0 +1,75 @@ +#!/bin/sh +# rewrite TianyiShi2001's python script which rewrite NeteaseCloudMusicApi node.js api + +# references: +# https://github.com/ytdl-org/youtube-dl/issues/18051#issuecomment-859964832 +# https://github.com/Binaryify/NeteaseCloudMusicApi +# info about encrytion and api: +# https://github.com/metowolf/NeteaseCloudMusicApi/wiki +# https://github.com/darknessomi/musicbox/wiki + +# lan lan, aid=48860966, rid=793052426, example id=1397315179,1817498734 +# cheng ruan, aid=46703185, rid=792968433 + +# trial and error to get dj_max, don't know why, maybe because it is 2^31 and it is the size limit of whatever type of the variable +dj_max=2147483647 +dl_urls_tmp="$(mktemp)" +# printf 'e82ckenh8dichen8' | hexdump -ve '/1 "%02x"' +key='653832636b656e683864696368656e38' +url='/api/song/enhance/player/url' +request_id=$(date +'%s%3N')_$(seq -w 1 1000 | shuf -n1) +user_agent='Mozilla/5.0 (Linux; U; Android 9; zh-cn; Redmi Note 8 Build/PKQ1.190616.001) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/71.0.3578.141 Mobile Safari/537.36 XiaoMi/MiuiBrowser/12.5.22' +cookie="$(printf '"appver":"8.0.0","versioncode":"140","buildver":"1623455100","resolution":"1920x1080","__csrf":"","os":"pc","requestId":"%s"' "$request_id")" + +die () { + echo "error: $*" >&2 + exit 1 +} + +# artist, song, djradio id +while getopts a:i:r: opt; do + case $opt in + a) aid="$OPTARG";; + i) ids="$OPTARG";; + r) rid="$OPTARG";; + \?) exit 1;; + esac +done +shift $((OPTIND-1)) +download_dir="${1:-"$PWD"}" +[ -d "$download_dir" ] || mkdir -p "$download_dir" + +if [ "$ids" ]; then + # temporary solution, can improve by auto curl names + data="$(echo "$ids" | tr ',' '\n' | awk '{printf("%s\t%s\n",$0,"songname")}')" +else + if [ "$rid" ]; then + data="$(curl -s -G --data-urlencode id="$rid" -d limit="$dj_max" 'https://music.163.com/djradio' | grep 'songlist\|tt f-thide' | sed -e 's/.*songlist-\(.*\)" class.*/\1/g' -e 's/.*title="\(.*\)".*/\1/g' | paste -sd '\t\n' | sort | tr '/' '_')" + # can only curl featured 50 songs for the artist, can improve + elif [ "$aid" ]; then + data="$(curl -s -G --data-urlencode id="$aid" 'https://music.163.com/artist' | grep '\[{.*}\]' | sed -e 's/.*>\[{/\[{/' -e 's/}\]<.*/}\]/' | jq -r '.[]|[.id,.name]|@tsv' | sort | tr '/' '_')" + fi + # awk code not print separator at beginning and end steal from: + # https://unix.stackexchange.com/a/581785/459013 + # not sure awk one-liner or paste way which is better, need benchmark + ids="$(echo "$data" | awk -F'\t' '{printf "%s%s",sep,$1;sep=","}')" + #ids="$(echo "$data" | awk -F'\t' '{print $1}' | paste -sd ',')" +fi + +# I don't fully understand following several lines of code +# I rewrite TianyiShi2001's python script, he rewrites NeteaseCloudMusicApi +text="$(printf '{"ids":"[%s]","br":999000,"header":{%s}}' "$ids" "$cookie")" +message="nobody${url}use${text}md5forencrypt" +digest="$(printf '%s' "$message" | openssl dgst -md5 -hex | awk '{print $2}')" +params="$(printf '%s-36cd479b6b5-%s-36cd479b6b5-%s' "$url" "$text" "$digest")" +encrypted_params="$(printf '%s' "$params" | openssl enc -aes-128-ecb -K "$key" | hexdump -ve '/1 "%02X"')" +# curl default user agent header seems not working +curl -s -A "$user_agent" -d params="$encrypted_params" 'https://interface3.music.163.com/eapi/song/enhance/player/url' | jq -r '.data|sort_by(.id)|.[].url' > "$dl_urls_tmp" +[ "$(echo "$data" | wc -l)" -ne "$(wc -l < "$dl_urls_tmp")" ] && die "number of download urls doesn't match request" + +# can't download some music if live abroad, may need proxy or vpn +# using user_agent, content-type header, cookie header, referer are not necessary? but feels faster +# not sure about cookie header format for aria2c, this link shows that it's kinda messy, so I didn't use it +# https://github.com/aria2/aria2/issues/545#issuecomment-650070869 +echo "$data" | awk -F'\t' '{printf(" out=%s_%s.mp3\n",$2,$1)}' | paste "$dl_urls_tmp" - -d '\n' | aria2c -U "$user_agent" --header='Content-Type: application/x-www-form-urlencoded' --referer='https://music.163.com' -d "$download_dir" --auto-file-renaming=false --console-log-level=warn -i- +rm "$dl_urls_tmp" |