#!/bin/bash # # kodiator # # Requires: egrep, GNU sed, curl # # Index tv.jw.org by processing the files from mediator.jw.org # and create a hirarchy of variables and arrays. Then read that # hirarchy and create directories containing .strm files (which # is text file with URLs that Kodi can open). # cleanup() { if (( CLEANUP )); then [[ -e /tmp/.kodiator ]] && rm "${temp_file}" [[ -e /tmp/.kodiator.history ]] && rm "${history_file}" fi } error() { echo "${@:-Something went wrong, exiting}" 1>&2 cleanup exit 1 } show_help() { cat</dev/null; do sub_array=$RANDOM done fi # Add new "sub array" eval "${array_now}+=($sub_array)" # We go in to it array_now=${array_now}_${sub_array} } new_variable() { # Arguments: "VARIABLE" "VALUE" # "VARIABLE" VALUE # Remove quotations marks variable=${1//\"/} value="${2//\"/}" # Remove underscore from the variable name variable=${variable//_/UNDERSCORE} # Add the variable to the current array eval ${array_now}'+=('${variable}')' || error # Give the variable a value eval ${array_now}_${variable}'="'${value}'"' || error } recursive_read() { # Arguments: DIRECTORY ARRAY SUB-ARRAYS ... # Current directory in the filesystem dir_now="${1}" dir_next="${dir_now}" shift # Current array array_now=${1} shift # Save important variables (from "sub arrays") # Decide if we should write files, create dirs or download files # # [0-9_]*$ Underscores and numbers in the end of the line # // Remove them # # ^.*_ All characters until the last underscore # // Remove them case $(echo $array_now | sed 's/[0-9_]*$//; s/^.*_//') in category|subcategories|media) # Use "name" or "title" as direcotry name, depending on which there name="$(eval 'echo ${'${array_now}'_name}')" title="$(eval 'echo ${'${array_now}'_title}')" sub_dir="${name:-${title}}" # Remove underscores, if any sub_dir="${sub_dir//\//}" if [[ ${sub_dir} ]]; then # Create the directory if [[ ! -e "${dir_now}/${sub_dir}" ]]; then mkdir -p "${dir_now}/${sub_dir}" || error fi # The dir to move into, next round dir_next="${dir_now}/${sub_dir}" fi # URL to another mediator-file url_next="$(eval 'echo ${'${array_now}'_key}')" if [[ ${url_next} ]] && ((RECURSIVE)); then # Start new instance of kodiator and point it to the other URL # (do not remove the files with URL history) "$0" --no-cleanup "${dir_now}" "${url_next}" || echo "$url_sub: Continuing" fi ;; files) # Video URL link="$(eval 'echo ${'${array_now}'_progressiveDownloadURL}')" # File to save the link in (remove underscores, if any) file_name="$(eval 'echo ${'${array_now}'_label}')" file_name="${file_name//\//}" # Create the file if [[ ${file_name} && ${link} ]]; then echo "${link}" > "${dir_now}/${file_name}.strm" || error fi ;; images) # Skip all contens of "images" return ;; esac # Process the "sub arrays" for sub_array in "$@"; do # Skip empty variables (or declare will get upset) [[ -z $(eval 'echo ${'${array_now}_${sub_array}'}') ]] && continue # Check if it is an array if declare -p ${array_now}_${sub_array} | egrep -q '^declare -a'; then # Repeat this for every "sub array" # Note: Do this in a sub-shell so that our current variables # will stay unaffected eval '(recursive_read \ "${dir_next}" \ ${array_now}_${sub_array} \ ${'${array_now}_${sub_array}'[@]}\ )' || error fi done } lang_check() { # Arguments: LANGUAGE ( curl --silent "$lang_list_url" || error "Unable to download language list" ) | egrep -q '"code":"'$1'"' } lang_list() { # Make the language list readable # Note: This is probably not very failsafe # # sed 1: # Make newline at every opening bracket # where a new language starts # # sed 2: # Replace "name":"LANG" ... "code":"CODE" # with LANG CODE # # Sort it # # sed 3: # Switch LANG with CODE and vice versa # # Make a nice list with columns curl --silent "$lang_list_url" \ | sed 's/{/\n/g' \ | sed -n 's/.*"name":"\([^"]*\)".*"code":"\([^"]*\)".*/\1:\2/p' \ | sort \ | sed 's/\(.*\):\(.*\)/\2:\1/' \ | column -t -s : exit } # Clean up before exit trap cleanup SIGINT SIGTERM SIGHUP # Check requirements for command in sed egrep curl; do type $command &>/dev/null || error "This script requires $command" done # Check if sed is GNU or not sed --version | egrep -q "GNU sed" || cat <> "${history_file}" echo "$url_sub: Processing" # Download and process the file # Note: Here, a pipe is used. Commands in a pipe is run in a sub-shell. # Thus we can not keep the variables saved by these commands. # So we dump the output to a file and let parse_lines() deal with creating # the variables later. ( curl --silent "${url_root}${url_sub}${url_suffix}" || error "$url_sub: Download failed" ) | unsquash_file > "${temp_file}" || error # Process and save all values in variables parse_lines ${array_root} < "${temp_file}" || error [[ ${!array_root} ]] || error "$url_sub: Nothing to save" # Read the variables and create directories containing STRM-files. eval 'recursive_read \ "${dir_root}" \ ${array_root} \ ${'${array_root}'[@]}\ ' || error cleanup echo "$url_sub: Done"