2014-07-29 14:37:29 +02:00
#!/bin/bash
2014-09-11 17:36:43 +02:00
### Exit codes:
2014-08-03 16:51:21 +02:00
# 1 : General error (invalid argument option, missing parameter)
# 2 : Specified language not recognized
# 3 : Aborted (Ctrl+C)
# 10 : Series not found in TvDB
# 11 : Series not found in EPG
2014-09-11 17:36:43 +02:00
# 12 : Several possible series found
2014-08-03 16:51:21 +02:00
# 20 : No info for this episode found
# 21 : No episode title found in EPG
# 40 : Downloading EPG data failed
# 41 : Downloading list of episodes from TvDB failed
2014-07-27 02:59:45 +02:00
2014-09-11 17:36:43 +02:00
### What this program does
# Analyze the file name
# Ask TvDB or cache for the ID of the series
# Get EPG from OTR
# Search the episode title in EPG
# Get a list of all episodes from TvDB
# Search the episode in this list
# Print file name with episode and series number
2014-07-29 14:37:29 +02:00
##########
# Config #
##########
2014-07-27 02:59:45 +02:00
apikey = "2C9BB45EFB08AD3B"
2015-03-03 16:05:21 +01:00
productname = "SaneRename for OTR (ALPHA) v0.4"
2014-07-29 14:37:29 +02:00
lang = "de"
2014-10-29 13:01:39 +01:00
debug = false
2015-11-25 09:03:56 +01:00
cache = true
2014-07-29 14:37:29 +02:00
2014-07-27 02:59:45 +02:00
2014-07-29 14:37:29 +02:00
##########
# Script #
##########
# Print only of not silent
2014-07-27 19:48:56 +02:00
function eecho {
if [ -z " $silent " ] ; then
echo " $1 " " $2 " " $3 "
fi
}
2014-08-19 00:28:38 +02:00
function logNexit {
2014-09-04 14:09:02 +02:00
str = " $1 - $file_name - $series_title "
2015-03-12 21:52:58 +01:00
if [ -f " $PwD /log " ] && ! grep -q " $str " " $PwD /log " ; then
2014-09-04 14:09:02 +02:00
echo " $str " >> " $PwD /log "
fi
2014-08-19 00:28:38 +02:00
exit $1
}
2014-07-30 20:26:31 +02:00
# trap ctrl-c and call ctrl_c()
trap ctrl_c INT
function ctrl_c( ) {
if $wget_running ; then
rm -f $wget_file
fi
2014-08-03 16:51:21 +02:00
exit 3
2014-07-30 20:26:31 +02:00
}
wget_running = false;
2014-07-29 14:37:29 +02:00
# Parse the parameters
function funcParam {
2015-11-25 09:03:56 +01:00
while getopts "dcf:l:s" optval; do
2014-07-29 14:37:29 +02:00
case $optval in
2014-10-29 13:01:39 +01:00
"d" ) # Enable debugging
debug = true; ;
2015-11-25 09:03:56 +01:00
"c" ) # Disable series ID cache
cache = false; ;
2014-07-29 14:37:29 +02:00
"f" ) # Path to file
path = " $OPTARG " ; ;
"s" ) # Silent switch
silent = 1; ;
"l" ) # Language
case " $OPTARG " in
de*)
lang = "de" ; ;
2015-04-07 22:18:19 +02:00
en*) ; &
2014-07-29 14:37:29 +02:00
us*)
lang = "en" ; ;
fr*)
lang = "fr" ; ;
*)
echo " Language not recognized: $OPTARG "
2014-08-03 16:51:21 +02:00
exit 2; ;
2014-07-29 14:37:29 +02:00
esac ; ;
"?" ) # Help
2015-11-25 09:03:56 +01:00
echo " Usage: $0 -f pathToAvi [-c] [-s] [-l LANG] "
2015-04-07 22:18:19 +02:00
echo
2015-11-25 09:03:56 +01:00
echo "-c disables the series ID cache on the local drive"
2015-04-07 22:18:19 +02:00
echo "-s makes this script to only output the filename"
echo "-l lets you search TheTVDB in a different language"
2014-07-29 14:37:29 +02:00
exit; ;
":" )
echo " No argument value for option $OPTARG "
2014-08-03 16:51:21 +02:00
exit 1; ;
2014-07-29 14:37:29 +02:00
esac
done
}
# Print the header
function funcHeader {
eecho " :: $productname "
2015-03-03 15:11:46 +01:00
eecho " :: by Leroy Foerster & Jonny007-MKD"
2014-07-29 14:37:29 +02:00
eecho
}
# Get title, date and time
function funcAnalyzeFilename {
2014-10-29 13:01:39 +01:00
if $debug ; then echo -e "\033[36mfuncAnalyzeFilename\033[37m" ; fi ;
2014-10-26 21:04:17 +01:00
local tmp;
2015-03-03 16:05:21 +01:00
# Remove series and episode information
2015-03-22 17:14:50 +01:00
if [ [ " $file_name " = = S[ 0-9] [ 0-9] _E[ 0-9] [ 0-9] _* ] ] ; then # S00_E00_Series_
2015-03-03 16:05:21 +01:00
episode_season = " ${ file_name : 1 : 2 } " # Retreive information
episode_number = " ${ file_name : 5 : 2 } "
episode_season = ${ episode_season #0 } # Remove leading 0
episode_number = ${ episode_number #0 }
file_name = " ${ file_name : 8 } "
fi
2014-07-29 14:37:29 +02:00
# Split filename into words, divided by _ (underscores)
file = " ${ file_name //_/ } "
2014-10-26 21:04:17 +01:00
file_suffix = " ${ file_name ##*. } " # Get file suffix
tmp = " ${ file_name //. $file_suffix } "
case " ${ tmp ##*. } " in # Prepend special suffixes
HQ| HD)
file_suffix = " ${ tmp ##*. } . $file_suffix " ; ;
esac
2014-07-29 14:37:29 +02:00
firstField = " ${ file %% * } " # Get the first word
test $firstField -eq 0 2>/dev/null # If first word is a number -> cutlist id
if [ $? -ne 2 ] ; then
file = ${ file ## $firstField } # remove it
2014-07-29 10:45:19 +02:00
fi
2014-07-29 14:37:29 +02:00
file_title = ${ file %% [0-9][0-9].* } # Cut off everything after the title: date, hour, sender, ...
file_sender = ${ file ##*-[0-9][0-9] } # Cut off everything bevor the sender: title, date, time, ...
file_date = ${ file %% $file_sender } # Cut off the sender
file_date = ${ file_date ## $file_title } # Cut off the title, now we do have the date and time
file_time = ${ file_date ##* }
file_date = ${ file_date %% * }
file_dateInv = $( date +%d.%m.%Y --date= " ${ file_date //./- } " ) # Convert YY.MM.DD to DD.MM.YY
2014-09-11 17:36:43 +02:00
file_time = ${ file_time /-/ : } # Convert HH-MM to HH:MM
2014-07-29 10:45:19 +02:00
2015-03-27 20:35:19 +01:00
if [ [ " $file_title " = = *S[ 0-9] [ 0-9] E[ 0-9] [ 0-9] ] ] ; then # Series_S00E00_
episode_season = " ${ file_title : (-5) : 2 } " # Retreive information
episode_number = " ${ file_title : (-2) : 2 } "
episode_season = ${ episode_season #0 } # Remove leading 0
episode_number = ${ episode_number #0 }
file_title = " ${ file_title : 0 :- 7 } "
fi
2014-07-29 14:37:29 +02:00
eecho -e " Work dir:\t $PwD "
eecho -e " Datum:\t $file_dateInv "
eecho -e " Uhrzeit:\t $file_time "
2014-09-11 17:36:43 +02:00
funcConvertName " $file_title "
eecho -e " Titel:\t $tmp "
}
function funcConvertName {
2014-10-29 13:01:39 +01:00
if $debug ; then echo -e " \033[36mfuncConvertName $1 \033[37m " ; fi ;
2014-09-11 17:36:43 +02:00
tmp = " $1 "
tmp = ${ tmp // s / \' s } # Replace a single s with 's
2014-10-29 13:03:35 +01:00
if [ " $langCurrent " = = "de" ] ; then
2014-09-11 17:36:43 +02:00
tmp = ${ tmp //Ae/Ä } # Replace umlauts
tmp = ${ tmp //Oe/Ö }
tmp = ${ tmp //Ue/Ü }
tmp = ${ tmp //ae/ä }
tmp = ${ tmp //oe/ö }
tmp = ${ tmp //ue/ü }
fi
2014-07-29 14:37:29 +02:00
}
# Get the series ID from TvDB (needed to fetch episodes from TvDB)
function funcGetSeriesId {
2014-10-29 13:01:39 +01:00
if $debug ; then echo -e "\033[36mfuncGetSeriesId\033[37m" ; fi ;
2014-07-30 20:26:31 +02:00
local tmp;
2015-11-25 09:03:56 +01:00
if $cache && [ -f " $PwD /series.cache " ] ; then # Search the series cache
2014-09-19 22:14:36 +02:00
funcGetSeriesIdFromCache " $file_title "
2014-10-28 16:27:48 +01:00
if [ -z " $series_id " ] ; then # and search the cache with translation
funcConvertName " $file_title "
funcGetSeriesIdFromCache " $tmp "
fi
2014-07-30 20:26:31 +02:00
fi
if [ -z " $series_id " ] ; then # Otherwise ask TvDB whether they do know the series
2015-11-25 08:46:00 +01:00
funcGetSeriesIdFromTvdb " $file_title " false
2014-09-19 21:54:42 +02:00
fi
2014-09-19 22:14:36 +02:00
if [ -z " $series_id " ] ; then # Otherwise ask TvDB with translation
2014-10-29 13:55:31 +01:00
funcConvertName " $file_title "
2015-11-25 08:46:00 +01:00
funcGetSeriesIdFromTvdb " $tmp " true
2014-07-30 20:26:31 +02:00
fi
if [ -z " $series_id " ] ; then # This series was not found anywhere :(
eecho -e " TVDB:\tSeries not found!"
2014-08-19 00:28:38 +02:00
logNexit 10
2014-07-30 20:26:31 +02:00
fi
if [ -n " $series_alias " ] ; then
eecho -e " \t\t\t\tAlias: $series_alias "
2014-07-29 14:37:29 +02:00
fi
2014-07-30 20:26:31 +02:00
}
# Search the series.cache file for this series and get TvDB series id
function funcGetSeriesIdFromCache {
2014-10-29 13:01:39 +01:00
if $debug ; then echo -e " \033[36mfuncGetSeriesIdFromCache $1 \033[37m " ; fi ;
2014-07-30 20:26:31 +02:00
local title;
local tmp;
2014-09-19 22:14:36 +02:00
title = " $1 " ;
2014-07-30 20:26:31 +02:00
while true; do
2014-09-19 22:14:36 +02:00
series_id = " $( grep " $title | " " $PwD /series.cache " ) " # Search for this title in the cache
2014-07-30 20:26:31 +02:00
if [ -n " $series_id " ] ; then # Stop if we have found something
2014-09-11 17:36:43 +02:00
series_title_file = " ${ series_id %|_|* } "
series_title_tvdb = " ${ series_id #*|_| } "
series_title_tvdb = " ${ series_title_tvdb %|#|* } "
series_id = " ${ series_id ##*|#| } "
2014-07-30 20:26:31 +02:00
eecho -e " Cache:\tSeries found.\tID:\t $series_id "
2014-09-11 17:36:43 +02:00
eecho -e " \t \tName:\t $series_title_tvdb "
2014-07-30 20:26:31 +02:00
break;
fi
tmp = " ${ title % * } " # Shorten the title by one word
if [ ${# tmp } -le 4 ] || [ " $tmp " = = " $title " ] ; then # Too short or was not shortened
break;
fi
2014-10-15 14:51:09 +02:00
title = " $( echo $tmp | sed -e 's/^[^a-zA-Z0-9]*//' -e 's/ *$//' ) "
2014-07-30 20:26:31 +02:00
done
}
# Search the TvDB for this series and get TvDB series id
function funcGetSeriesIdFromTvdb {
2014-10-29 13:01:39 +01:00
if $debug ; then echo -e " \033[36mfuncGetSeriesIdFromTvdb $1 \033[37m " ; fi ;
2014-07-30 20:26:31 +02:00
local title;
local tmp;
2015-11-25 08:46:00 +01:00
local lastChance;
2015-11-25 09:03:56 +01:00
local cacheLine;
2014-09-19 21:54:42 +02:00
title = " $1 " ;
2015-11-25 08:46:00 +01:00
lastChance = " $2 " ;
2014-10-29 14:33:05 +01:00
2014-07-30 20:26:31 +02:00
while true; do
series_db = " https://www.thetvdb.com/api/GetSeries.php?seriesname= ${ title } &language= $lang "
wget_file = " $PwD /series.xml "
wget_running = true;
2014-10-29 14:33:05 +01:00
if $debug ; then echo -e " \033[36mwget \" $series_db \" -O \" $wget_file \"\033[37m " ; fi ;
2014-07-30 20:26:31 +02:00
wget " $series_db " -O " $wget_file " -o /dev/null
2014-07-29 14:37:29 +02:00
error = $?
2015-03-03 16:09:38 +01:00
wget_running = false;
2014-07-29 14:37:29 +02:00
if [ $error -ne 0 ] ; then
2014-08-19 00:28:38 +02:00
eecho -e " \t\t\tDownloading $series_db failed \(Exit code: $error \)! "
2014-07-30 20:26:31 +02:00
fi
2014-09-11 17:36:43 +02:00
2015-11-25 08:46:00 +01:00
if $debug ; then echo -e " grep -Ei -m 1 -B 3 -A 1 \"> ${ title // / \\ W + } <\" \" $wget_file \") " ; fi ;
2015-09-23 17:39:17 +02:00
tmp = " $( grep -Ei -m 1 -B 3 -A 1 " > ${ title // / \\ W + } < " " $wget_file " ) "
2014-08-18 22:35:24 +02:00
if [ ${# tmp } -eq 0 ] ; then # No series with this name found
2015-11-25 08:46:00 +01:00
if $debug ; then echo -e " grep -Pzo --binary-files=text \"(?s)> $langCurrent </language>\n<SeriesName>\" \" $wget_file \") " ; fi ;
tmp = " $( grep -Pzo --binary-files= text " (?s)> $langCurrent </language>\n<SeriesName> " " $wget_file " ) " # Let's get all series from the query
2014-08-18 22:35:24 +02:00
if [ $( echo " $tmp " | wc -l) -eq 1 ] ; then # If we only found one series
2015-11-25 08:46:00 +01:00
if $debug ; then echo -e " grep -Pzo --binary-files=text \"(?s)<Series>.*? $langCurrent </language>.*?</SeriesName>\" \" $wget_file \") " ; fi ;
2015-09-23 17:39:17 +02:00
tmp = " $( grep -Pzo --binary-files= text " (?s)<Series>.*? $langCurrent </language>.*?</SeriesName> " " $wget_file " ) " # Lets use this one
2014-08-18 22:35:24 +02:00
else
2015-11-25 08:46:00 +01:00
eecho -e " TvDB: $( echo " $tmp " | wc -l) series found with title \" $title \" "
if $lastChance ; then # If we really had to find a value now
logNexit 12
else # If we may try something else
return
fi
2014-08-18 22:35:24 +02:00
fi
fi
2014-07-30 20:26:31 +02:00
if [ -n " $tmp " ] ; then
series_id = $( echo " $tmp " | grep "seriesid>" )
2014-09-11 17:36:43 +02:00
series_title_file = " $file_title "
series_title_tvdb = $( echo " $tmp " | grep "SeriesName>" ) # Get series name from TvDB
series_alias = $( echo " $tmp " | grep "AliasNames>" )
2014-07-30 20:26:31 +02:00
series_id = ${ series_id %<* } # Remove XML tags
series_id = ${ series_id #*> }
2014-09-11 17:36:43 +02:00
series_title_tvdb = ${ series_title_tvdb %<* }
series_title_tvdb = ${ series_title_tvdb #*> }
2014-07-30 20:26:31 +02:00
series_alias = ${ series_alias %<* }
series_alias = ${ series_alias #*> }
2015-09-23 17:39:17 +02:00
if [ -n " $series_id " -a -n " $series_title_tvdb " ] ; then
2015-11-25 09:03:56 +01:00
cacheLine = " $file_title |_| $series_title_tvdb |#| $series_id "
if [ -f " $PwD /series.cache " ] ; then
tmp = " $( grep " $cacheLine " " $PwD /series.cache " ) "
else
tmp =
fi
if [ -z " $tmp " ] ; then
2015-11-25 09:37:20 +01:00
echo " $cacheLine " >> " $PwD /series.cache "
2015-11-25 09:03:56 +01:00
fi
2015-09-23 17:39:17 +02:00
eecho -e " TVDB:\tSeries found.\tID: $series_id "
eecho -e " \t \tName: $series_title_tvdb "
fi
2014-07-30 20:26:31 +02:00
break
2014-07-29 14:37:29 +02:00
fi
2015-09-23 17:39:17 +02:00
tmp = " ${ title % * } " # Shorten the title by one word
if [ ${# tmp } -le 4 ] || [ " $tmp " = = " $title " ] ; then # Too short or was not shortened
break;
2014-07-29 14:37:29 +02:00
fi
2015-09-23 17:39:17 +02:00
title = " $( echo $tmp | sed -e 's/^[^a-zA-Z0-9]*//' -e 's/ *$//' ) "
2014-07-30 20:26:31 +02:00
done
2014-07-29 14:37:29 +02:00
}
# Get the EPG from OnlineTvRecorder and get the title of the episode
function funcGetEPG {
2014-10-29 13:01:39 +01:00
if $debug ; then echo -e "\033[36mfuncGetEPG\033[37m" ; fi ;
2014-07-29 14:37:29 +02:00
# Download OTR EPG data and search for series and time
2014-07-30 20:26:31 +02:00
wget_file = " $PwD /epg- ${ file_date } .csv "
2014-10-15 15:08:25 +02:00
if [ -f " $wget_file " ] ; then
2015-11-25 09:47:14 +01:00
wget_file_date = $( stat --format= %Z " $wget_file " )
2014-10-15 15:08:25 +02:00
if [ $(( $( date +%s) - $wget_file_date )) -gt $(( 60 * 60 * 24 * 7 * 2 )) ] ; then # if file is older than 2 weeks
2015-11-25 09:47:14 +01:00
if $debug ; then echo " Deleting file with timestamp $wget_file_date ( $wget_file ) " ; fi ;
2014-10-15 15:08:25 +02:00
rm " $wget_file "
2014-10-26 20:09:03 +01:00
elif [ $( stat --format= %s " $wget_file " ) -eq 0 ] ; then # if file is empty
2015-11-25 09:47:14 +01:00
if $debug ; then echo " Deleting empty file $wget_file " ; fi ;
2014-10-26 20:09:03 +01:00
rm " $wget_file "
2014-10-15 15:08:25 +02:00
fi
fi
2014-07-30 20:26:31 +02:00
if [ ! -f " $wget_file " ] ; then # This EPG file does not exist
2014-08-06 18:04:41 +02:00
#rm -f ${PwD// /\\ }/epg-*.csv 2> /dev/null # Delete all old files
2014-07-29 14:37:29 +02:00
epg_csv = " https://www.onlinetvrecorder.com/epg/csv/epg_20 ${ file_date //./_ } .csv "
2014-07-30 20:26:31 +02:00
wget_running = true;
2014-10-29 14:33:05 +01:00
if $debug ; then echo -e " \033[36mwget \" $epg_csv \" -O \" $wget_file \"\033[37m " ; fi ;
2014-07-30 20:26:31 +02:00
wget " $epg_csv " -O " $wget_file " -o /dev/null # Download the csv
2014-07-29 14:37:29 +02:00
error = $?
2015-03-03 16:09:38 +01:00
wget_running = false;
2014-07-29 14:37:29 +02:00
if [ $error -ne 0 ] ; then
eecho " Downloading $epg_csv failed (Exit code: $error )! "
2014-08-19 00:28:38 +02:00
logNexit 40
2014-07-29 14:37:29 +02:00
fi
2014-09-04 13:55:39 +02:00
iconv -f LATIN1 -t utf8 " $wget_file " -o " ${ wget_file } .iconv "
mv " ${ wget_file } .iconv " " $wget_file "
2014-07-29 14:37:29 +02:00
fi
2014-10-15 14:51:09 +02:00
epg = " $( grep -i " $series_title_file " " $wget_file " | grep " ${ file_time } " ) " # Get the line with the movie
2014-07-29 14:37:29 +02:00
if [ -z " $epg " ] ; then
2014-09-11 17:36:43 +02:00
funcConvertName " $series_title_file "
2014-10-15 14:51:09 +02:00
epg = " $( grep -i " $tmp " " $wget_file " | grep " ${ file_time } " ) " # Get the line with the movie
2014-09-11 17:36:43 +02:00
if [ -z " $epg " ] ; then
2014-10-15 14:51:09 +02:00
epg = " $( grep -i " $series_title_tvdb " " $wget_file " | grep " ${ file_time } " ) " # Get the line with the movie
2014-09-11 17:36:43 +02:00
if [ -z " $epg " ] ; then
eecho -e " EPG:\tSeries \" $series_title_file \" not found in EPG data " # This cannot happen :)
logNexit 11
fi
fi
2014-07-29 14:37:29 +02:00
fi
# Parse EPG data using read
2015-03-03 15:42:00 +01:00
OLDIFS = $IFS
2014-07-29 14:37:29 +02:00
IFS = ";"
2014-08-03 16:51:21 +02:00
while read epg_id epg_start epg_end epg_duration epg_sender epg_title epg_type epg_text epg_genre epg_fsk epg_language epg_weekday epg_additional epg_rpt epg_downloadlink epg_infolink epg_programlink; do
if [ [ " $epg_start " = = *$file_time * ] ] ; then # Use the one with the correct start time
break
fi
done <<< " $epg "
2014-07-29 14:37:29 +02:00
IFS = $OLDIFS
}
# Get the title of the episode from description in EPG using $1 as delimiter to the real description
2015-03-03 15:37:25 +01:00
function funcGetEpisodeTitleFromEpg {
if $debug ; then echo -e " \033[36mfuncGetEpisodeTitleFromEpg \" $1 \" \" $2 \"\033[37m " ; fi ;
2014-09-11 17:36:43 +02:00
local delimiter;
2014-10-29 13:55:31 +01:00
local delimiter2;
delimiter1 = " $1 "
delimiter2 = " $2 "
2014-10-29 13:01:39 +01:00
2014-10-29 13:55:31 +01:00
episode_title = " ${ epg_text %% $delimiter1 * } " # Text begins with episode title, cut off the rest
if [ -n " $delimiter2 " ] ; then
episode_title = " ${ episode_title ##* $delimiter2 } " # Cut of anything before the second delimiter
fi
episode_title = " $( echo ${ episode_title } | sed -e 's/^[^a-zA-Z0-9]*//' -e 's/ *$//' ) "
2014-09-11 17:36:43 +02:00
episode_title = " ${ episode_title # $series_title_file } " # Get the title without the series title
episode_title = " $( echo ${ episode_title # $series_title_tvdb } | sed -e 's/^[^a-zA-Z0-9]*//' -e 's/ *$//' ) " # Get the title without the series title
2014-07-29 14:37:29 +02:00
if [ -z " $episode_title " ] ; then
2014-10-26 14:00:44 +01:00
eecho -e " EPG:\tNo episode title found"
2014-08-03 16:51:21 +02:00
else
2014-10-26 14:00:44 +01:00
episode_title = " $( echo $episode_title | sed -e 's/^[^a-zA-Z0-9]*//' -e 's/ *$//' ) "
eecho -e " EPG:\tEpisode title:\t $episode_title " # We found some title :)
2014-07-29 14:37:29 +02:00
fi
}
2014-09-11 17:36:43 +02:00
# Download episodes list from TvDB, language as argument
2015-03-03 15:37:25 +01:00
function funcDownloadEpisodesFile {
if $debug ; then echo -e "\033[36mfuncDownloadEpisodesFile\033[37m" ; fi ;
2014-10-28 14:56:59 +01:00
wget_file = " $PwD /episodes- ${ series_id } - ${ langCurrent } .xml "
2014-10-15 15:08:25 +02:00
if [ -f " $wget_file " ] ; then
2015-11-25 09:47:14 +01:00
wget_file_date = $( stat --format= %Z " $wget_file " )
if [ $(( $( date +%s) - $wget_file_date )) -gt $(( 60 * 60 * 24 * 2 )) ] ; then # if file is older than 2 days
if $debug ; then echo " Deleting file with timestamp $wget_file_date ( $wget_file ) " ; fi ;
2014-10-15 15:08:25 +02:00
rm " $wget_file "
2014-10-26 20:09:03 +01:00
elif [ $( stat --format= %s " $wget_file " ) -eq 0 ] ; then # if file is empty
2015-11-25 09:47:14 +01:00
if $debug ; then echo " Deleting empty file $wget_file " ; fi ;
2014-10-26 20:09:03 +01:00
rm " $wget_file "
2014-10-15 15:08:25 +02:00
fi
fi
2014-07-31 20:42:42 +02:00
if [ ! -f " $wget_file " ] ; then
# Download Episode list of series
2014-10-28 14:56:59 +01:00
episode_db = " https://www.thetvdb.com/api/ $apikey /series/ $series_id /all/ $langCurrent .xml "
2014-07-31 20:42:42 +02:00
wget_running = true;
2014-10-29 14:33:05 +01:00
if $debug ; then echo -e " \033[36mwget \" $episode_db \" -O \" $wget_file \"\033[37m " ; fi ;
2014-07-31 20:42:42 +02:00
wget $episode_db -O " $wget_file " -o /dev/null
error = $?
2015-03-03 16:09:38 +01:00
wget_running = false;
2014-07-31 20:42:42 +02:00
if [ $error -ne 0 ] ; then
eecho " Downloading $episode_db failed (Exit code: $error )! "
2014-08-19 00:28:38 +02:00
logNexit 41
2014-07-31 20:42:42 +02:00
fi
2014-07-27 02:59:45 +02:00
fi
2014-07-29 14:37:29 +02:00
}
2014-07-29 18:21:22 +02:00
# Get the information from episodes list of TvDB
2015-03-03 15:37:25 +01:00
function funcGetEpisodeInfoByTitle {
if $debug ; then echo -e "\033[36mfuncGetEpisodeInfoByTitle\033[37m" ; fi ;
2015-03-12 22:19:10 +01:00
local i;
local tmp; # Some tmp variable
local title; # The current string of the title
local title_not_converted; # Whether the title was converted
local remove_begin; # How many words shall be removed from the beginnig
2014-09-11 17:36:43 +02:00
title = " $episode_title " # Use coded version of episode title
funcConvertName " $episode_title "
if [ " $episode_title " != " $tmp " ] ; then # Save state to change to decoded version later
2015-03-12 22:19:10 +01:00
title_not_converted = true;
2014-09-11 17:36:43 +02:00
else
2015-03-12 22:19:10 +01:00
title_not_converted = false;
2014-09-11 17:36:43 +02:00
fi
2014-10-28 14:56:59 +01:00
wget_file = " $PwD /episodes- ${ series_id } - ${ langCurrent } .xml "
2015-03-12 22:19:10 +01:00
while true; do # Loop: Convert title
for remove_begin in 1 2 3 4; do # Loop: Remove up to 3 words from beginning
while true; do # Loop: Remove words from end
eecho -e " \tEpisode title:\t $title "
episode_info = $( grep -i " sodeName> $title " " $wget_file " -B 10 | tail -11) # Get XML data of episode
if [ -z " $episode_info " ] ; then # Nothing found. Search the description
if [ ${# title } -gt 10 ] ; then # If title is long enough
episode_info = $( grep -i " verView> $title " " $wget_file " -B 16 | tail -17)
fi
if [ -n " $episode_info " ] ; then # We have found something!
2014-10-15 17:17:01 +02:00
break;
2015-09-03 10:14:12 +02:00
else
episode_info = $( grep -Gi " sodeName> ${ title // \? /. } " " $wget_file " -B 10 | tail -11) # Get XML data of episode with place holders. Sometime special characters are encoded as '?'
if [ -n " $episode_info " ] ; then # We have found something!
2015-03-12 22:19:10 +01:00
break;
2015-09-03 10:14:12 +02:00
else # Still nothing found. Shorten the title
tmp = ${ title % * }
if [ ${# tmp } -le 4 ] || [ " $tmp " = = " $title " ] ; then # Stop when the title is to short
break;
fi
2015-03-12 22:19:10 +01:00
fi
2015-09-03 10:14:12 +02:00
title = " $( echo $tmp | sed -e 's/^[^a-zA-Z0-9.?]*//' -e 's/ *$//' ) " # Remove special characters
2014-10-15 17:17:01 +02:00
fi
2015-03-12 22:19:10 +01:00
else
break;
2014-09-11 17:36:43 +02:00
fi
2015-03-12 22:19:10 +01:00
done # Loop: Remove words from end
if [ -n " $episode_info " ] ; then # We have found something! :)
2014-10-15 17:17:01 +02:00
break;
2014-07-29 14:37:29 +02:00
fi
2015-03-12 22:19:10 +01:00
title = " $episode_title "
for i in $( seq 1 $remove_begin ) ; do # Remove $remove_begin words from the beginning
tmp = " ${ title %% * } " # Get the first word
title = " ${ title # $tmp } " # Remove it from the title
done
done # Loop: Remove words from beginning
if [ -n " $episode_info " ] ; then # We have found something! :)
2014-07-29 11:33:39 +02:00
break;
fi
2015-03-12 22:19:10 +01:00
if $title_not_converted ; then # We have not yet tried to convert the title
funcConvertName " $episode_title "
title_not_converted = false;
else
break;
2014-10-15 17:17:01 +02:00
fi
2015-03-12 22:19:10 +01:00
done # Loop: Convert title
2014-07-29 14:37:29 +02:00
2015-03-03 16:05:21 +01:00
funcGetEpisodeInfo_ParseData
}
function funcGetEpisodeInfoBySE {
if $debug ; then echo -e "\033[36mfuncGetEpisodeInfoBySE\033[37m" ; fi ;
wget_file = " $PwD /episodes- ${ series_id } - ${ langCurrent } .xml "
2015-09-03 10:41:11 +02:00
#echo "grep -i \"bined_episodenumber>$episode_number\" \"$wget_file\" -A 10 | grep -i \"bined_season>$episode_season\" -B 1 -A 9"
2015-03-03 16:05:21 +01:00
episode_info = $( grep -i " bined_episodenumber> $episode_number " " $wget_file " -A 10 | grep -i " bined_season> $episode_season " -B 1 -A 9) # Get XML data
funcGetEpisodeInfo_ParseData
}
function funcGetEpisodeInfo_ParseData {
if $debug ; then echo -e "\033[36mfuncGetEpisodeInfo_ParseData\033[37m" ; fi ;
2014-07-29 14:37:29 +02:00
if [ -n " $episode_info " ] ; then # If we have found something
2015-09-03 10:44:43 +02:00
if [ -z " $episode_number " ] ; then
episode_number = $( echo -e " $episode_info " | grep -m 1 "Combined_episodenumber" ) # Get episode number
episode_number = ${ episode_number %<* } # remove xml tags
episode_number = ${ episode_number #*> }
fi
if [ -z " $episode_season " ] ; then
episode_season = $( echo -e " $episode_info " | grep -m 1 "Combined_season" ) # Get season number
episode_season = ${ episode_season %<* }
episode_season = ${ episode_season #*> }
fi
2014-07-29 14:37:29 +02:00
episode_title = $( echo -e " $episode_info " | grep -m 1 "EpisodeName" ) # Get season name
episode_title = ${ episode_title %<* }
episode_title = ${ episode_title #*> }
2014-10-29 14:45:38 +01:00
if [ -z " $episode_number " -o -z " $episode_season " ] ; then # If we have an illegal match (e.g. Series Overview)
episode_info = # Empty result
episode_title =
else
if [ [ " $episode_number " = = *.* ] ] ; then # Convert float to integer. Float!?
episode_number = ${ episode_number %%.* }
fi
if [ [ " $episode_season " = = *.* ] ] ; then
episode_season = ${ episode_number %%.* }
fi
2014-07-29 14:37:29 +02:00
2014-10-29 14:45:38 +01:00
if [ $episode_number -le 9 ] ; then # add leading zero
episode_number = " 0 $episode_number "
fi
if [ $episode_season -le 9 ] ; then
episode_season = " 0 $episode_season "
fi
eecho -e " TvDB:\tSeason: \t $episode_season "
eecho -e " \tEpisode:\t $episode_number "
fi
2014-07-29 14:37:29 +02:00
fi
}
2014-07-31 20:42:42 +02:00
2015-03-03 16:05:21 +01:00
2014-07-31 20:42:42 +02:00
function funcMakeFilename {
2014-10-29 13:01:39 +01:00
if $debug ; then echo -e "\033[36mfuncMakeFilename\033[37m" ; fi ;
2014-07-31 20:42:42 +02:00
if [ " $lang " = = "de" ] ; then
episode_title = ${ episode_title //Ä/Ae } # Replace umlauts
episode_title = ${ episode_title //Ö/Oe }
episode_title = ${ episode_title //Ü/Ue }
episode_title = ${ episode_title //ä/ae }
episode_title = ${ episode_title //ö/oe }
episode_title = ${ episode_title //ü/ue }
fi
2014-09-11 17:36:43 +02:00
echo " ${ series_title_tvdb // /. } ..S ${ episode_season } E ${ episode_number } .. ${ episode_title // /. } . $file_suffix "
2014-07-31 20:42:42 +02:00
}
2014-07-29 18:21:22 +02:00
# This function does everything
2014-10-29 13:01:39 +01:00
function doIt {
2014-07-29 14:37:29 +02:00
funcHeader
if [ -z " $path " ] ; then # If no path was specified (-f)
echo " Usage: $0 -f pathToAvi [-s] [-l LANG] "
2015-04-07 22:18:19 +02:00
echo
echo "-s makes this script to only output the filename"
echo "-l lets you search TheTVDB in a different language"
2014-08-03 16:51:21 +02:00
exit 1
2014-07-29 14:37:29 +02:00
fi
PwD = $( readlink -e $0 ) # Get the path to this script
PwD = $( dirname " $PwD " )
2014-10-29 13:55:31 +01:00
langCurrent = " $lang "
2014-07-29 14:37:29 +02:00
file_name = " $( basename $path ) " # Get file name
file_dir = " $( dirname $path ) " # Get file directory
funcAnalyzeFilename # Get info from $file_name
funcGetSeriesId # Get series ID from cache or TvDB
2014-07-30 20:26:31 +02:00
2015-03-03 16:05:21 +01:00
if [ -n " $episode_season " -a -n " $episode_number " ] ; then # We already got info from filename
2015-04-07 15:33:05 +02:00
funcDownloadEpisodesFile
2015-03-03 16:05:21 +01:00
funcGetEpisodeInfoBySE
2015-09-03 10:41:11 +02:00
fi
if [ -z " $episode_info " ] ; then # We have to get info from EPG
2015-03-03 16:05:21 +01:00
funcConvertName " $file_title "
2015-09-23 17:50:20 +02:00
if [ " $tmp " != " $series_title_tvdb " ] && ( [ [ " $tmp " = = $series_title_tvdb * ] ] || [ [ " $file_title " = = $series_title_tvdb * ] ] ) ||
[ -n " $series_alias " -a " $tmp " != " $series_alias " ] && ( [ [ " $tmp " = = $series_alias * ] ] || [ [ " $file_title " = = $series_alias * ] ] ) ; then
if $debug ; then echo -e " \033[36mParsing file name only!\n\" $tmp \" != \" $series_title_tvdb \" && (\" $tmp \" == \" $series_title_tvdb *\" || \" $file_title \" == \" $series_title_tvdb *\") || \" $tmp \" != \" $series_alias \" && (\" $tmp \" == \" $series_alias *\" || \" $file_title \" == \" $series_alias *\")\033[37m " ; fi
2015-03-03 16:05:21 +01:00
episode_title = " $( echo ${ file_title # $series_title_tvdb } | sed -e 's/^[^a-zA-Z0-9]*//' -e 's/ *$//' ) "
funcConvertName " $series_title_file "
episode_title = " $( echo ${ episode_title # $tmp } | sed -e 's/^[^a-zA-Z0-9]*//' -e 's/ *$//' ) "
episode_title = " $( echo ${ episode_title # $series_title_tvdb } | sed -e 's/^[^a-zA-Z0-9]*//' -e 's/ *$//' ) "
episode_title = " $( echo ${ episode_title # $series_alias } | sed -e 's/^[^a-zA-Z0-9]*//' -e 's/ *$//' ) "
fi
if [ -n " $episode_title " ] ; then
eecho -e " \t\tEpisode title:\t $episode_title "
episode_title_set = true # used in doItEpisodes (whether the episode title shall be search in epg)
else # Otherwise search the episode title in the EPG:
funcGetEPG # Download epg file
episode_title_set = false
2014-07-29 18:53:03 +02:00
fi
2014-08-06 18:04:41 +02:00
2014-10-28 14:56:59 +01:00
langCurrent = " $lang "
2015-03-03 16:05:21 +01:00
doItEpisodes # Search for the episode in the specified language
if [ -z " $episode_info " ] ; then # Episode was not found!
2014-08-06 18:04:41 +02:00
if [ " $lang " != "en" ] ; then
2014-10-28 14:56:59 +01:00
langCurrent = "en"
2015-03-03 16:05:21 +01:00
doItEpisodes # Try it again with english
2014-08-06 18:04:41 +02:00
fi
2015-03-03 16:05:21 +01:00
fi
if $episode_title_set && [ -z " $episode_info " ] ; then # Episode was not found!
episode_title_set = false # Do not use file name as episode title
funcGetEPG # Download epg file
langCurrent = " $lang "
doItEpisodes # Search for the episode in the specified language and get title from EPG
if [ -z " $episode_info " ] ; then # Episode was not found!
if [ " $lang " != "en" ] ; then
langCurrent = "en"
doItEpisodes # Try it again with english
fi
if [ -z " $episode_info " ] ; then # Again/still no info found! Damn :(
eecho "No episode info found!"
logNexit 20
fi
2014-08-06 18:04:41 +02:00
fi
2014-07-29 18:53:03 +02:00
fi
fi
2014-09-11 17:36:43 +02:00
if [ -n " $episode_info " ] && [ -n " $series_title_tvdb " ] ; then
2014-07-31 20:42:42 +02:00
funcMakeFilename
2014-08-19 00:40:28 +02:00
exit 0
2014-07-29 18:53:03 +02:00
fi
}
# Parse the episodes, language as argument
function doItEpisodes {
2014-07-30 20:26:31 +02:00
if ! $episode_title_set ; then
2015-03-03 15:37:25 +01:00
funcGetEpisodeTitleFromEpg "." # Get the episode title using . as delimiter
2014-07-30 20:26:31 +02:00
fi
2015-03-03 15:37:25 +01:00
funcDownloadEpisodesFile # Download episodes file
2014-08-03 16:51:21 +02:00
if [ -n " $episode_title " ] ; then
2015-03-03 15:37:25 +01:00
funcGetEpisodeInfoByTitle
2014-08-03 16:51:21 +02:00
fi
2014-10-29 13:55:31 +01:00
if [ -z " $episode_info " ] && ! $episode_title_set && [ [ " $episode_title " = = *,* ] ] ; then # No info found and we are allowed to search and our title contains a ","
2015-03-03 15:37:25 +01:00
funcGetEpisodeTitleFromEpg "." "," # Try again with . AND , as delimiter
2015-03-12 21:57:52 +01:00
if [ -n " $episode_title " ] ; then # If we have got an episode title
2015-03-03 15:37:25 +01:00
funcGetEpisodeInfoByTitle
2014-08-03 16:51:21 +02:00
else
eecho -e " EPG:\tNo episode title found in EPG!"
2014-08-19 00:28:38 +02:00
logNexit 21
2014-08-03 16:51:21 +02:00
fi
2014-07-29 10:28:45 +02:00
fi
2014-10-29 13:55:31 +01:00
if [ -z " $episode_info " ] && ! $episode_title_set ; then # No info found and delimiter , is possible:
2015-03-03 15:37:25 +01:00
funcGetEpisodeTitleFromEpg "," # Try again with , as delimiter
2015-03-12 21:57:52 +01:00
if [ -n " $episode_title " ] ; then # If we have got an episode title
2015-03-03 15:37:25 +01:00
funcGetEpisodeInfoByTitle
2014-10-29 13:55:31 +01:00
fi
fi
if [ -z " $episode_info " ] && ! $episode_title_set && [ [ " $episode_title " = = *.* ] ] ; then # No info found and our title contains a "."
2015-03-03 15:37:25 +01:00
funcGetEpisodeTitleFromEpg "," "." # Try again with , AND . as delimiter
2015-03-12 21:57:52 +01:00
if [ -n " $episode_title " ] ; then # If we have got an episode title
2015-03-03 15:37:25 +01:00
funcGetEpisodeInfoByTitle
2014-10-29 13:55:31 +01:00
fi
fi
2014-07-29 14:37:29 +02:00
}
funcParam $@
2014-07-29 18:21:22 +02:00
doIt
2014-08-19 00:28:38 +02:00
logNexit 20