pod 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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='Podcasts/*' $@
  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 'Podcasts/' > /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. git_annex_wrap metadata -r seek_time $FILE
  124. else
  125. for FILE in $* ; do
  126. git_annex_wrap metadata -s tag=listened $FILE
  127. git_annex_wrap metadata -r seek_time $FILE
  128. done
  129. fi
  130. exit
  131. ;;
  132. update-feeds)
  133. CUR_DIR=$(pwd)
  134. cd $POD_BASE_DIR
  135. grep -v '^# ' ${POD_SUB_DIR}/podcasts-list.txt |\
  136. while read -r LINE; do
  137. URL=${LINE##* }
  138. TITLE=${LINE% *}
  139. echo "Downloading ${TITLE}..."
  140. git annex importfeed \
  141. --template="${POD_SUB_DIR}/\${feedtitle}/\${itempubdate}-\${itemtitle}\${extension}" \
  142. "${URL}"
  143. echo
  144. done
  145. git annex add ${POD_SUB_DIR}
  146. git commit -m "Caught podcasts"
  147. git annex find --include='Podcasts/*' --and --not --metadata tag=listened |\
  148. xargs git annex metadata --tag unlistened
  149. mpc update
  150. cd $CUR_DIR
  151. exit
  152. ;;
  153. wanted)
  154. find_unlistened --not --in=here
  155. exit
  156. ;;
  157. *)
  158. show_usage
  159. echo
  160. echo "Subcommand $CMD is not valid." >&2
  161. exit 1
  162. ;;
  163. esac