scripts

shell scripts
git clone git://git.bitsmanent.org/scripts
Log | Files | Refs | README

commit 5087aef506e217280f89efdec0442bfa52bf6f85
parent 9ca3c5f3fb00e4cfb3d7edc95947600d616f9128
Author: Claudio Alessi <smoppy@gmail.com>
Date:   Mon, 19 Sep 2016 14:31:04 +0200

[moin] Do not hardcode API key and playlist ID.

Diffstat:
MREADME.md | 16+++++++++-------
Msrc/moin | 27++++++++++++++++-----------
2 files changed, 25 insertions(+), 18 deletions(-)

diff --git a/README.md b/README.md @@ -271,18 +271,20 @@ 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 given $DEFLIST is used. Default player is mpv, refine the -$PLAYER and $PLAYER_OPTS variables to fit your needs. You may want to wake up -in the morning by putting the following in your crontab: +The [moin](src/moin) script takes a random song from the specified YouTube +playlist and play it. The default player is mpv but you can refine the $PLAYER +and $PLAYER_OPTS variables to fit your needs. You may want to wake up in the +morning by putting the following in your crontab: ``` # Plays from monday to friday at 08:00am -0 8 * * 1-5 /path/of/moin +0 8 * * 1-5 /path/of/moin <YOUR-API-KEY> <PLAYLIST-ID> ``` -Where to take the playlist ID? It's specified by the "list" parameter of the -URL. For example, if the URL of your playlist is the following: +Both the API key and the playlist ID are required. If you don't have a key then +[make one here](https://console.developers.google.com/apis/credentials). +The playlist ID is specified by the "list" parameter of the URL. For example, +if the URL of your playlist is the following: ``` https://www.youtube.com/watch?v=xvIuuKVwY_8&list=RDEMWmz07MSPRGna5rHl5FWPRw&index=40 diff --git a/src/moin b/src/moin @@ -1,22 +1,22 @@ #!/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" + apikey="$1" + cmd="$2" + params="$3" + wget -qO - "$APIURI/$cmd?key=$apikey&part=contentDetails&$params" } ytplrand() { - listid="$1" + apikey="$1" + listid="$2" maxres=50 - data="$(ytapi "playlists" "id=$listid")" + data="$(ytapi "$apikey" "playlists" "id=$listid")" nitems="$(echo "$data" |grep itemCount |cut -d: -f2)" choose="$(shuf -i 1-"$nitems" -n1)" page="$(echo "1 + ($choose - 1) / $maxres" |bc)" @@ -26,19 +26,24 @@ ytplrand() { # 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}")" + 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() { - listid="$1" - [ -z "$listid" ] && listid="$DEFLIST" + 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 "$listid")" + songid="$(ytplrand "$apikey" "$listid")" "$PLAYER" $PLAYER_OPTS "$VIDURI/$songid" #rv=$? rv=0