-
Notifications
You must be signed in to change notification settings - Fork 3
/
moin
executable file
·54 lines (47 loc) · 1.36 KB
/
moin
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
#!/bin/sh
APIURI="https://www.googleapis.com/youtube/v3"
VIDURI="https://youtube.com/embed"
PLAYER="mpv"
PLAYER_OPTS="-vo null --quiet"
ytapi() {
apikey="$1"
cmd="$2"
params="$3"
wget -qO - "$APIURI/$cmd?key=$apikey&part=contentDetails&$params"
}
ytplrand() {
apikey="$1"
listid="$2"
maxres=50
data="$(ytapi "$apikey" "playlists" "id=$listid")"
nitems="$(echo "$data" |grep itemCount |cut -d: -f2)"
[ -z "$nitems" ] && return
choose="$(shuf -i 1-"$nitems" -n1)"
page="$(echo "1 + ($choose - 1) / $maxres" |bc)"
item="$(echo "$choose - (($page-1) * $maxres)" |bc)"
pagetok=""
# browse the pages until reach the item (that's the way YouTube works)
for p in $(seq $page); do
[ $p -gt 1 ] && pagetok="&pageToken=$(echo $data|grep nextPageToken |cut -d: -f2)"
data="$(ytapi "$apikey" "playlistItems" "playlistId=$listid&maxResults=${maxres}${pagetok}")"
done
echo "$(echo "$data" |grep videoId |head -n $item |tail -1 |cut -d: -f2 |tr -cd "[:alnum:]_-")"
}
main() {
apikey="$1"
listid="$2"
if [ -z "$apikey" -o -z "$listid" ]; then
echo "Usage: "$(basename "$0")" <API key> <playlist ID>"
exit 1
fi
pgrep -a "$PLAYER" >/dev/null && exit 1
rv=1
while [ $rv -ne 0 ]; do
songid="$(ytplrand "$apikey" "$listid")"
[ -z "$songid" ] && echo "$(basename "$0"): no song ID found" && break
"$PLAYER" $PLAYER_OPTS "$VIDURI/$songid"
#rv=$?
rv=0
done
}
main "$@"