pod 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. #!/usr/bin/env bash
  2. POD_BASE_DIR=${POD_BASE_DIR:-~/Music/}
  3. POD_SUB_DIR=${POD_SUB_DIR:-Podcasts}
  4. POD_EXPORT_BRANCH=${POD_EXPORT_BRANCH:-master}
  5. POD_EXPORT_TARGET=${POD_EXPORT_TARGET:-sansa-cast}
  6. show_usage() {
  7. echo "$(basename $0) [ list | list-all | play [ FILE ] | pick [-x] | pause | stop | listened | update-feeds | wanted | export [ BRANCH [ TARGET ] ] ]" >&2
  8. }
  9. show_help() {
  10. show_usage
  11. cat <<EOF
  12. $(basename $0): Manage podcasts
  13. help : show help
  14. list : list local podcast episodes
  15. list-all : list all podcast episodes
  16. play [ FILE ] : if FILE, play, picking up from where last paused/stopped
  17. pick [ -x ] : pick a podcast to play, if -x, using an X-windows picker
  18. pause : pause current episode, saving current location
  19. stop : stop current episode, saving current location
  20. listened [ FILE* ] : mark current episode as listened or if FILES mark them
  21. update-feeds : update feeds
  22. wanted : list wanted episodes
  23. export [ BRNCH ] [ TGT ] : export unlistened podcasts to device
  24. EOF
  25. }
  26. git_annex_wrap() {
  27. CUR_DIR=$(pwd)
  28. cd $POD_BASE_DIR
  29. git annex $@
  30. RET_VAL=$?
  31. cd $CUR_DIR
  32. return $RET_VAL
  33. }
  34. find_unlistened() {
  35. git_annex_wrap find --metadata "tag=unlistened" --include="${POD_SUB_DIR}/*" $@
  36. }
  37. play_pod() {
  38. FILE=$1
  39. PAUSE_LOC=$(git_annex_wrap metadata -g seek_time $FILE)
  40. if [ "${#PAUSE_LOC}" -lt 1 ] ; then
  41. mpc add $FILE
  42. mpc play
  43. else
  44. mpc add $FILE
  45. mpc play
  46. mpc seek $PAUSE_LOC
  47. fi
  48. }
  49. get_cur_seek() {
  50. mpc status | \
  51. awk 'NR==2 {print $0}' |\
  52. grep -Eo '([0-9]+:)?[0-9][0-9]:[0-9][0-9]' |\
  53. head -n 1
  54. }
  55. get_cur_file() {
  56. mpc status -f '%file%' | head -n 1
  57. }
  58. set_loc() {
  59. FILE=$1
  60. SEEK=$2
  61. if echo $FILE | grep "${POD_SUB_DIR}/" > /dev/null ; then
  62. git_annex_wrap metadata -s "seek_time=${SEEK}" "${FILE}"
  63. fi
  64. }
  65. if [ $# -lt 1 ] ; then
  66. show_usage
  67. exit 1
  68. fi
  69. CMD=$1
  70. shift
  71. case "$CMD" in
  72. help)
  73. show_help
  74. exit
  75. ;;
  76. list)
  77. find_unlistened --in=here
  78. exit
  79. ;;
  80. list-all)
  81. find_unlistened
  82. exit
  83. ;;
  84. play)
  85. pod stop
  86. mpc clear
  87. if [ $# -lt 1 ] ; then
  88. for item in $(find_unlistened --in=here) ; do
  89. mpc add $item
  90. done
  91. else
  92. play_pod $1
  93. fi
  94. exit
  95. ;;
  96. pick)
  97. CHOSEN_POD=''
  98. if [ "$1" == "-x" ] ; then
  99. CHOSEN_POD=$(find_unlistened --in=here | cut -d'/' -f 2- | rofi -dmenu -p "podcast")
  100. else
  101. CHOSEN_POD=$(find_unlistened --in=here | cut -d'/' -f 2- | fzf)
  102. fi
  103. if [ "${#CHOSEN_POD}" -gt 0 ] ; then
  104. play_pod "${POD_SUB_DIR}/${CHOSEN_POD}"
  105. fi
  106. exit
  107. ;;
  108. pause)
  109. FILE=$(get_cur_file)
  110. SEEK=$(get_cur_seek)
  111. mpc pause
  112. set_loc $FILE $SEEK
  113. exit
  114. ;;
  115. stop)
  116. FILE=$(get_cur_file)
  117. SEEK=$(get_cur_seek)
  118. mpc stop
  119. set_loc $FILE $SEEK
  120. exit
  121. ;;
  122. listened)
  123. if [ $# -lt 1 ] ; then
  124. FILE=$(get_cur_file)
  125. git_annex_wrap metadata -s tag=listened $FILE
  126. else
  127. for FILE in $* ; do
  128. git_annex_wrap metadata -s tag=listened $FILE
  129. done
  130. fi
  131. exit
  132. ;;
  133. update-feeds)
  134. CUR_DIR=$(pwd)
  135. cd $POD_BASE_DIR
  136. grep -v '^# ' ${POD_SUB_DIR}/podcasts-list.txt |\
  137. while read -r LINE; do
  138. URL=${LINE##* }
  139. TITLE=${LINE% *}
  140. echo "Downloading ${TITLE}..."
  141. git annex importfeed \
  142. --template="${POD_SUB_DIR}/\${feedtitle}/\${itempubdate}-\${itemtitle}\${extension}" \
  143. "${URL}"
  144. echo
  145. done
  146. git annex add ${POD_SUB_DIR}
  147. git commit -m "Caught podcasts"
  148. git annex find --include="${POD_SUB_DIR}/*" --and --not --metadata tag=listened |\
  149. xargs git annex metadata --tag unlistened
  150. mpc update
  151. cd $CUR_DIR
  152. exit
  153. ;;
  154. wanted)
  155. find_unlistened --not --in=here
  156. exit
  157. ;;
  158. export)
  159. BRANCH=${1:-${POD_EXPORT_BRANCH}}
  160. TARGET=${2:-${POD_EXPORT_TARGET}}
  161. git_annex_wrap export "${BRANCH}" -t "${TARGET}"
  162. exit
  163. ;;
  164. *)
  165. show_usage
  166. echo
  167. echo "Subcommand $CMD is not valid." >&2
  168. exit 1
  169. ;;
  170. esac