Skip to content

Instantly share code, notes, and snippets.

@lovromazgon
Last active October 8, 2015 16:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lovromazgon/227e9d1caa148212c88f to your computer and use it in GitHub Desktop.
Save lovromazgon/227e9d1caa148212c88f to your computer and use it in GitHub Desktop.
Downloads the subtitles for all episodes of one season of a TV series - usage: ./subtitle-downloader.sh -s [season] {-l [ISO 639-1 language code] -e [comma separated episodes]} [TV series title]
#!/bin/bash
# Downloads the most popular subtitles for all episodes of one season of a TV series
#
# Flags:
# -s : define the season (required if 'u' is missing)
# -u : explicitly define the URL (required if 's' is missing)
# -l : define the language code ISO 639-1 (optional, default: sl)
# -e : define the episodes in a comma separated list (optional, default: all)
# -h : displays this message
#
# After the flags an argument is required, which defines the title of the TV series.
# The downloaded subtitles will be stored in a folder named \"[title] - subtitles\"
#
# -------------
#
# Sample usage:
# English subtitles for all episodes in season 1 of TV series Tyrant:
# ./subtitle-downloader.sh -s 1 \"Tyrant\"
#
# Slovenian subtitles for episodes 1, 2 and 3 in season 2 of TV series Prison break:
# ./subtitle-downloader.sh -s 2 -e 1,2,3 -l sl \"Prison break\"
#
# Subtitles from a supplied URL:
# ./subtitle-downloader.sh -u \"http://www.podnapisi.net/subtitles/search/manhattan-2014/Nts?sort=stats.downloads&order=desc\" \"Manhattan\"
SEARCH_URL='http://www.podnapisi.net/subtitles/search?sort=stats.downloads&order=desc'
NUMBER_REGEX='^[0-9]+$'
SUBTITLES_FOLDER=''
SEASONS_PARAMETER=''
EPISODES_PARAMETER=''
KEYWORD_PARAMETER=''
LANGUAGE_PARAMETER=''
URL=''
function processInputVariables {
e=''
s=''
l=''
while getopts ":e:l:s:u:h" opt; do
case $opt in
e)
e=$OPTARG
;;
h)
echo "
*** SUBTITLE DOWNLOADER ***
Downloads the most popular subtitles for all episodes of one season of a TV series
Flags:
-s : define the season (required if 'u' is missing)
-u : explicitly define the URL (required if 's' is missing)
-l : define the language code ISO 639-1 (optional, default: sl)
-e : define the episodes in a comma separated list (optional, default: all)
-h : displays this message
After the flags an argument is required, which defines the title of the TV series.
The downloaded subtitles will be stored in a folder named \"[title] - subtitles\"
-------------
Sample usage:
English subtitles for all episodes in season 1 of TV series Tyrant:
./subtitle-downloader.sh -s 1 \"Tyrant\"
Slovenian subtitles for episodes 1, 2 and 3 in season 2 of TV series Prison break:
./subtitle-downloader.sh -s 2 -e 1,2,3 -l sl \"Prison break\"
Subtitles from a supplied URL:
./subtitle-downloader.sh -u \"http://www.podnapisi.net/subtitles/search/manhattan-2014/Nts?sort=stats.downloads&order=desc\" \"Manhattan\"
"
exit 0
;;
l)
l=$OPTARG
;;
s)
s=$OPTARG
;;
u)
URL=$OPTARG
;;
\?)
echo "Invalid option: -$OPTARG" >&2; exit 1
;;
:)
echo "Option -$OPTARG requires an argument." >&2; exit 1
;;
esac
done
if [ -z "$s" ] && [ -z "$URL" ]; then
echo "error: you did not provide a season (-s) or an URL (-u)" >&2; exit 1
fi
if [ -z "$l" ]; then
l='en'
fi
shift $((OPTIND-1))
if [ -z "$1" ]; then
echo "error: you did not provide a keyword (series title)" >&2; exit 1
fi
SUBTITLES_FOLDER="$1 - subtitles"
KEYWORD_PARAMETER="&keywords=$1"
SEASONS_PARAMETER="&seasons=!$s"
LANGUAGE_PARAMETER="&language=!$l"
while IFS=',' read -ra EPISODES_ARRAY; do
for ep in "${EPISODES_ARRAY[@]}"; do
if ! [[ $ep =~ $NUMBER_REGEX ]] ; then
echo "error: $ep is not a number" >&2; exit 1
fi
EPISODES_PARAMETER="$EPISODES_PARAMETER&episodes=$ep"
done
done <<< "$e"
}
function buildSearchUrl {
URL="$SEARCH_URL$LANGUAGE_PARAMETER$KEYWORD_PARAMETER$SEASONS_PARAMETER$EPISODES_PARAMETER"
}
function containsElement {
local e
for e in "${@:2}"; do [[ "$e" == "$1" ]] && return 0; done
return 1
}
function downloadSubtitles {
htmlResults=$1
rows=$(grep -o '<tr class="subtitle-entry".*</tr>' <<< $htmlResults | sed -e 's/<\/tr>/\'$'\n/g')
subtitlesArray=()
while read r; do
temp=$(grep -oP '(?<=<td> ).*?(?= </td>)' <<< $r | sed '6q;d')
episode=$(sed -e 's,.*<a[^>]*>\([^<]*\)</a>.*,\1,g' <<< $temp)
link=$(sed -n 's/.*data-href="\([^"]\+\).*/\1/p' <<< $r)
if [ -z "${subtitlesArray[$episode]}" ]; then
if ! [[ $episode =~ $NUMBER_REGEX ]] ; then
echo "Episode '$episode' is not a number (probably for the whole season) - not downloading"
continue
fi
echo "Downloading subtitles for episode $episode"
wget -q -P "$SUBTITLES_FOLDER" "http://www.podnapisi.net$link/download"
subtitlesArray[$episode]=1
else
echo "Episode $episode already downloaded"
fi
done <<< "$rows"
}
function extractAndDeleteZipFiles {
echo "Extracting subtitles..."
unzip "$SUBTITLES_FOLDER/*" -d "$SUBTITLES_FOLDER"
echo "Deleting zip files..."
find "$SUBTITLES_FOLDER" -type f -name 'download*' -delete
}
processInputVariables "$1" "$2" "$3" "$4" "$5" "$6" "$7"
if [ -z "$l" ]; then
buildSearchUrl
echo "keyword: $KEYWORD_PARAMETER"
echo "episodes: $EPISODES_PARAMETER"
else
echo "using supplied url"
fi
echo "$URL"
htmlResults=$(curl --header "Accept-Language: sl,en-US;q=0.7,en;q=0.3" -s -L "$URL")
downloadSubtitles "$htmlResults"
extractAndDeleteZipFiles
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment