pod 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. #!/bin/sh
  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 | rofi -dmenu -p "podcast")
  97. else
  98. CHOSEN_POD=$(find_unlistened --in=here | fzf)
  99. fi
  100. if [ "${#CHOSEN_POD}" -gt 0 ] ; then
  101. play_pod $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. cut -f2 |\
  137. xargs -L1 git annex importfeed \
  138. --template="${POD_SUB_DIR}/\${feedtitle}/\${itempubdate}-\${itemtitle}\${extension}"
  139. git annex add ${POD_SUB_DIR}
  140. git commit -m "Caught podcasts"
  141. git annex find --include='Podcasts/*' --and --not --metadata tag=listened |\
  142. xargs git annex metadata --tag unlistened
  143. mpc update
  144. cd $CUR_DIR
  145. exit
  146. ;;
  147. wanted)
  148. find_unlistened --not --in=here
  149. exit
  150. ;;
  151. *)
  152. show_usage
  153. echo
  154. echo "Subcommand $CMD is not valid." >&2
  155. exit 1
  156. ;;
  157. esac