2010-11-05, 21:55
OK I was trying to get Banners for my TV Shows. I threw this together and thought I would share it. It can be built upon with a few little tweaks. I will probably work on getting it to recuse search each TV folders and put the banner.jpg in that folder. currently it just downloads it to a single folder (TVDIR variable).
Have a great day.
Thanks,
super sobbie
Code:
#!/bin/bash
#############
## VARIABLES
#############
# Make all input lowercase
SEARCH="$(echo ${1} | tr A-Z a-z)"
resultLink="" # Variable for passing Strings in getResultLink function
bannerLink="" # Variable for passing Strings in getBannerLink function
TVDIR="/tmp" #location of TV Root Directory
# Setup for acpting different language searchs
#LANGUAGE="English"
#if "${2}" then LANGUAGE="$(echo ${1} | tr A-Z a-z)"
# Get a dump of the search from thetvdb.com
lynx -dump "http://thetvdb.com/?string=${SEARCH}&searchseriesid=&tab=listseries&function=Search" > /tmp/searchResults.txt
# Create Array of the options returned
i=1
IFS="
"
for line in `grep English /tmp/searchResults.txt`; do
tmp="$(echo ${line} | tr A-Z a-z | sed -e 's/^[ \t]*//' | sed -e 's/english/:/' | cut -d':' -f1 | sed -e 's/ \{1,\}$//')"
# echo "line 31 - $i: $tmp"
ENGLISHARRAY[$i]=$tmp
(( i++ ))
done
#############
## FUNCTIONS
#############
# listOptions - output of all results and there array number
function listOptions {
for (( i = 0 ; i < ${#ENGLISHARRAY[@]} ; i++ )); do
if [[ ${ENGLISHARRAY[$i]} =~ "$SEARCH" ]]
then
echo "$i: ${ENGLISHARRAY[$i]}"
fi
done
}
function getResultLink() {
if [ -z "$1" ] # Is parameter #1 zero length?
then
echo "-Parameter #1 is zero length.-" # Or no parameter passed.
else
linknum="$(echo ${ENGLISHARRAY[$1]} | sed 's/\[//g;s/\].*//g')"
# echo "The Link Number is: $linknum" ; echo
linktmp="$(grep "$linknum. " /tmp/searchResults.txt)"
# echo "The Linktmp is: $linktmp" ; echo
resultLink="$(echo $linktmp | sed 's/^[^h]*//g')"
# echo "***********"
# echo "$resultLink"
# echo
fi
}
function getBannerLink() {
if [ -z "$1" ]
then
echo "-Parameter #1 is zero length.-" # Or no parameter passed.
else
linktmp="$(lynx -dump "$1" | grep -v 'tab=DMCA' | grep -m1 -i 'banners/graphical')"
# echo "$linktmp"
if [ -z "$linktmp" ]
then
echo "There are no Banners for this TV Series"
exit
fi
bannerLink="$(echo $linktmp | sed 's/^[^h]*//g')"
# echo "***********"
# echo "$bannerLink"
# echo
fi
}
function downloadBanner() {
wget -O "$TVDIR/banner.jpg "$1"
}
#############
##MAIN
#############
#count=${#ENGLISHARRAY[@]}
#echo "******"
#echo "$count"
if [ "${#ENGLISHARRAY[@]}" -gt 1 ]
then
echo
listOptions
echo "Choose a matching number and hit enter: "
read ANSWER
getResultLink "$ANSWER"
else
getResultLink 1
fi
getBannerLink "$resultLink"
downloadBanner "$bannerLink"
#Clean temp files and variables
resultLink=""
bannerLink=""
ANSWER=""
rm /tmp/searchResults.txt
Have a great day.
Thanks,
super sobbie