summaryrefslogtreecommitdiff
path: root/.local/bin/curlkg
blob: 2b6f4f91beb09516ac96e46c80be16ae8611f004 (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
#!/bin/sh
# references:
# https://blog.csdn.net/qq_41730930/article/details/79327757
# https://blog.csdn.net/u011112876/article/details/89634505
# https://github.com/pystardust/ytfzf

homepage_url='https://node.kg.qq.com/cgi/fcgi-bin/kg_ugc_get_homepage'
music_url='https://node.kg.qq.com/cgi/fcgi-bin/fcg_get_play_url'
#tab_space=$(printf '\t')
# num seems only 8 and 15 works
num=15
tmp_json_file='/tmp/curlkg.json'

while getopts u: opt; do
	case $opt in
		u) uid="$OPTARG";;
		\?) exit 1;;
	esac
done
# for lan lan, uid=649b9e82272a348b
# for cheng ruan, uid=64949d822c25328c
[ -z "$uid" ] && echo 'error: must provide share_uid
ex: curlkg -u 649b9e82272a348b ./' >&2 && exit 1 
shift $((OPTIND-1))
download_dir="${1:-"$PWD"}"
[ -d "$download_dir" ] || mkdir -p "$download_dir"

get_json () 
{
	curl -s -G --data-urlencode type=get_uinfo -d outCharset=utf-8 -d start="$1" -d num=$num -d share_uid="$uid" "$homepage_url" | sed -e 's/^MusicJsonCallback(//' -e 's/)$//'
}

# is it possible not to write to a file? only pipe stdout? should I?
get_json 1 > "$tmp_json_file"
ugc_total_count=$(jq '.data.ugc_total_count' "$tmp_json_file")
# can also use while loop with i=$((i+1))
# the calculation considers both ugc_total_count%num==0 and ugc_total_count%num>0
for i in $(seq 2 $(((ugc_total_count+num-1)/num))); do
	get_json "$i" >> "$tmp_json_file"
done

# how to show less aria2c stdout?
jq -r '.data.ugclist[]|"'"$music_url"'?shareid=\(.shareid)\n out=\(.title)_\(.time).m4a"' "$tmp_json_file" | aria2c -d "$download_dir" --auto-file-renaming=false -i-

## ytfzf way with curl only
#data="$(jq -r '.data.ugclist[]|[.shareid,.time,.title]|@tsv' "$tmp_json_file")"
#while IFS=$tab_space read -r shareid time title; do
#	filename="$download_dir/${title}_$time.m4a"
#	if ! [ -e "$filename" ]; then
#		# may need parallel download and control pid for curl to be faster
#		curl -L -G --data-urlencode shareid="$shareid" "$music_url" > "$filename"
#	else
#		echo "$filename exist"
#	fi
#done <<- EOF
#$data
#EOF

rm "$tmp_json_file"