pod 5.7 KB

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