#!/bin/sh
# scrape iPlayer programme URLs from a BBC web page

# args: [--queue|-q] iplayer_series_url

mung_url()
{ # prefix
    local url
    while read url; do
        url=${url##href=\"};
        echo $1${url%%\"}
    done
}

case $1 in 

--queue|-q) 
       if which qtube >/dev/null; then
               qqq() {
                       while read -r line; do
                               qtube "$@" "$line"
                        done
               }
       else
               printf "No qtube program is installed; listing qtube commands\n" >&2
               qqq() {
                       while read -r line; do
                               echo qtube "$@" $(printf "'%s'" "$line")
                        done
               }
       fi
       shift
       ;;

--help|-h) { 
       printf "Usage:\n\n%s [--queue|-q] iplayer_series_url\n\n" "${0##*/}"
       printf "Extract iPlayer programme URLs from series page and pass to youtube-dl.\n\n"
       printf "With queue option, instead try to queue each URL for download.\n\n"
       } 1>&2
       exit
       ;; 

*)     qqq() { youtube -a -; }
       ;;
       
esac

# get BBC's base address
bbc="$1"; bbc="${bbc%%/iplayer*}"

# parse the web page for episode URLs, extract and prepare them for youtube-dl
# curl: -k insecure, needed due to Humax's old SSL libs, -s silent, -S show errors anyway
# grep: -o print matching substring, -E match extended regular expression
curl -k -s -S $1 | grep -oE "href=('|\")/iplayer/episode/[^'\"]+\\1" | mung_url $bbc | \
  sort | uniq | qqq
