#!/usr/bin/env bash
#root url
parent_url="https://elfquest.com/read/"
#Arrays for each comic on the webpage. Each index in each array relates to each other array
#lazy associative arrays
quests=("ELFQUEST" "SIEGE_AT_BLUE_MOUNTAIN" "KINGS_OF_THE_BROKEN_WHEEL" "WOLFRIDER" "DREAMTIME" "HIDDEN_YEARS" "SHARDS" "SEARCHER_AND_THE_SWORD" "THE_DISCOVERY")
#quests=( "WOLFRIDER" "DREAMTIME" "HIDDEN_YEARS" "SHARDS" "SEARCHER_AND_THE_SWORD" "THE_DISCOVERY")
#Number of issues for each comic
num_issues=( 21 8 9 1 1 29 16 1 1 )
#num_issues=( 1 1 29 16 1 1 )
directories=( "OQ" "SABM" "KOBW" "WR" "DTC" "HY" "SH" "SAS" "DISC" )
#directories=( "WR" "DTC" "HY" "SH" "SAS" "DISC" )
filenames=( "oq" "sabm" "kobw" "awr" "dtc" "hy" "sh" "sas" "disc" )
#filenames=( "awr" "dtc" "hy" "sh" "sas" "disc" )
#Lazy bools for oddball quests/comics
#Initially added to facilitate HIDDEN_YEARS which has 30 issues due to an issue #9.5
#Using an array in case this comes up again
#Using C-style 1 = true and 0 = false
#booleans=( 0 )
#bool_index=0
declare -A booleans
booleans["HY"]=0
#used for troubleshooting to help indicate arrays worked as intended
for i in ${!quests[@]}
do
echo Quest: ${quests[$i]} Issues: ${num_issues[$i]}
done
#Go through each quest/comic and then go through each issue of the comic
for i in ${!quests[@]}
do
echo ${quests[$i]}
curr_issue=1
#Go through each issue of the current comic/quest
while [ "$curr_issue" -le "${num_issues[$i]}" ]
do
#Start on page 0 which appears to always be cover page
curr_page=0
#echo "$curr_issue "
#If there's only one issue then there is no subdirectory
#Subdirectories are for each issue only
if [ ${num_issues[$i]} -eq 1 ]
then
# url="$parent_url${directories[$i]}-"
url="$parent_url${directories[$i]}/${filenames[$i]}-"
else
#Check if the current issue is < 10 and prepend with a 0 if so
if [ $curr_issue -lt 10 ]
then
url="$parent_url${directories[$i]}/${directories[$i]}0${curr_issue}/${filenames[$i]}0${curr_issue}-"
else
url="$parent_url${directories[$i]}/${directories[$i]}${curr_issue}/${filenames[$i]}${curr_issue}-"
fi
fi
#Specific check for the HIDDEN_YEARS comic edition 9.5
#NOTE - Bash does not handle floating point values (eg 9.5) and I'm not using awk or anything to work around it
#If the current issue is 9.5, the current comic is HIDDEN YEARS and the HY bool has NOT been set (so remains 0)
#Set URL accordingly and curr_isue to 9.5 (a string, not a float)
if [ $curr_issue -eq 10 -a ${directories[$i]} = "HY" -a ${booleans["HY"]} -eq 0 ]
then
url="$parent_url${directories[$i]}/${directories[$i]}09.5/${filenames[$i]}09.5-"
#Set first boolean index to 1
booleans["HY"]=1
curr_issue="9.5"
fi
#Wget each image until failure
#Susceptible to premature failure and so may not finish an issue. A few runs should be fine
#Wget options
#--no-clobber Don't overwrite existing files
#--continue Continue partial downloads
#--verbose Verbose output
#--timeout=5 Wait 5 seconds before moving on from timeout
#--directory-prefix Directory to store files in. Example - ELFQUEST/issue_1/
while wget --no-clobber --continue --verbose --timeout=5 --directory-prefix="${quests[$i]}/issue_$curr_issue/" "$url${curr_page}.jpg"
do
#increment to next page
((curr_page+=1))
#sleep for 2-9 seconds (excessive but safe in my experience)
sleep $((1 + $RANDOM % 3))
done
#NOTE - This will run on every run for better or worse. Comment it out if undesired
#Convert each issue into a pdf use ImageMagick's convert tool
#Check if convert is installed before attemping to make a pdf
which convert > /dev/null
if [ $? -eq 0 ]
then
#If appropriate pdfs directory does not exist, create it
if [ ! -d ${quests[$i]}/pdfs ]
then
mkdir ${quests[$i]}/pdfs
fi
if [ ${num_issues[$i]} -gt 1 ]
then
convert $(ls ${quests[$i]}/issue_$curr_issue/*jpg | sort -n -t - -k 2) ${quests[$i]}/pdfs/${quests[$i]}_$curr_issue.pdf
else
convert $(ls ${quests[$i]}/issue_$curr_issue/*jpg | sort -n -t - -k 2) ${quests[$i]}/pdfs/${quests[$i]}.pdf
fi
fi
#If the current issue is 9.5, the current comic is HIDDEN YEARS and the HY bool has been set to 1
#Then set curr_issue=10
#Else increment by 1
if [ $curr_issue = "9.5" -a ${directories[$i]} = "HY" -a ${booleans["HY"]} -eq 1 ]
then
curr_issue=10
else
#increment to next issue of current comic
((curr_issue+=1))
fi
done
done