#!/bin/bash
# /usr/local/bin/getimdb.sh
# Arg[1] = VIDEO_ID to update (ignores prev imdb num
# Arg[1] = "" => Uses first db record with inetref="00000000"
# Arg[1] = "-1" => Update ALL db records with inetref="00000000"
# Arg[2] = (optional) IMDBNUM => if Arg[1] = VIDEO_ID then arg[2] may be used to force the IMDBNUM to use rather than rely on a lookup
# Arg[2] = (optional) Title => if Arg[1] = VIDEO_ID then arg[2] may be used to force the title to use rather than rely on a lookup
# return 0 = OK
# return 1 = No more records to update (if $1 = -1 will always return 1 for ok or higher for error)
# return 2 = No title name found in database, hence not able to search. (inetref set = "" so won't search again, unless exact initid sent or inetref set = "00000000"
# return 3 = No IMDB entry found (inetref set = "" as above)
ECHO_OUT='/myth/video/.covers/getimdb.log'
POSTERPATH="/myth/video/.covers"
ADDCOUNT=0
STACKCOUNT=0
COPYCOUNT=0
BADCOUNT=0
NOIMDBCOUNT=0
EXITVAL=0
DOTITLE=0 #turn copying imdb title over current title on/off. Turn off for series where 1x10 is req'd
STACKING=1 #turn stacking on/off
#case $2 in *[^[:digit:]]*) echo not a number;; *) echo okay;; esac
function IsNumber() { case $1 in *[^[:digit:]]*) return 1 ;; *) return 0 ;; esac } # function IsNumber() { case $1 in *[^[:digit:]]*) echo 0 ;; *) echo 1 ;; esac }
while [ $EXITVAL -eq 0 ] ; do ### endless loop for if $1 = -1
VIDEO_ID=""
IMDBNUM=""
TITLE=""
if [[ $1 == "" || $1 == "-1" ]] ; then
# Only do for category = movies
VIDEO_ID=`mysql -u root -e "SELECT intid FROM mythconverg.videometadata WHERE inetref = '00000000' && category = 4 LIMIT 0, 1;" -BN`
if [[ $VIDEO_ID = "" ]] ; then #no unchecked items left
echo "FINISHED: No more records to update" | tee -a ${ECHO_OUT}
EXITVAL=10
fi
else
VIDEO_ID=$1
if [[ ${#2} = 7 ]] && IsNumber "$2"; then # if [[ ${#2} = 7 && $(IsNumber $2) = 1 ]] ; then
IMDBNUM=$2
else
TITLE=$2
fi
fi
#echo videoid = \"$VIDEO_ID\" , title = \"$TITLE\" , imdbnum = \"$IMDBNUM\"
#exit 1
if [[ $EXITVAL -eq 0 && $IMDBNUM == "" ]] ; then
echo video_id = \"$VIDEO_ID\" | tee -a ${ECHO_OUT}
if [[ $TITLE == "" ]] ; then
TITLE=`mysql -u root -e "SELECT title FROM mythconverg.videometadata WHERE intid = $VIDEO_ID;" -BN`
fi
if [[ $TITLE = "" ]] ; then #not found in imdb
echo "ERROR: No title set for record $VIDEO_ID" | tee -a ${ECHO_OUT}
let BADCOUNT+=1 # ((BADCOUNT += 1)) inc number of files with no imdb val
EXITVAL=11
else
echo original title = \"${TITLE}\" | tee -a ${ECHO_OUT}
# TITLE=${TITLE%% -*} #remove all after first space hyphen (lose CD1/CD2/1x01 etc)
# TITLE=${TITLE%% .*} #remove all after first dot found
echo search title = \"$TITLE\" | tee -a ${ECHO_OUT}
### Get IMDBNUM
DVDCMD=(/usr/share/mythtv/mythvideo/scripts/imdb.pl -M tv=no\;video=no "$TITLE") #for movies
# DVDCMD=(/usr/share/mythtv/mythvideo/scripts/imdb.pl -M tv=yes\;video=yes "$TITLE") #for tv shows
IMDBNUM=`"${DVDCMD[@]}"` #0159273:Behind Enemy Lines (2001) 0091713:Behind Enemy Lines (1986) 0115647:Behind Enemy Lines (1998) 0093345:Killer Instinct (1987)
IMDBNUM=${IMDBNUM%%:*} #get all before ':' (extract imdbnum)
if [[ $IMDBNUM == "" ]] ; then #not found in imdb
echo "ERROR: No IMDB Number found" | tee -a ${ECHO_OUT}
let NOIMDBCOUNT+=1 #inc number of files with no imdb val
EXITVAL=12
fi
fi
if [[ $EXITVAL -ne 0 ]] ; then #If no title or imdbnum, clear imdbnum so doesn't search again
mysql -u root -e "UPDATE mythconverg.videometadata SET inetref = '' WHERE intid = '${VIDEO_ID}';"
fi
fi
if [[ $EXITVAL -eq 0 ]] ; then
echo imdbnum = \"$IMDBNUM\" | tee -a ${ECHO_OUT}
VIDEO_ID_ORIG=`mysql -u root -e "SELECT intid FROM mythconverg.videometadata WHERE inetref = '${IMDBNUM}' && intid <> '${VIDEO_ID}' LIMIT 0, 1;" -BN`
if [[ $VIDEO_ID_ORIG == "" ]] ; then #IMDBNUM not already in db for other movie so add new one
### Download Poster locally to POSTERPATH
DVDCMD=(/usr/share/mythtv/mythvideo/scripts/imdb.pl -P "$IMDBNUM")
POSTERURL=`"${DVDCMD[@]}"` # http://www.impawards.com/2001/posters/behind_enemy_lines.jpg
POSTERPATHNAME="$POSTERPATH/${IMDBNUM}.jpg"
if [[ $POSTERURL != "" ]] ; then
echo copying poster file from \"$POSTERURL\" to \"$POSTERPATHNAME\" | tee -a ${ECHO_OUT}
cd "${POSTERPATH}"
wget -b -a "wget-log" -O "${IMDBNUM}.jpg" "${POSTERURL}" #grab poster in background
else
# POSTERPATHNAME="No Cover: ${POSTERPATHNAME}"
echo No Poster Found!!! | tee -a ${ECHO_OUT}
fi
### Get Movie info
DVDCMD=(/usr/share/mythtv/mythvideo/scripts/imdb.pl -D "$IMDBNUM")
IMDBINFO=`"${DVDCMD[@]}"`
# imdbinfo = Title:Behind Enemy Lines Year:2001 Director:John Moore Plot:A Navy navigator is shot down over enemy territory and is ruthlessly pursued by a secret police enforcer and the opposing troops. Meanwhile his commanding officer goes against orders in an attempt to rescue him. UserRating:6.0 MovieRating:Rated PG-13 for war violence and some language. Runtime:106 Writers: Jim Thomas Cast: Owen Wilson,Gene Hackman,Gabriel Macht,Charles Malik Whitfield,David Keith,Olek Krupa,Joaquim de Almeida,Vladimir Mashkov,Marko Igonda,Eyal Podell,Geoffrey Pierson,Aernout Van Lynden,Sam Jaeger,Shane Johnson,Don Winston Genres: Action,War,Drama,Thriller Countries: USA
echo imdb_Info = \"$IMDBINFO\" | tee -a ${ECHO_OUT}
# Extract Title
IMDB_Title=${IMDBINFO##Title:}
IMDB_Title=${IMDB_Title%%?Year:*}
IMDB_Title=${IMDB_Title//\'/\\\'} #sub ' with \' (single ' escapes all chars except ') Alt = IMDB_Title=${IMDB_Title//"'"/"''"}
echo imdb_Title = \"$IMDB_Title\" | tee -a ${ECHO_OUT}
# Extract Year
IMDB_Year=${IMDBINFO##*Year:}
IMDB_Year=${IMDB_Year%%?Director:*}
echo imdb_Year = \"$IMDB_Year\" | tee -a ${ECHO_OUT}
# Extract Director
IMDB_Director=${IMDBINFO##*Director:}
IMDB_Director=${IMDB_Director%%?Plot:*}
IMDB_Director=${IMDB_Director//\'/\\\'}
echo imdb_Director = \"$IMDB_Director\" | tee -a ${ECHO_OUT}
# Extract Plot
IMDB_Plot=${IMDBINFO##*Plot:}
IMDB_Plot=${IMDB_Plot%%?UserRating:*}
IMDB_Plot=${IMDB_Plot//\'/\\\'}
echo imdb_Plot = \"$IMDB_Plot\" | tee -a ${ECHO_OUT}
# Extract UserRating
IMDB_UserRating=${IMDBINFO##*UserRating:}
IMDB_UserRating=${IMDB_UserRating%%?MovieRating:*}
echo imdb_UserRating = \"$IMDB_UserRating\" | tee -a ${ECHO_OUT}
# Extract MovieRating
IMDB_MovieRating=${IMDBINFO##*MovieRating:}
IMDB_MovieRating=${IMDB_MovieRating%%?Runtime:*}
IMDB_MovieRating=${IMDB_MovieRating//\'/\\\'}
echo imdb_MovieRating = \"$IMDB_MovieRating\" | tee -a ${ECHO_OUT}
# Extract Runtime
IMDB_Runtime=${IMDBINFO##*Runtime:}
IMDB_Runtime=${IMDB_Runtime%%?Writers:*}
echo imdb_Runtime = \"$IMDB_Runtime\" | tee -a ${ECHO_OUT}
# Extract Writers
IMDB_Writers=${IMDBINFO##*Writers: }
IMDB_Writers=${IMDB_Writers%%?Cast:*}
IMDB_Writers=${IMDB_Writers//\'/\\\'}
echo imdb_Writers = \"$IMDB_Writers\" | tee -a ${ECHO_OUT}
# Extract Cast
IMDB_Cast=${IMDBINFO##*Cast: }
IMDB_Cast=${IMDB_Cast%%?Genres:*}
IMDB_Cast=${IMDB_Cast//\'/\\\'}
echo imdb_Cast = \"$IMDB_Cast\" | tee -a ${ECHO_OUT}
# Extract Genres : Add to videogenre & videometadatagenre
IMDB_Genres=${IMDBINFO##*Genres: }
IMDB_Genres=${IMDB_Genres%%?Countries:*}
IMDB_Genres=${IMDB_Genres//\'/\\\'}
echo imdb_Genres = \"$IMDB_Genres\" | tee -a ${ECHO_OUT}
# Extract Countries : Add to videocountry & videometadatacountry
IMDB_Countries=${IMDBINFO##*Countries: }
IMDB_Countries=${IMDB_Countries//\'/\\\'}
echo imdb_Countries = \"$IMDB_Countries\" | tee -a ${ECHO_OUT}
# UPDATE DB (REMOVED category = 4)
if [[ $DOTITLE == 1 ]] ; then
mysql -u root -e "UPDATE mythconverg.videometadata SET title = '${IMDB_Title}', director = '${IMDB_Director}', plot = '${IMDB_Plot}', rating = '${IMDB_MovieRating}', inetref = '${IMDBNUM}', year = '${IMDB_Year}', userrating = '${IMDB_UserRating}', length = '${IMDB_Runtime}', coverfile = '${POSTERPATHNAME}' WHERE intid = '${VIDEO_ID}';"
else
mysql -u root -e "UPDATE mythconverg.videometadata SET director = '${IMDB_Director}', plot = '${IMDB_Plot}', rating = '${IMDB_MovieRating}', inetref = '${IMDBNUM}', year = '${IMDB_Year}', userrating = '${IMDB_UserRating}', length = '${IMDB_Runtime}', coverfile = '${POSTERPATHNAME}' WHERE intid = '${VIDEO_ID}';"
fi
O=$IFS
IFS=","
IMDB_Country_Array=(${IMDB_Countries})
IMDB_Genre_Array=(${IMDB_Genres})
IFS=$O
# videogenre + videometadatagenre
mysql -u root -e "DELETE FROM mythconverg.videometadatagenre WHERE idvideo = '${VIDEO_ID}';"
for (( i = 0 ; i < ${#IMDB_Genre_Array[@]} ; i++ ))
do
NewGenre=${IMDB_Genre_Array[$i]}
GENRE_ID=`mysql -u root -e "SELECT intid FROM mythconverg.videogenre WHERE genre = '${NewGenre}';" -BN`
if [[ ${GENRE_ID} == "" ]] ; then
GENRE_ID=`mysql -u root -e "INSERT INTO mythconverg.videogenre VALUES (-1,'${NewGenre}'); SELECT LAST_INSERT_ID();" -BN`
echo Added new genre: \"${NewGenre}\" | tee -a ${ECHO_OUT}
else
echo Genre already exists: \"${NewGenre}\" | tee -a ${ECHO_OUT}
fi
mysql -u root -e "INSERT INTO mythconverg.videometadatagenre VALUES ('${VIDEO_ID}','${GENRE_ID}');"
done
# videocountry + videometadatacountry
mysql -u root -e "DELETE FROM mythconverg.videometadatacountry WHERE idvideo = '${VIDEO_ID}';"
for (( i = 0 ; i < ${#IMDB_Country_Array[@]} ; i++ ))
do
NewCountry=${IMDB_Country_Array[$i]}
COUNTRY_ID=`mysql -u root -e "SELECT intid FROM mythconverg.videocountry WHERE country = '${NewCountry}';" -BN`
if [[ ${COUNTRY_ID} == "" ]] ; then
COUNTRY_ID=`mysql -u root -e "INSERT INTO mythconverg.videocountry VALUES (-1,'${NewCountry}'); SELECT LAST_INSERT_ID();" -BN`
echo Added new country: \"${NewCountry}\" | tee -a ${ECHO_OUT}
else
echo Country already exists: \"${NewCountry}\" already exists | tee -a ${ECHO_OUT}
fi
mysql -u root -e "INSERT INTO mythconverg.videometadatacountry VALUES ('${VIDEO_ID}','${COUNTRY_ID}');"
done
let ADDCOUNT+=1 #inc number of new files added
EXITVAL=1
else #got matching imdb record so no need to re-add
# copy fields
# src = VIDEO_ID_ORIG
# dest VIDEO_ID
STACKOK=0 #assume not stackable
if [[ $STACKING == 1 ]] ; then
# may have found earlier match, see if id before hand is a match
VIDEO_ID_ADJ=$(mysql -u root -e "SELECT intid FROM mythconverg.videometadata WHERE inetref = '${IMDBNUM}' && intid = $((VIDEO_ID - 1));" -BN)
if [[ $VIDEO_ID_ADJ != "" ]] ; then
VIDEO_ID_ORIG=${VIDEO_ID_ADJ}
fi
if [[ $VIDEO_ID -eq $((VIDEO_ID_ORIG + 1)) ]] ; then
EPNUM1=$(mysql -u root -e "SELECT filename FROM mythconverg.videometadata WHERE intid = $VIDEO_ID_ORIG;" -BN)
EPNUM1=$((${EPNUM1:(-6):2}))
EPNUM2=$(mysql -u root -e "SELECT filename FROM mythconverg.videometadata WHERE intid = $VIDEO_ID;" -BN)
EPNUM2=$((${EPNUM2:(-6):2} - 1))
echo EPNUM1=$EPNUM1 , EPNUM2=$EPNUM2
if [ ${EPNUM1} -gt 0 ] && [ ${EPNUM1} -eq ${EPNUM2} ] ; then # both eps must end with adjacent nums eg xxxx.01.??? and xxxx.02.??? to be stackable
STACKOK=1
fi
fi
fi
if [[ ${STACKOK} == 1 ]] ; then
#copy & stack! as adjacent to original. so will only stack at most 2 videos together (don't want series to stack, just 2cd movies)
#note for series first 2 eps will therefore end up stacked, but they can be manually unstacked
#it is NOT advisable to stack more than 2 entries or myth will keep trying to play them one after the other.
if [[ $DOTITLE == 1 ]] ; then
mysql -u root -e "USE mythconverg; UPDATE videometadata AS src, videometadata AS dest SET dest.title=src.title, dest.director=src.director, dest.plot=src.plot, dest.rating=src.rating, dest.inetref=src.inetref, dest.year=src.year, dest.userrating=src.userrating, dest.length=src.length, dest.coverfile=src.coverfile, src.childid=${VIDEO_ID}, dest.browse=0 WHERE src.intid=${VIDEO_ID_ORIG} AND dest.intid=${VIDEO_ID};"
else
mysql -u root -e "USE mythconverg; UPDATE videometadata AS src, videometadata AS dest SET dest.director=src.director, dest.plot=src.plot, dest.rating=src.rating, dest.inetref=src.inetref, dest.year=src.year, dest.userrating=src.userrating, dest.length=src.length, dest.coverfile=src.coverfile, src.childid=${VIDEO_ID}, dest.browse=0 WHERE src.intid=${VIDEO_ID_ORIG} AND dest.intid=${VIDEO_ID};"
fi
echo Stacked id:${VIDEO_ID} on id:${VIDEO_ID_ORIG} | tee -a ${ECHO_OUT}
let STACKCOUNT+=1 #inc number of new files stacked
else
# normal copy NOTE neither one sets title this is left to differ from other entry with same imdb
if [[ $DOTITLE == 1 ]] ; then
mysql -u root -e "USE mythconverg; UPDATE videometadata AS src, videometadata AS dest SET dest.title=src.title, dest.director=src.director, dest.plot=src.plot, dest.rating=src.rating, dest.inetref=src.inetref, dest.year=src.year, dest.userrating=src.userrating, dest.length=src.length, dest.coverfile=src.coverfile, src.childid=-1, dest.browse=1 WHERE src.intid=${VIDEO_ID_ORIG} AND dest.intid=${VIDEO_ID};"
else
mysql -u root -e "USE mythconverg; UPDATE videometadata AS src, videometadata AS dest SET dest.director=src.director, dest.plot=src.plot, dest.rating=src.rating, dest.inetref=src.inetref, dest.year=src.year, dest.userrating=src.userrating, dest.length=src.length, dest.coverfile=src.coverfile, src.childid=-1, dest.browse=1 WHERE src.intid=${VIDEO_ID_ORIG} AND dest.intid=${VIDEO_ID};"
fi
echo Copied id:${VIDEO_ID} from id:${VIDEO_ID_ORIG} | tee -a ${ECHO_OUT}
let COPYCOUNT+=1 #inc number of new files copied data from original
fi
EXITVAL=2
fi
fi
if [[ $1 -eq -1 && $EXITVAL -ne 10 ]] ; then
EXITVAL=0 #reset exitval so does next entry if req'd
echo ---------- | tee -a ${ECHO_OUT}
fi
done #### loop until all records updated if $1 = -1
echo IMDB Results: new_imdb=${ADDCOUNT}, stacked=${STACKCOUNT}, copied_imdb=${COPYCOUNT}, no_title=${BADCOUNT}, no_imdb=${NOIMDBCOUNT}, exitval=${EXITVAL} | tee -a ${ECHO_OUT}
echo ------------------------------------------ | tee -a ${ECHO_OUT}
exit $EXITVAL