| 1 | #!/usr/bin/env bash |
| 2 | |
| 3 | # Useful to follow command execution and determine when an extra echo outputs during silent mode. |
| 4 | #set -x |
| 5 | |
| 6 | # ---------------------------------------------------------------------------------------------------------------------------------- |
| 7 | # Filename: podget_update_playlists {{{ |
| 8 | # Maintainer: Dave Vehrs <davevehrs(at)users.sourceforge.net> |
| 9 | # Copyright: (c) 2016 Dave Vehrs |
| 10 | # |
| 11 | # This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public |
| 12 | # License as published by the Free Software Foundation; either version 3 of the License, or (at your option) any |
| 13 | # later version. |
| 14 | # |
| 15 | # This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied |
| 16 | # warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
| 17 | # details. |
| 18 | # ---------------------------------------------------------------------------------------------------------------------------------- |
| 19 | # Exit Codes {{{ |
| 20 | |
| 21 | # "Reserved" Exit codes |
| 22 | # 1 General Error |
| 23 | # 2 Misuse of shell built-ins |
| 24 | # 126 Command invoked cannot execute |
| 25 | # 127 Command not found |
| 26 | # 128+n Invalid argument to exit |
| 27 | # 130 Script terminated by Control-C (128+2) |
| 28 | # 143 Script terminated by TERM signal (128+15) |
| 29 | |
| 30 | # "Our" Exit codes |
| 31 | |
| 32 | # Display Help (set to '0' because it is an valid exit condition, not an error.) |
| 33 | ERR_DISPLAYHELP=0 |
| 34 | |
| 35 | # Library directory not defined. |
| 36 | ERR_LIBNOTDEF=50 |
| 37 | |
| 38 | # }}} |
| 39 | # ---------------------------------------------------------------------------------------------------------------------------------- |
| 40 | # Traps {{{ |
| 41 | |
| 42 | # FUNCNAME is declared with a default value in case the trap is triggered while |
| 43 | # outside a function. |
| 44 | trap 'EXIT_ERROR ${LINENO} $? ${FUNCNAME:-"Unconfigured"}' ERR |
| 45 | |
| 46 | # trap to run CLEANUP function if program receives a TERM (kill) or INT (ctrl-c) signal |
| 47 | # - CLEANUP called in line for other normal exits. |
| 48 | trap 'CLEANUP_AND_EXIT 143' TERM |
| 49 | trap 'CLEANUP_AND_EXIT 130' INT |
| 50 | |
| 51 | # }}} |
| 52 | # ---------------------------------------------------------------------------------------------------------------------------------- |
| 53 | # Set Shell Options to catch errors ('man bash' for details) {{{ |
| 54 | |
| 55 | set -o errexit |
| 56 | set -o nounset |
| 57 | set -o pipefail |
| 58 | |
| 59 | # Enable extended glob matches. |
| 60 | shopt -s extglob |
| 61 | |
| 62 | # }}} |
| 63 | # ---------------------------------------------------------------------------------------------------------------------------------- |
| 64 | # Help text and default file formats {{{ |
| 65 | |
| 66 | : << HELP_STEXT |
| 67 | -c --config <FILE> Name of configuration file to use. |
| 68 | -d --dir_config <DIRECTORY> Directory that configuration files are |
| 69 | --dir_session <DIRECTORY> Directory that session files are stored in. |
| 70 | -f --force Force download of items from each feed even |
| 71 | if they have already been downloaded. |
| 72 | -l --library <DIRECTORY> Directory to store downloaded files in. |
| 73 | -s --silent Run silently (for cron jobs). |
| 74 | --verbosity <LEVEL> Set verbosity level (0-4). |
| 75 | -v Set verbosity to level 1. |
| 76 | -vv Set verbosity to level 2. |
| 77 | -vvv Set verbosity to level 3. |
| 78 | -vvvv Set verbosity to level 4. |
| 79 | -h --help Display help. |
| 80 | HELP_STEXT |
| 81 | |
| 82 | # }}} |
| 83 | # ---------------------------------------------------------------------------------------------------------------------------------- |
| 84 | # Defaults {{{ |
| 85 | |
| 86 | ######################################################################################################################### |
| 87 | ## Do not configure here. Run podget once to install default user configuration files ($HOME/.podget) and edit there. ## |
| 88 | ######################################################################################################################### |
| 89 | |
| 90 | # Set DIR_LIBRARY, DIR_SESSION, and DIR_LOG in config file. |
| 91 | DIR_CONFIG="${HOME}/.podget" |
| 92 | CONFIG_CORE="podgetrc" |
| 93 | |
| 94 | # Default VERBOSITY |
| 95 | # 0 == silent |
| 96 | # 1 == Warning messages only. |
| 97 | # 2 == Progress and Warning messages. |
| 98 | # 3 == Debug, Progress and Warning messages. |
| 99 | # 4 == All messages and wget set to maximum VERBOSITY. |
| 100 | VERBOSITY=2 |
| 101 | |
| 102 | # Force |
| 103 | # 0 == Only download new material. |
| 104 | # 1 == Force download all items even those you've downloaded before. |
| 105 | FORCE=0 |
| 106 | |
| 107 | # Date format for new playlist names |
| 108 | DATE_FORMAT=+%F |
| 109 | |
| 110 | # ASX Playlists for Windows Media Player |
| 111 | # 0 == do not create |
| 112 | # 1 == create |
| 113 | ASX_PLAYLIST=0 |
| 114 | |
| 115 | # Default DEBUG Disabled (Deletion of temporary files allowed) |
| 116 | DEBUG=0 |
| 117 | |
| 118 | # Default DEBUG string leader |
| 119 | DEBUG_LEADER="DEBUG --" |
| 120 | |
| 121 | ######################################################################################################################### |
| 122 | ## Do not configure here. Run podget once to install default user configuration files ($HOME/.podget) and edit there. ## |
| 123 | ######################################################################################################################### |
| 124 | |
| 125 | # }}} |
| 126 | # ---------------------------------------------------------------------------------------------------------------------------------- |
| 127 | # Functions {{{ |
| 128 | |
| 129 | # Function: CLEANUP_AND_EXIT |
| 130 | # Closes session and removes lock file (if it exists) |
| 131 | # ARGUMENTS: |
| 132 | # ${1} == Exit Status to report. |
| 133 | CLEANUP_AND_EXIT() { |
| 134 | local EXITSTATUS=${1} |
| 135 | exit "${EXITSTATUS}" |
| 136 | } |
| 137 | |
| 138 | display_shelp() { |
| 139 | echo; echo "Usage $0 [options]"; echo |
| 140 | sed --silent -e '/HELP_STEXT$/,/^HELP_STEXT/p' "$0" | sed -e '/HELP_STEXT/d' |
| 141 | } |
| 142 | |
| 143 | EXIT_ERROR() { |
| 144 | # Name of script |
| 145 | JOB_NAME=$(basename "$0") |
| 146 | # The following three variables are configured with a default value in case |
| 147 | # the function is called without options set. |
| 148 | LINENUM="${1:-"Unconfigured"}" # Line with error |
| 149 | EXITSTATUS="${2:-"Unconfigured"}" # exit status of error |
| 150 | FUNCTION="${3:-"Unconfigured"}" # If error occurred in a function, its name will be listed. |
| 151 | |
| 152 | echo |
| 153 | echo "Error:" |
| 154 | echo -e "\tScript:\t\t${JOB_NAME}" |
| 155 | |
| 156 | # Function line only appears if it has been set to value other than the |
| 157 | # default. Works on the assumption that "Unconfigured" is not likely to be |
| 158 | # chosen as a function name. |
| 159 | if [[ ${FUNCTION} != "Unconfigured" ]]; then |
| 160 | echo -e "\tFunction:\t${FUNCTION}" |
| 161 | fi |
| 162 | |
| 163 | echo -e "\tAt line:\t${LINENUM}" |
| 164 | echo -e "\tExit Status:\t${EXITSTATUS}" |
| 165 | echo |
| 166 | CLEANUP_AND_EXIT 1 |
| 167 | } |
| 168 | |
| 169 | PLAYLIST_ConvertToASX() { |
| 170 | local DIR_LIBRARY=${1} |
| 171 | local M3U_PLAYLISTNAME=${2} |
| 172 | ASX_LOCATION="\\SD Card\\POD\\" |
| 173 | ASX_PLAYLISTNAME=$(basename "${DIR_LIBRARY}"/"${M3U_PLAYLISTNAME}" .m3u).asx |
| 174 | sed --silent -e '/TEXT_ASX_BEGINNING$/,/^TEXT_ASX_BEGINNING/p' "$0" | |
| 175 | sed -e '/TEXT_ASX_BEGINNING/d' > "${DIR_LIBRARY}"/"${ASX_PLAYLISTNAME}" |
| 176 | |
| 177 | while read -r line ; do |
| 178 | # local FIXED_ENTRY=$(echo "${line}" | sed 's/\//\\/g') |
| 179 | local FIXED_ENTRY="${line//\//\\}" |
| 180 | { |
| 181 | echo ' <ENTRY>' |
| 182 | echo " <ref href = \"${ASX_LOCATION}${FIXED_ENTRY}\" />" |
| 183 | echo " <ref href = \".\\${FIXED_ENTRY}\" />" |
| 184 | echo ' </ENTRY>' |
| 185 | } >> "${DIR_LIBRARY}"/"${ASX_PLAYLISTNAME}" |
| 186 | done < "${DIR_LIBRARY}"/"${M3U_PLAYLISTNAME}" |
| 187 | |
| 188 | sed --silent -e '/TEXT_ASX_END$/,/^TEXT_ASX_END/p' "$0" | |
| 189 | sed -e '/TEXT_ASX_END/d' >> "${DIR_LIBRARY}"/"${ASX_PLAYLISTNAME}" |
| 190 | |
| 191 | # Removing unix2dos dependency. Converting to sed statement with in-place editing of the file in question. |
| 192 | # ctrl-v ctrl-m for windows line end. |
| 193 | sed -i 's/$/ /' "${DIR_LIBRARY}"/"${ASX_PLAYLISTNAME}" |
| 194 | } |
| 195 | |
| 196 | # }}} |
| 197 | # ---------------------------------------------------------------------------------------------------------------------------------- |
| 198 | # Version (Update with changes!) {{{ |
| 199 | |
| 200 | VERSION=0.1 |
| 201 | REPORT_VERSION=0 |
| 202 | |
| 203 | # }}} |
| 204 | # ---------------------------------------------------------------------------------------------------------------------------------- |
| 205 | # Parse command line {{{ |
| 206 | |
| 207 | # Set defaults for command line options so variables do not conflict with 'set -o nounset' by being undeclared. |
| 208 | CMDL_FORCE=0 |
| 209 | |
| 210 | # Parameter Substitution will cause script to exit with an error when 'errexit' is enabled. Disable while processing the command |
| 211 | # line. |
| 212 | set +o errexit |
| 213 | # Also disable trap for reporting errors on non-zero exit statuses. |
| 214 | trap - ERR |
| 215 | |
| 216 | while (( $# >= 1 )); do |
| 217 | case ${1} in |
| 218 | -c | --config ) CONFIG_CORE=${2:-NONE} ; shift ; shift ;; |
| 219 | -d | --dir_config ) DIR_CONFIG=${2:-NONE} ; shift ; shift ;; |
| 220 | --dir_session ) CMDL_SESSION=${2:-NONE} ; shift ; shift ;; |
| 221 | -f | --force ) CMDL_FORCE=1 ; shift ;; |
| 222 | -l | --library ) CMDL_LIBRARY=${2:-NONE} ; shift ; shift ;; |
| 223 | -s | --silent ) VERBOSITY=0 ; shift ;; |
| 224 | -V | --version ) VERBOSITY=2 ; REPORT_VERSION=1 ; shift ;; |
| 225 | -v ) VERBOSITY=1 ; shift ;; |
| 226 | -vv ) VERBOSITY=2 ; shift ;; |
| 227 | -vvv ) VERBOSITY=3 ; shift ;; |
| 228 | -vvvv ) VERBOSITY=4 ; shift ;; |
| 229 | --verbosity ) VERBOSITY=${2:-NONE} ; shift ; shift ;; |
| 230 | * ) display_shelp ; CLEANUP_AND_EXIT ${ERR_DISPLAYHELP} ;; |
| 231 | esac |
| 232 | done |
| 233 | |
| 234 | # Re-enable errexit |
| 235 | set -o errexit |
| 236 | # Re-enable trap |
| 237 | trap 'EXIT_ERROR ${LINENO} $? ${FUNCNAME:-"Unconfigured"}' ERR |
| 238 | |
| 239 | if [[ -n ${VERBOSITY+set} ]] ; then |
| 240 | if [[ -z ${VERBOSITY##*[!0-9]*} ]]; then |
| 241 | echo "Verbosity is not a supported integer value" |
| 242 | exit 1 |
| 243 | fi |
| 244 | fi |
| 245 | |
| 246 | if (( VERBOSITY >= 2 )) ; then |
| 247 | echo "podget_update_playlists" |
| 248 | fi |
| 249 | |
| 250 | if (( REPORT_VERSION == 1 )); then |
| 251 | echo "Version: ${VERSION}" |
| 252 | CLEANUP_AND_EXIT 0 |
| 253 | fi |
| 254 | |
| 255 | if (( VERBOSITY >= 2 )) ; then |
| 256 | echo |
| 257 | fi |
| 258 | |
| 259 | if [[ ${CONFIG_CORE} == "NONE" ]]; then |
| 260 | echo "Unset filename for configuration" |
| 261 | CLEANUP_AND_EXIT 1 |
| 262 | fi |
| 263 | |
| 264 | if [[ ${DIR_CONFIG} == "NONE" ]]; then |
| 265 | echo "Unset directory to store configuration" |
| 266 | CLEANUP_AND_EXIT 1 |
| 267 | fi |
| 268 | |
| 269 | # }}} |
| 270 | # ---------------------------------------------------------------------------------------------------------------------------------- |
| 271 | # Test for existing configuration directory, if missing install it. {{{ |
| 272 | |
| 273 | if [[ ! -d ${DIR_CONFIG} ]] ; then |
| 274 | echo " Configuration directory not found." |
| 275 | CLEANUP_AND_EXIT 1 |
| 276 | fi |
| 277 | |
| 278 | if [[ ! -f "${DIR_CONFIG}/${CONFIG_CORE}" ]] ; then |
| 279 | echo "Configuration file does not exist." |
| 280 | CLEANUP_AND_EXIT 1 |
| 281 | fi |
| 282 | |
| 283 | # }}} |
| 284 | # ---------------------------------------------------------------------------------------------------------------------------------- |
| 285 | # Configuration {{{ |
| 286 | |
| 287 | # SHELLCHECK SC1090 |
| 288 | # shellcheck source=/dev/null |
| 289 | source "${DIR_CONFIG}"/"${CONFIG_CORE}" |
| 290 | |
| 291 | if (( DEBUG == 1 )) ; then |
| 292 | echo |
| 293 | echo "${DEBUG_LEADER} Parsing Config file." |
| 294 | echo -e "${DEBUG_LEADER} Config directory:\t\t${DIR_CONFIG}" |
| 295 | echo -e "${DEBUG_LEADER} Config file:\t\t\t${CONFIG_CORE}" |
| 296 | echo |
| 297 | fi |
| 298 | |
| 299 | # for testing |
| 300 | #echo "Verbosity: ${VERBOSITY}" |
| 301 | #echo "Debug: ${DEBUG}" |
| 302 | #CLEANUP_AND_EXIT 0 |
| 303 | |
| 304 | if [[ -n ${CMDL_LIBRARY+set} ]] ; then |
| 305 | if [[ ${CMDL_LIBRARY} == "NONE" ]]; then |
| 306 | echo "Unset directory to store podcast library" |
| 307 | CLEANUP_AND_EXIT 1 |
| 308 | fi |
| 309 | if (( VERBOSITY >= 3 )) ; then |
| 310 | echo -e "\t\tOverriding Library Directory specified in configuration file." |
| 311 | fi |
| 312 | DIR_LIBRARY=${CMDL_LIBRARY} |
| 313 | fi |
| 314 | |
| 315 | if (( VERBOSITY >= 3 )) ; then |
| 316 | echo -e "Library Directory:\t\t${DIR_LIBRARY}" |
| 317 | fi |
| 318 | |
| 319 | if [[ -z ${DIR_LIBRARY} ]] ; then |
| 320 | echo "ERROR - Library directory not defined." 1>&2 |
| 321 | CLEANUP_AND_EXIT ${ERR_LIBNOTDEF} |
| 322 | fi |
| 323 | |
| 324 | if [[ -n ${CMDL_ASX+set} ]]; then |
| 325 | ASX_PLAYLIST=${CMDL_ASX} |
| 326 | fi |
| 327 | |
| 328 | if (( CMDL_FORCE != 0 )); then |
| 329 | FORCE=${CMDL_FORCE} |
| 330 | fi |
| 331 | |
| 332 | if (( VERBOSITY >= 3 )) ; then |
| 333 | if (( DEBUG == 0 )); then |
| 334 | echo -e "Debug:\t\t\t\tDisabled - Reduced progress messages." |
| 335 | else |
| 336 | echo -e "Debug:\t\t\t\tEnabled - Increased progress messages." |
| 337 | fi |
| 338 | fi |
| 339 | |
| 340 | if (( VERBOSITY >= 1 )) ; then |
| 341 | echo |
| 342 | fi |
| 343 | |
| 344 | # }}} |
| 345 | # ---------------------------------------------------------------------------------------------------------------------------------- |
| 346 | # Loop over files in Library to create playlists {{{ |
| 347 | |
| 348 | CATEGORIES=$(ls -d ${DIR_LIBRARY}/*/ | awk -F'/' '{print $(NF-1)}') |
| 349 | |
| 350 | while read -r CAT; do |
| 351 | if (( VERBOSITY >= 1 )); then |
| 352 | echo "CATEGORY: ${CAT}" |
| 353 | fi |
| 354 | # Test to determine if Category directory contains any Feed directories. |
| 355 | if find "${DIR_LIBRARY}/${CAT}" -maxdepth 0 ! -empty -type d | read ;then |
| 356 | FEEDS=$(ls -d "${DIR_LIBRARY}/${CAT}"/*/ | awk -F'/' '{print $(NF-1)}') |
| 357 | while read -r FEED; do |
| 358 | if (( VERBOSITY >= 2 )); then |
| 359 | echo " FEED: ${FEED}" |
| 360 | fi |
| 361 | PLAYLIST_NAME="PLAYLIST_${FEED//[[:space:]]/_}.m3u" |
| 362 | if (( VERBOSITY >= 1 )); then |
| 363 | echo " PLAYLIST NAME: ${PLAYLIST_NAME}" |
| 364 | fi |
| 365 | if [ -f "${DIR_LIBRARY}/${PLAYLIST_NAME}" ]; then |
| 366 | rm -f "${DIR_LIBRARY}/${PLAYLIST_NAME}" |
| 367 | fi |
| 368 | touch "${DIR_LIBRARY}/${PLAYLIST_NAME}" |
| 369 | ITEMS=$(ls -1tr "${DIR_LIBRARY}/${CAT}/${FEED}/") |
| 370 | while read -r ITEM; do |
| 371 | if (( VERBOSITY >= 3 )); then |
| 372 | echo " ITEM: ${ITEM}" |
| 373 | fi |
| 374 | echo "${CAT}/${FEED}/${ITEM}" >> "${DIR_LIBRARY}/${PLAYLIST_NAME}" |
| 375 | done <<< "${ITEMS}" |
| 376 | done <<< "${FEEDS}" |
| 377 | else |
| 378 | echo " NO FEEDS" |
| 379 | fi |
| 380 | done <<< "${CATEGORIES}" |
| 381 | |
| 382 | |
| 383 | # }}} |
| 384 | # ---------------------------------------------------------------------------------------------------------------------------------- |
| 385 | # Close session with '0' status and clean up: {{{ |
| 386 | |
| 387 | # Disable extended glob matches. |
| 388 | shopt -u extglob |
| 389 | |
| 390 | CLEANUP_AND_EXIT 0 |
| 391 | |
| 392 | # }}} |
| 393 | # ---------------------------------------------------------------------------------------------------------------------------------- |
| 394 | # vim:tw=132:ts=4:sw=4 |