commit d386c0e6c5f11c53a27b3e3abc39ff1e649a4ca6
parent 6b8d179d42d217946e12035b33f19a4bb4812ac9
Author: Claudio Alessi <smoppy@gmail.com>
Date: Sun, 18 Sep 2016 12:22:04 +0200
New script: moin.
Diffstat:
M | README.md | | | 11 | +++++++++++ |
A | src/moin | | | 48 | ++++++++++++++++++++++++++++++++++++++++++++++++ |
2 files changed, 59 insertions(+), 0 deletions(-)
diff --git a/README.md b/README.md
@@ -19,6 +19,7 @@ index
* [sober](#sober) - Simple sobriety checker.
* [mkbkp](#mkbkp) - Simple backups.
* [setmon](#setmon) - Switch to HDMI video/audio if any.
+* [moin](#moin) - Play a random song from a YouTube playlist
iwpick
------
@@ -267,3 +268,13 @@ setmon
Detects connected screens and set proper video and audio output. It uses xrandr
and pulseaudio. I wrote it to easily switch forth and back from my HDMI TV.
Change it to fit your needs.
+
+moin
+----
+Play a random song from a YouTube playlist. The playlist is specified by its
+ID, if none is give $DEFLIST is used. Default player is mpv, refine the $PLAYER
+and $PLAYER_OPTS variable to fit your needs. You may want to wake up in the
+morning like this:
+
+# Plays from monday to friday at 08:00am
+0 8 * * 1-5 /path/of/moin
diff --git a/src/moin b/src/moin
@@ -0,0 +1,48 @@
+#!/bin/sh
+
+APIKEY="AIzaSyA5626nAOZTrgWtobeTGrFe0vFoEo9VBZg"
+APIURI="https://www.googleapis.com/youtube/v3"
+VIDURI="https://youtube.com/embed"
+DEFLIST="RDEMWmz07MSPRGna5rHl5FWPRw"
+PLAYER="mpv"
+PLAYER_OPTS="-vo null --quiet"
+
+ytapi() {
+ cmd="$1"
+ params="$2"
+ wget -qO - "$APIURI/$cmd?key=$APIKEY&part=contentDetails&$params"
+}
+
+ytplrand() {
+ listid="$1"
+ maxres=50
+ data="$(ytapi "playlists" "id=$listid")"
+ nitems="$(echo "$data" |grep itemCount |cut -d: -f2)"
+ 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 "playlistItems" "playlistId=$listid&maxResults=${maxres}${pagetok}")"
+ done
+ echo "$(echo "$data" |grep videoId |head -n $item |tail -1 |cut -d: -f2 |tr -cd "[:alnum:]_-")"
+}
+
+main() {
+ listid="$1"
+ [ -z "$listid" ] && listid="$DEFLIST"
+
+ pgrep -a "$PLAYER" >/dev/null && exit 1
+ rv=1
+ while [ $rv -ne 0 ]; do
+ songid="$(ytplrand "$listid")"
+ "$PLAYER" $PLAYER_OPTS "$VIDURI/$songid"
+ #rv=$?
+ rv=0
+ done
+}
+
+main "$@"