123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160 |
- #!/bin/sh
- POD_BASE_DIR=${POD_BASE_DIR:-~/Music/}
- POD_SUB_DIR=${POD_SUB_DIR:-Podcasts}
- show_usage() {
- echo "$(basename $0) [ list | play [ FILE ] | pick [-x] | pause | stop | listened | update-feeds ]" >&2
- }
- show_help() {
- show_usage
- cat <<EOF
- $(basename $0): Manage podcasts
- help : show help
- list : list podcast episodes
- play [ FILE ] : if FILE, play, picking up from where last paused/stopped
- pick [ -x ] : pick a podcast to play, if -x, using an X-windows picker
- pause : pause current episode, saving current location
- stop : stop current episode, saving current location
- listened [ FILE* ] : mark current episode as listened or if FILES mark them
- update-feeds : update feeds
- EOF
- }
- git_annex_wrap() {
- CUR_DIR=$(pwd)
- cd $POD_BASE_DIR
- git annex $@
- RET_VAL=$?
- cd $CUR_DIR
- return $RET_VAL
- }
- find_unlistened() {
- git_annex_wrap find --metadata "tag=unlistened" --include='Podcasts/*' --in=here
- }
- play_pod() {
- FILE=$1
- PAUSE_LOC=$(git_annex_wrap metadata -g seek_time $FILE)
- if [ "${#PAUSE_LOC}" -lt 1 ] ; then
- mpc add $FILE
- mpc play
- else
- mpc add $FILE
- mpc play
- mpc seek $PAUSE_LOC
- fi
- }
- get_cur_seek() {
- mpc status | \
- awk 'NR==2 {print $0}' |\
- grep -Eo '([0-9]+:)?[0-9][0-9]:[0-9][0-9]' |\
- head -n 1
- }
- get_cur_file() {
- mpc status -f '%file%' | head -n 1
- }
- set_loc() {
- FILE=$1
- SEEK=$2
- if echo $FILE | grep 'Podcasts/' > /dev/null ; then
- git_annex_wrap metadata -s "seek_time=${SEEK}" "${FILE}"
- fi
- }
- if [ $# -lt 1 ] ; then
- show_usage
- exit 1
- fi
- CMD=$1
- shift
- case "$CMD" in
- help)
- show_help
- exit
- ;;
- list)
- find_unlistened
- exit
- ;;
- play)
- pod stop
- mpc clear
- if [ $# -lt 1 ] ; then
- for item in $(find_unlistened) ; do
- mpc add $item
- done
- else
- play_pod $1
- fi
- exit
- ;;
- pick)
- CHOSEN_POD=''
- if [ "$1" == "-x" ] ; then
- CHOSEN_POD=$(find_unlistened | rofi -dmenu -p "podcast")
- else
- CHOSEN_POD=$(find_unlistened | fzf)
- fi
- if [ "${#CHOSEN_POD}" -gt 0 ] ; then
- play_pod $CHOSEN_POD
- fi
- exit
- ;;
- pause)
- FILE=$(get_cur_file)
- SEEK=$(get_cur_seek)
- mpc pause
- set_loc $FILE $SEEK
- exit
- ;;
- stop)
- FILE=$(get_cur_file)
- SEEK=$(get_cur_seek)
- mpc stop
- set_loc $FILE $SEEK
- exit
- ;;
- listened)
- if [ $# -lt 1 ] ; then
- FILE=$(get_cur_file)
- git_annex_wrap metadata -s tag=listened $FILE
- git_annex_wrap metadata -r seek_time $FILE
- else
- for FILE in $* ; do
- git_annex_wrap metadata -s tag=listened $FILE
- git_annex_wrap metadata -r seek_time $FILE
- done
- fi
- exit
- ;;
- update-feeds)
- CUR_DIR=$(pwd)
- cd $POD_BASE_DIR
- grep -v '^# ' ${POD_SUB_DIR}/podcasts-list.txt |\
- cut -f2 |\
- xargs -L1 git annex importfeed \
- --template="${POD_SUB_DIR}/\${feedtitle}/\${itempubdate}-\${itemtitle}\${extension}"
- git annex add ${POD_SUB_DIR}
- git commit -m "Caught podcasts"
- git annex find --include='Podcasts/*' --and --not --metadata tag=listened |\
- xargs git annex metadata --tag unlistened
- mpc update
- cd $CUR_DIR
- exit
- ;;
- *)
- show_usage
- echo
- echo "Subcommand $CMD is not valid." >&2
- exit 1
- ;;
- esac
|