summaryrefslogtreecommitdiff
path: root/.local/bin/curlncm
blob: faaf701822b1d217806ac03ca39923e6c4bcbf11 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#!/bin/sh
# rewrite TianyiShi2001's python script which rewrite NeteaseCloudMusicApi node.js pai
# https://github.com/ytdl-org/youtube-dl/issues/18051#issuecomment-859964832
# https://github.com/Binaryify/NeteaseCloudMusicApi

# lan lan, aid=48860966, rid=793052426, example id=1397315179,1817498734
# cheng ruan, aid=46703185, rid=792968433

key='653832636b656e683864696368656e38'
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
# artist, song, djradio id
while getopts a:i:n: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";;
		\?) exit 1;;
	esac
done
[ -z "$aid" ] && [ -z "$id" ] && [ -z "$rid" ] && echo 'error' >&2 && exit 1 
shift $((OPTIND-1))
download_dir="${1:-"$PWD"}"
[ -d "$download_dir" ] || mkdir -p "$download_dir"

if [ -n "$id" ]; then
	ids="$id"
	[ -z "$names" ] && names="$id"
	num=1
else
	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')"
	fi
	# with only awk, has a ',' at the end, not sure bad or not
	#echo "$data" | awk -F'\t' '{printf "%s,", $1}'
	ids="$(echo "$data" | awk -F'\t' '{print $1}' | paste -sd ',')"
	names="$(echo "$data" | awk -F'\t' '{print $2}' | tr '/' '_')"
	num=$(echo "$names" | wc -l)
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")"
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"')"
download_urls="$(curl 'https://interface3.music.163.com/eapi/song/enhance/player/url' -s -d params="$encrypted_params" -H "$user_agent" | jq -r '.data|sort_by(.id)|.[].url')"

# 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
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)"
done