diff options
author | xyz <gky44px1999@gmail.com> | 2021-09-03 01:11:51 -0700 |
---|---|---|
committer | xyz <gky44px1999@gmail.com> | 2021-09-03 01:11:51 -0700 |
commit | e701d95348cd1020a9ab5ed27811cef4153a2855 (patch) | |
tree | f44ec3c78312e4b5db5413fe8c715e0445f19066 /.local | |
parent | e495dda3c3a57a5e6b3dbb5f0f0103f7e0288716 (diff) |
curlncm, fix bug, refactor, better error
Diffstat (limited to '.local')
-rwxr-xr-x | .local/bin/curlncm | 46 |
1 files changed, 32 insertions, 14 deletions
diff --git a/.local/bin/curlncm b/.local/bin/curlncm index faaf7018..5056b2de 100755 --- a/.local/bin/curlncm +++ b/.local/bin/curlncm @@ -11,32 +11,45 @@ url='/api/song/enhance/player/url' request_id=$(date +'%s%3N')_$(seq -w 1 1000 | shuf -n1) user_agent='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' -# need to improve to let a i r mutually exclusive? or find a way to chain their song ids and names +die () +{ + echo "error: $*" >&2 + exit 1 +} + +# let a i r mutually exclusive, can improve by chain ids # artist, song, djradio id -while getopts a:i:n:r: opt; do +flag=0 +while getopts a:i:r: opt; do case $opt in - a) aid="$OPTARG";; - i) id="$OPTARG";; - # use -n to rename the music if pass -i to download only one song - n) names="$OPTARG";; - r) rid="$OPTARG";; + a) [ "$flag" -eq 1 ] && die "options a,i,r are mutually exclusive" + aid="$OPTARG" + flag=1;; + i) [ "$flag" -eq 1 ] && die "options a,i,r are mutually exclusive" + ids="$OPTARG" + flag=1;; + r) [ "$flag" -eq 1 ] && die "options a,i,r are mutually exclusive" + rid="$OPTARG" + flag=1;; \?) exit 1;; esac done -[ -z "$aid" ] && [ -z "$id" ] && [ -z "$rid" ] && echo 'error' >&2 && exit 1 shift $((OPTIND-1)) +[ $flag -eq 0 ] && die "please specify at least one option" +[ $# -gt 1 ] && die "directory name should be at the end" download_dir="${1:-"$PWD"}" [ -d "$download_dir" ] || mkdir -p "$download_dir" if [ -n "$id" ]; then - ids="$id" - [ -z "$names" ] && names="$id" + # temporary solution, can improve by auto curl names + names="$id" num=1 else + # need to consider second page of djradio if [ -n "$rid" ]; then data="$(curl -s -G --data-urlencode id="$rid" '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)" elif [ -n "$aid" ]; then - data="$(curl -s -G --data-urlencode id="$aid" 'https://music.163.com/artist' | grep -E '\[\{.*\}\]' | sed -e 's/.*>\[{/\[{/' -e 's/}\]<.*/}\]/' | jq -r '.[]|[.id,.name]|@tsv')" + data="$(curl -s -G --data-urlencode id="$aid" 'https://music.163.com/artist' | grep -E '\[\{.*\}\]' | sed -e 's/.*>\[{/\[{/' -e 's/}\]<.*/}\]/' | jq -r '.[]|[.id,.name]|@tsv' | sort)" fi # with only awk, has a ',' at the end, not sure bad or not #echo "$data" | awk -F'\t' '{printf "%s,", $1}' @@ -47,7 +60,8 @@ fi # I don't fully understand following severl lines of code # I rewrite TianyiShi2001's python script, he rewrites NeteaseCloudMusicApi -text="$(printf '{"ids":"[%s]","br":999000,"header":{"appver":"8.0.0","versioncode":"140","buildver":"1623455100","resolution":"1920x1080","__csrf":"","os":"pc","requestId":"%s"}}' "$ids" "$request_id")" +cookie="$(printf '{"appver":"8.0.0","versioncode":"140","buildver":"1623455100","resolution":"1920x1080","__csrf":"","os":"pc","requestId":"%s"}' "$request_id")" +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")" @@ -56,7 +70,11 @@ download_urls="$(curl 'https://interface3.music.163.com/eapi/song/enhance/player # using user agent seems faster? maybe using cookie will be faster too? # maybe try to use -i for aria2c, should I? -# can't download some music if live abroad, maybe need change host? cdn? for downloading some music? don't think it will work, may need proxy or vpn +# can't download some music if live abroad, may need proxy or vpn for i in $(seq 1 "$num"); do - aria2c -U "$user_agent" -d "$download_dir" --auto-file-renaming=false --out="$(echo "$names" | head -n "$i" | tail -1).mp3" "$(echo "$download_urls" | head -n "$i" | tail -1)" + download_url="$(echo "$download_urls" | head -n "$i" | tail -1)" + if [ "$download_url" != 'null' ]; then + filename="$(echo "$names" | head -n "$i" | tail -1).mp3" + aria2c -U "$user_agent" -d "$download_dir" --auto-file-renaming=false -o "$filename" "$download_url" + fi done |