pod 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. if [ $# -lt 1 ] ; then
  78. find_unlistened --in=here
  79. else
  80. find_unlistened --in=here | grep $@
  81. fi
  82. exit
  83. ;;
  84. list-all)
  85. if [ $# -lt 1 ] ; then
  86. find_unlistened
  87. else
  88. find_unlistened | grep $@
  89. fi
  90. exit
  91. ;;
  92. play)
  93. pod stop
  94. mpc clear
  95. if [ $# -lt 1 ] ; then
  96. for item in $(find_unlistened --in=here) ; do
  97. mpc add $item
  98. done
  99. else
  100. play_pod $1
  101. fi
  102. exit
  103. ;;
  104. pick)
  105. CHOSEN_POD=''
  106. if [ "$1" == "-x" ] ; then
  107. CHOSEN_POD=$(find_unlistened --in=here | cut -d'/' -f 2- | rofi -dmenu -p "podcast")
  108. else
  109. CHOSEN_POD=$(find_unlistened --in=here | cut -d'/' -f 2- | fzf)
  110. fi
  111. if [ "${#CHOSEN_POD}" -gt 0 ] ; then
  112. play_pod "${POD_SUB_DIR}/${CHOSEN_POD}"
  113. fi
  114. exit
  115. ;;
  116. pause)
  117. FILE=$(get_cur_file)
  118. SEEK=$(get_cur_seek)
  119. mpc pause
  120. set_loc $FILE $SEEK
  121. exit
  122. ;;
  123. stop)
  124. FILE=$(get_cur_file)
  125. SEEK=$(get_cur_seek)
  126. mpc stop
  127. set_loc $FILE $SEEK
  128. exit
  129. ;;
  130. listened)
  131. if [ $# -lt 1 ] ; then
  132. FILE=$(get_cur_file)
  133. git_annex_wrap metadata -s tag=listened $FILE
  134. else
  135. for FILE in $* ; do
  136. git_annex_wrap metadata -s tag=listened $FILE
  137. done
  138. fi
  139. exit
  140. ;;
  141. update-feeds)
  142. CUR_DIR=$(pwd)
  143. cd $POD_BASE_DIR
  144. grep -v '^# ' ${POD_SUB_DIR}/podcasts-list.txt |\
  145. while read -r LINE; do
  146. URL=${LINE##* }
  147. TITLE=${LINE% *}
  148. echo "Downloading ${TITLE}..."
  149. git annex importfeed \
  150. --template="${POD_SUB_DIR}/\${feedtitle}/\${itempubdate}-\${itemtitle}\${extension}" \
  151. "${URL}"
  152. echo
  153. done
  154. git annex add ${POD_SUB_DIR}
  155. git commit -m "Caught podcasts"
  156. git annex find --include="${POD_SUB_DIR}/*" --and --not --metadata tag=listened |\
  157. xargs git annex metadata --tag unlistened
  158. mpc update
  159. cd $CUR_DIR
  160. exit
  161. ;;
  162. wanted)
  163. find_unlistened --not --in=here
  164. exit
  165. ;;
  166. export)
  167. BRANCH=${1:-${POD_EXPORT_BRANCH}}
  168. TARGET=${2:-${POD_EXPORT_TARGET}}
  169. git_annex_wrap export "${BRANCH}" -t "${TARGET}"
  170. exit
  171. ;;
  172. *)
  173. show_usage
  174. echo
  175. echo "Subcommand $CMD is not valid." >&2
  176. exit 1
  177. ;;
  178. esac