pod 4.1 KB

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