Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # cat `which mvmovie`
- #!/bin/sh
- # Needs:
- #
- # Path to /opt/bin:opt/sbin before rest
- # The perl-script imdb (modified)
- # ln -s /opt/bin/perl /usr/bin/perl
- # ipkg install findutils
- # ipkg install coreutils
- # ipkg install perl-libnet
- # ipkg install perl-libwww
- # ipkg install sed
- export PATH=/opt/bin:/opt/sbin:$PATH
- SCRIPTVER="Tue Aug 18 18:40:43 CEST 2009"
- REMOVE=0
- SUFFIX=""
- AGE=5
- while getopts r name
- do
- case $name in
- r) REMOVE=1;;
- ?) printf "Usage: %s: [-r] args\n" $0
- exit 2;;
- esac
- done
- shift $(($OPTIND - 1))
- _logger ()
- {
- logger -t mvmovie -s "$*"
- }
- _fileage() {
- now=`date +%s`
- fSeconds=${now}
- if [ -e "${f}" ] ; then
- fSeconds=`date -r "${f}" +%s`
- fi
- FILE_AGE=$((${now} - ${fSeconds}))
- return 0
- }
- imdbinfo=/tmp/imdb.info
- # Worldbook Bluering
- BASEDIR=/shares/internal/PUBLIC
- # Worldbook Whitelight
- # BASEDIR=/shares/Public/Shared\ Videos
- # Other Distro
- # BASEDIR=/mnt
- if [ -z "$1" ] ; then
- DOWNLOAD_PATH=${BASEDIR}/Torrent/target
- else
- DOWNLOAD_PATH="`dirname $1`/`basename $1`"
- fi
- if [ ! -d "${BASEDIR}" ] ; then
- _logger "${BASEDIR} does not exist, exiting"
- exit 1
- fi
- TV_PATH="${BASEDIR}/TV"
- [ -d "${TV_PATH}" ] || TV_PATH=""
- if [ ! -d "$DOWNLOAD_PATH" ]; then
- _logger "The directory $DOWNLOAD_PATH does not exist!"
- exit
- fi
- if [ -z "$2" ] ; then
- SUFFIX=avi
- else
- SUFFIX=`echo "$2" | tr '[A-Z]' '[a-z]'`
- fi
- UPPERSUFFIX=`echo "$SUFFIX" | tr '[a-z]' '[A-Z]'`
- DVD_PATH=${BASEDIR}/$UPPERSUFFIX
- DISKTYPE="CD"
- [ "${UPPERSUFFIX}" == "ISO" ] && DISKTYPE="DVD"
- if [ ! -d "$DVD_PATH" ]; then
- mkdir -p "$DVD_PATH"
- fi
- # Garbage collecting
- cd "${DOWNLOAD_PATH}"
- if [ "${DOWNLOAD_PATH}" == "`pwd`" ] ; then
- find -depth -type l -mtime +$AGE -exec rm {} 2>/dev/null \;
- find -depth -type f -mtime +$AGE -name \.\*.info -exec rm {} 2>/dev/null \;
- # find -depth -type f -mtime +$AGE -size -4M -exec rm {} 2>/dev/null \;
- find -iname \*\.${SUFFIX} -type l | while read SYMLINK ; do
- if [ ! -e "${SYMLINK}" ]; then
- rm -f "${SYMLINK}"
- _logger "Removed ${SYMLINK}, because it was a dead symbolic link"
- fi
- done
- # Removing empty directories
- find -depth -type d -empty -exec rmdir {} 2>/dev/null \;
- fi
- DATAUSER=`stat -c %U "${DVD_PATH}" 2>/dev/null`
- if [ $? -eq 0 ] ; then
- DATAGROUP=`stat -c %G "${DVD_PATH}"`
- else
- DATAUSER=www-data
- DATAGROUP=www-data
- fi
- FTMP1=`mktemp`
- FTMP2=`mktemp`
- minsize="170000000"
- cd "${DOWNLOAD_PATH}"
- find ${DOWNLOAD_PATH} -iname \*\.${SUFFIX} -type f >$FTMP1
- while read f ; do
- _fileage
- if [ $FILE_AGE -gt -30 ] && [ $FILE_AGE -lt 1800 ]; then
- _logger "The filedate of $f is quite recent and is probably being created at this moment, skip..."
- else
- orgdir="`dirname "$f"`"
- sizeiso=`stat -c%s "$f" 2>/dev/null`
- if [ "${?}" -ne "0" ] ; then
- sizeiso=$minsize
- fi
- if test $sizeiso -ge $minsize ; then
- cd "${orgdir}"
- SEASONDIR=""
- CDEXT=""
- isofile="`basename "$f"`"
- orgfile="$isofile"
- orgfile_nosuffix="`echo $orgfile | sed -e s/.${SUFFIX}$// | sed -e s/.${UPPERSUFFIX}$//`"
- isofile="`echo "$isofile" | tr '[a-z]' '[A-Z]' | tr '[-]' '(-)' | sed 's/.'"$UPPERSUFFIX"'//g'`"
- # remove brackets and other characters, turn dots and underscores into spaces.
- isofile="`echo "$isofile" | tr -d '{},\!"' | tr '.' ' ' | tr '-' ' ' | tr '_' ' '`"
- # Remove some words and strip double spaces afterwards.
- isofile="`echo "$isofile" | sed -e 's/FTU //g' -e 's/XXX//g' -e 's/DVDRIP//g' -e 's/RETAIL//g' -e 's/WWW.*.COM//g' -e 's/NTSC//g' -e 's/PAL//g'`"
- isofile="`echo "$isofile" | sed -e 's/ CAM / /g' -e 's/^AAF //g' -e 's/TELESYNC//g' -e 's/SCREENER//g' -e 's/ TS / /g' -e 's/KLAXXON//g' -e 's/AXXO//g'`"
- isofile="`echo "$isofile" | sed -e 's/ITALIAN.*SILENT//g' -e 's/ITALIAN.*REPUBLIC//g' -e 's/ITALIAN.*SVD//g' -e 's/ITALIAN.*FSI//g' -e 's/(ITALIAN)//g'`"
- isofile="`echo "$isofile" | sed -e 's/DIVX.*ENG//g' -e 's/(ENG)//g' -e 's/FXG//g' -e 's/RELEASE//g' -e 's/720P//g' -e 's/BLURAY//g' -e 's/X264//g'`"
- isofile="`echo "$isofile" | sed -e 's/XVID//g' -e 's/HDTV//g' -e 's/TV / /g' -e 's/DIVX//g' -e 's/AC3//g' -e 's/DTS//g' | tr -s ' '`"
- # Replace CD1, cda, part1 or DVD with -CD1
- CDEXT="`echo "$isofile" | grep -o "[ _.-]CD[ _.-]*[0-9A-D]" | head -n1`"
- [ -z "${CDEXT}" ] && CDEXT="`echo "$isofile" | grep -o "[ _.-]DVD[ _.-]*[0-9A-D]" | head -n1`"
- [ -z "${CDEXT}" ] && CDEXT="`echo "$isofile" | grep -o "[ _.-]PART[ _.-]*[0-9A-D]" | head -n1`"
- [ -z "${CDEXT}" ] && CDEXT="`echo "$isofile" | grep -o "[ _.-]DIS[CK][ _.-]*[0-9A-D]" | head -n1`"
- if [ ! -z "${CDEXT}" ] ; then
- isofile="`echo "${isofile}" | sed -e s/"${CDEXT}"//g`"
- CDEXT="-${DISKTYPE}`echo "${CDEXT#${CDEXT%?}}" | tr [a-d] [1-4]`"
- fi
- isofile="`echo "${isofile}" | sed -e 's/DVD//g'`"
- # Turn into lowercase with capitalized first letters.
- isofile="`echo "$isofile" | sed 's/\<\(.\)\([^ ]*\)/\u\1\L\2/g'`"
- # Remove some more words
- isofile="`echo "$isofile" | sed -e 's/ Rip//g' -e 's/ Nl//g'`"
- # Change some words into complete lowercase as they look awkward otherwise.
- isofile="`echo "$isofile" | sed -e 's/ My / my /g' -e 's/ In / in /g' -e 's/ Or / or /g' -e 's/ And / and /g' -e 's/ It / it /g' `"
- isofile="`echo "$isofile" | sed -e 's/ A / a /g' -e 's/ An / an /g' -e 's/ Of / of /g' -e 's/ Up / up /g' -e 's/ The / the /g' `"
- isofile="`echo "$isofile" | sed -e 's/ Van / van /g' -e 's/ Het / het /g' -e 's/ De / de /g' -e 's/ Des / des /g' -e 's/ Le / le /g' `"
- isofile="`echo "$isofile" | sed -e 's/Cd/CD/g' -e 's/ Over / over /g' -e 's/ To / to /g' -e 's/ On / on /g' -e 's/ Is / is /g' -e 's/ As / as /g' `"
- isofile="`echo "$isofile" | sed 's/()//g' | tr -s ' '`"
- # Delete YEAR and everything behind it....
- YEAR=0
- YEARPOS=`echo "${isofile}" | awk '{ print match($0,"[[({,._ ][12][90][0-9][0-9]") }'`
- if [ ${YEARPOS} -gt 5 ]; then
- YEAR=`echo "${isofile}" | grep -o "[[({,._ ][12][90][0-9][0-9]" | cut -b2-`
- let YEARPOS-=1
- isofile=`echo "${isofile}" | cut -b1-${YEARPOS} | sed 's/[_ ]$//g'`
- _logger "Movie is of the year: ${YEAR}"
- fi
- SEASON_EP=`echo "${isofile} " | egrep -o '[Ss][0-9][0-9].?[EeXx][0-9][ .0-9]'`
- if [ ! -z "${SEASON_EP}" ] ; then
- DELIM=`echo "${SEASON_EP}" | tr -cd EeXx`
- SEASON="`echo "${SEASON_EP}" | tr -cd 0-9EeXx | awk -F${DELIM} '{print $1}' | tr -cd 0-9 | awk '{printf "%02d\n",$0}'`"
- EP="`echo "${SEASON_EP}" | tr -cd 0-9EeXx | awk -F${DELIM} '{print $2}' | tr -cd 0-9 | awk '{printf "%02d\n",$0}'`"
- else
- SEASON_EP=`echo "${isofile} " | egrep -o '[ .0-9][0-9][Xx][0-9][ .0-9]'`
- if [ ! -z "${SEASON_EP}" ] ; then
- DELIM=`echo "${SEASON_EP}" | tr -cd Xx`
- SEASON="`echo "${SEASON_EP}" | tr -cd 0-9Xx | awk -F${DELIM} '{print $1}' | tr -cd 0-9 | awk '{printf "%02d\n",$0}'`"
- EP="`echo "${SEASON_EP}" | tr -cd 0-9Xx | awk -F${DELIM} '{print $2}' | tr -cd 0-9 | awk '{printf "%02d\n",$0}'`"
- else
- SEASON_EP=`echo "${isofile} " | egrep -o '[ .][0-6][0-5][0-9][ .]'`
- if [ ! -z "${SEASON_EP}" ] ; then
- SEASON="`echo "${SEASON_EP}" | tr -cd 0-9 | cut -b1-1 | awk '{printf "%02d\n",$0}'`"
- EP="`echo "${SEASON_EP}" | tr -cd 0-9 | cut -b2- | awk '{printf "%02d\n",$0}'`"
- else
- SEASON_EP=`echo "${isofile} " | egrep -o '[ .]0[0-6][0-5][0-9][ .]'`
- if [ ! -z "${SEASON_EP}" ] ; then
- SEASON="`echo "${SEASON_EP}" | tr -cd 0-9 | cut -b1-2 | awk '{printf "%02d\n",$0}'`"
- EP="`echo "${SEASON_EP}" | tr -cd 0-9 | cut -b3- | awk '{printf "%02d\n",$0}'`"
- else
- SEASON_EP=`echo "${isofile} " | egrep -o '[Ss._ -]*[0-6][._ -]*[EeXx][0-9][ .]*'`
- if [ ! -z "${SEASON_EP}" ] ; then
- DELIM=`echo "${SEASON_EP}" | tr -cd EeXx`
- SEASON="`echo "${SEASON_EP}" | tr -cd 0-9EeXx | awk -F${DELIM} '{print $1}' | tr -cd 0-9 | awk '{printf "%02d\n",$0}'`"
- EP="`echo "${SEASON_EP}" | tr -cd 0-9EeXx | awk -F${DELIM} '{print $2}' | tr -cd 0-9 | awk '{printf "%02d\n",$0}'`"
- fi
- fi
- fi
- fi
- fi
- if [ ! -z "${SEASON_EP}" ] ; then
- SEASON_EP=`echo "${SEASON_EP}" | sed 's/^ *//;s/ *$//'`
- SEASEP=" S${SEASON}E$EP "
- isofile="`echo "${isofile}" | sed "s/${SEASON_EP}/${SEASEP}/"`"
- SEASONPOS=`echo "${isofile}" | awk '{ print match($0,"S[0-9][0-9]E[0-9][0-9]") }'`
- if [ ${SEASONPOS} -gt 3 ]; then
- let SEASONPOS-=1
- SERIE="`echo "${isofile}" | cut -b-${SEASONPOS} | tr ' _' '.' | tr -s '.' | sed 's/\.$//g'`"
- let SEASONPOS+=6
- isofile="`echo "${isofile}" | cut -b-${SEASONPOS}`"
- [ -z "${TV_PATH}" ] || SEASONDIR="${TV_PATH}/${SERIE}/${SERIE}.S${SEASON}"
- fi
- else
- # IMDB code
- simdb="`echo "$isofile" | tr '-' ' ' | tr -s ' ' | sed -e 's/ /_/1' -e 's/ /_/1'`"
- # Search IMDB, delete ANSI codes and remove accents from foreign characters
- _logger "Searching imdb for ${simdb}"
- rm -rf "$imdbinfo"
- imdb "${simdb}" | sed -e 's/\x1b\[[0-9]\{1,2\}\(;[0-9]\{1,2\}\)\{0,2\}m//g' | sed -e 's/\xc3[\x80\x81\x82\x83\x84\x85]/A/g' -e 's/\xc3\x86/AE/g' -e 's/\xc3[\x88\x90\x8a\x8b]/E/g' -e 's/\xc3[\x8c\x8d\x8e\x8f]/I/g' -e 's/\xc3[\x92\x93\x94\x95\x96\x98]/O/g' -e 's/\xc3[\x99\x9a\x9b\x9c]/U/g' -e 's/\xc3\x9f/ss/g' -e 's/\xc3[\xa0\xa1\xa2\xa3\xa4\xa5]/a/g' -e 's/\xc3[\xb2\xb3\xb4\xb5\xb6\xb8]/o/g' -e 's/\xc3[\xa8\xa9\xaa\xab]/e/g' -e 's/\xc3[\xb0\xb2\xb3\xb4\xb5\xb6\xb8]/e/g' -e 's/\xc3[\xb9\xba\xbb\xbc]/u/g' -e 's/\xc3\xb1/n/g' -e 's/\xc3\x91/N/g' | grep -v '[^[:print:]]' >$imdbinfo
- if [ -f "${imdbinfo}" ]; then
- lines=`wc -l ${imdbinfo} | awk '{print $1}'`
- if [ "$lines" == "0" ]; then
- _logger "imdb returned no data"
- else
- full_title=`grep -il "Title\:" "$imdbinfo"`
- if [ -n "$full_title" ]; then
- isofile=`cat "$imdbinfo" | grep "Title\:" | cut -b8- | tr -d '\"' `
- _logger "IMDB came up with $isofile"
- else
- if [ "$lines" -gt "6" ]; then
- _logger "IMDB returned $lines matches, I will leave it untouched"
- if [ "${YEAR}" -ne 0 ]; then
- WITHYEAR=`cat "$imdbinfo" | grep ${YEAR} | head -n1 | cut -b9- | tr -d '\"'`
- if [ ! "" == "${WITHYEAR}" ]; then
- isofile="${WITHYEAR}"
- _logger "IMDB came up with $lines entries.. I took the first one which contained ${YEAR}: $isofile"
- fi
- fi
- else
- firstline="`cat "$imdbinfo" | sed q`"
- colon=`echo "$firstline" | cut -b8`
- if [ "$colon" == ":" ]; then
- isofile="`echo "$firstline" | cut -b9- | tr -d '\"' `"
- _logger "IMDB came up with $lines entries.. I took the first named: $isofile"
- else
- echo "colon test failed"
- fi
- fi
- fi
- fi
- else
- _logger "Unable to write to file: ${imdbinfo}"
- fi
- fi
- # Delete year if it's from the 21st Century
- isofile="`echo "$isofile" | sed 's/(20[012][0-9])//g' | tr -cd "\-\&[].,:;'() 0-9a-zA-Z"`"
- # Turn spaces into dots, remove repeating dots and remove square brackets with nothing in between.
- isofile="`echo "${isofile}" | tr ':' ';' | tr ' ' '.' | tr -s '.' | sed -e 's/\[\]//g' -e 's/^\.//;s/\.$//g'`${CDEXT}"
- destfile_nosuffix="${isofile}"
- isofile="${isofile}.${SUFFIX}"
- if [ -z "${SEASONDIR}" ] ; then
- DEST_PATH="${DVD_PATH}"
- else
- DEST_PATH="${SEASONDIR}"
- if [ ! -d "${SEASONDIR}" ] ; then
- mkdir -p "${SEASONDIR}" 2>/dev/null
- chmod ug-x+rwX "${TV_PATH}"
- grep -q ${DATAGROUP} /etc/group && chgrp -R ${DATAGROUP} "${TV_PATH}"
- grep -q ${DATAUSER} /etc/passwd && chown -R ${DATAUSER} "${TV_PATH}"
- fi
- fi
- isopathfile="${DEST_PATH}/${isofile}"
- if [ ! -e "${isopathfile}" ]; then
- _logger "Moving \"${f}\" to \"${isopathfile}\""
- if [ ! "$orgfile" == "$isofile" ]; then
- _logger "I renamed $orgfile to $isofile for better reading"
- fi
- # If a subtitle with same name exists... move it too
- find "${DOWNLOAD_PATH}" -type f -iname "${orgfile_nosuffix}*.rar" >${FTMP2}
- while read rarfile ; do
- if [ `stat -c%s "${rarfile}"` -lt 3000000 ]; then
- unrar x -y "${rarfile}" 2>&1 >/dev/null
- unrarerror=${?}
- if [ "$REMOVE" == "0" ] && [ ${unrarerror} -eq 0 ] ; then
- rm -f "${rarfile}"
- fi
- fi
- done <${FTMP2}
- find "${DOWNLOAD_PATH}" -type f -iname "${orgfile_nosuffix}*.sub" >${FTMP2}
- while read subfile ; do
- _logger "sub-file ${subfile} found!"
- subfile_nosuffix="`echo "${subfile}" | sed 's/.\{4\}$//'`"
- subfile_nosuffix="`basename "${subfile_nosuffix}"`"
- idxfile="`find "${DOWNLOAD_PATH}" -type f -iname "${subfile_nosuffix}.idx" | head -n1`"
- if [ -z "${idxfile}" ] ; then
- _logger "NO idx-file found!"
- else
- _logger "idx-file ${idxfile} found!"
- fi
- LANG=""
- if [ `head -c50000 "${subfile}" | grep -c "the "` -gt 25 ] ; then
- LANG="eng."
- elif [ `head -c50000 "${subfile}" | grep -c "de "` -gt 25 ] ; then
- LANG="dut."
- fi
- idxfiledest="${DEST_PATH}/${destfile_nosuffix}.${LANG}idx"
- subfiledest="${DEST_PATH}/${destfile_nosuffix}.${LANG}sub"
- [ -f "${idxfile}" ] && mv "${idxfile}" "${idxfiledest}"
- mv "${subfile}" "${subfiledest}"
- if [ "$REMOVE" == "0" ] ; then
- ln -s "${idxfiledest}" "${idxfile}"
- ln -s "${subfiledest}" "${subfile}"
- fi
- done <${FTMP2}
- find "${DOWNLOAD_PATH}" -type f -iname "${orgfile_nosuffix}*.srt" >${FTMP2}
- while read subfile ; do
- _logger "srt-file ${subfile} found!"
- LANG=""
- if [ `grep -c "the " "${subfile}"` -gt 25 ] ; then
- LANG="eng."
- elif [ `grep -c "de " "${subfile}"` -gt 25 ] ; then
- LANG="dut."
- fi
- subfiledest="${DEST_PATH}/${destfile_nosuffix}.${LANG}srt"
- mv "${subfile}" "${subfiledest}"
- if [ "$REMOVE" == "0" ] ; then
- ln -s "${subfiledest}" "${subfile}"
- fi
- done <${FTMP2}
- mv "${f}" "${isopathfile}"
- [ "$REMOVE" == "0" ] && ln -s "${isopathfile}" "${f}"
- chmod -R ug-x+rwX "${DEST_PATH}"
- grep -q ${DATAGROUP} /etc/group && chgrp -R ${DATAGROUP} "${DEST_PATH}"
- grep -q ${DATAUSER} /etc/passwd && chown -R ${DATAUSER} "${DEST_PATH}"
- else
- _logger "$isopathfile already exists.... maybe a split movie?"
- fi
- cd "${DOWNLOAD_PATH}"
- fi
- fi
- done <$FTMP1
- rm -f $FTMP1
- rm -f $FTMP2
- cd "${DOWNLOAD_PATH}"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement