pod 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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 : export unlistened podcasts to devices
  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. if [ $1 = '-' ] ; then
  136. cat | while read FILE ; do
  137. git_annex_wrap metadata -s tag=listened "${FILE}"
  138. done
  139. else
  140. for FILE in $* ; do
  141. git_annex_wrap metadata -s tag=listened "${FILE}"
  142. done
  143. fi
  144. fi
  145. exit
  146. ;;
  147. update-feeds)
  148. CUR_DIR=$(pwd)
  149. cd $POD_BASE_DIR
  150. grep -v '^# ' ${POD_SUB_DIR}/podcasts-list.txt |\
  151. while read -r LINE; do
  152. URL=${LINE##* }
  153. TITLE=${LINE% *}
  154. echo "Downloading ${TITLE}..."
  155. git annex importfeed \
  156. --template="${POD_SUB_DIR}/\${feedtitle}/\${itempubdate}-\${itemtitle}\${extension}" \
  157. "${URL}"
  158. echo
  159. done
  160. git annex add ${POD_SUB_DIR}
  161. git commit -m "Caught podcasts"
  162. git annex find --include="${POD_SUB_DIR}/*" --and --not --metadata tag=listened |\
  163. xargs git annex metadata --tag unlistened
  164. mpc update
  165. cd $CUR_DIR
  166. exit
  167. ;;
  168. wanted)
  169. find_unlistened --not --in=here
  170. exit
  171. ;;
  172. export)
  173. grep -v '^# ' ${POD_BASE_DIR}/${POD_SUB_DIR}/export-devices.csv | \
  174. while read -r LINE; do
  175. REMOTE=$(echo "${LINE}" | cut -f 1)
  176. DIRECTORY=$(echo "${LINE}" | cut -f 2)
  177. BRANCH=$(echo "${LINE}" | cut -f 3)
  178. if [ -d "${DIRECTORY}" ] ; then
  179. git_annex_wrap export "${BRANCH}" --to "${REMOTE}"
  180. fi
  181. done
  182. exit
  183. ;;
  184. *)
  185. show_usage
  186. echo
  187. echo "Subcommand $CMD is not valid." >&2
  188. exit 1
  189. ;;
  190. esac