Advertisement
Guest User

frater

a guest
Aug 18th, 2009
330
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 15.34 KB | None | 0 0
  1. # cat `which mvmovie`
  2. #!/bin/sh
  3. # Needs:
  4. #
  5. # Path to /opt/bin:opt/sbin before rest
  6. # The perl-script imdb (modified)
  7. # ln -s /opt/bin/perl /usr/bin/perl
  8.  
  9. # ipkg install findutils
  10. # ipkg install coreutils
  11. # ipkg install perl-libnet
  12. # ipkg install perl-libwww
  13. # ipkg install sed
  14. export PATH=/opt/bin:/opt/sbin:$PATH
  15. SCRIPTVER="Tue Aug 18 18:40:43 CEST 2009"
  16. REMOVE=0
  17. SUFFIX=""
  18. AGE=5
  19. while getopts r name
  20. do
  21.   case $name in
  22.     r)    REMOVE=1;;
  23.     ?)   printf "Usage: %s: [-r] args\n" $0
  24.     exit 2;;
  25.   esac
  26. done
  27. shift $(($OPTIND - 1))
  28.  
  29. _logger ()
  30. {
  31. logger -t mvmovie -s "$*"
  32. }
  33.  
  34. _fileage() {
  35.   now=`date +%s`
  36.   fSeconds=${now}
  37.   if [ -e "${f}" ] ; then
  38.     fSeconds=`date -r "${f}" +%s`
  39.   fi
  40.   FILE_AGE=$((${now} - ${fSeconds}))
  41.   return 0
  42. }
  43.  
  44. imdbinfo=/tmp/imdb.info
  45.  
  46. # Worldbook Bluering
  47. BASEDIR=/shares/internal/PUBLIC
  48.  
  49. # Worldbook Whitelight
  50. # BASEDIR=/shares/Public/Shared\ Videos
  51.  
  52. # Other Distro
  53. # BASEDIR=/mnt
  54.  
  55. if [ -z "$1" ] ; then
  56.   DOWNLOAD_PATH=${BASEDIR}/Torrent/target
  57. else
  58.   DOWNLOAD_PATH="`dirname $1`/`basename $1`"
  59. fi
  60.  
  61. if [ ! -d "${BASEDIR}" ] ; then
  62.   _logger "${BASEDIR} does not exist, exiting"
  63.   exit 1
  64. fi
  65.  
  66. TV_PATH="${BASEDIR}/TV"
  67. [ -d "${TV_PATH}" ] ||  TV_PATH=""
  68.  
  69. if [ ! -d "$DOWNLOAD_PATH" ]; then
  70.   _logger  "The directory $DOWNLOAD_PATH does not exist!"
  71.   exit
  72. fi
  73.  
  74. if [ -z "$2" ] ; then
  75.   SUFFIX=avi
  76. else
  77.   SUFFIX=`echo "$2" |  tr '[A-Z]' '[a-z]'`
  78. fi
  79. UPPERSUFFIX=`echo "$SUFFIX" | tr '[a-z]' '[A-Z]'`
  80. DVD_PATH=${BASEDIR}/$UPPERSUFFIX
  81. DISKTYPE="CD"
  82. [ "${UPPERSUFFIX}" == "ISO" ] && DISKTYPE="DVD"
  83.  
  84. if [ ! -d "$DVD_PATH" ]; then
  85.   mkdir -p "$DVD_PATH"
  86. fi
  87.  
  88. # Garbage collecting
  89. cd "${DOWNLOAD_PATH}"
  90. if [ "${DOWNLOAD_PATH}" == "`pwd`" ] ; then
  91.   find -depth -type l -mtime +$AGE -exec rm {} 2>/dev/null \;
  92.   find -depth -type f -mtime +$AGE -name \.\*.info -exec rm {} 2>/dev/null \;
  93.   # find -depth -type f -mtime +$AGE -size -4M -exec rm {} 2>/dev/null \;
  94.  
  95.   find -iname \*\.${SUFFIX} -type l | while read SYMLINK ; do
  96.     if [ ! -e "${SYMLINK}" ]; then
  97.       rm -f "${SYMLINK}"
  98.       _logger "Removed ${SYMLINK}, because it was a dead symbolic link"
  99.     fi
  100.   done
  101.  
  102.   # Removing empty directories
  103.   find -depth -type d -empty -exec rmdir {} 2>/dev/null \;
  104. fi
  105.  
  106. DATAUSER=`stat -c %U "${DVD_PATH}" 2>/dev/null`
  107. if [ $? -eq 0 ] ; then
  108.   DATAGROUP=`stat -c %G "${DVD_PATH}"`
  109. else
  110.   DATAUSER=www-data
  111.   DATAGROUP=www-data
  112. fi
  113.  
  114. FTMP1=`mktemp`
  115. FTMP2=`mktemp`
  116. minsize="170000000"
  117.  
  118. cd "${DOWNLOAD_PATH}"
  119. find ${DOWNLOAD_PATH} -iname \*\.${SUFFIX} -type f >$FTMP1
  120.  
  121. while read f ; do
  122.  
  123.  _fileage
  124.  if [ $FILE_AGE -gt -30 ] && [ $FILE_AGE -lt 1800 ]; then
  125.    _logger "The filedate of $f is quite recent and is probably being created at this moment, skip..."
  126.  else
  127.    orgdir="`dirname "$f"`"
  128.    sizeiso=`stat -c%s "$f" 2>/dev/null`
  129.    if [ "${?}" -ne "0" ] ; then
  130.        sizeiso=$minsize
  131.    fi
  132.    if test $sizeiso -ge $minsize ; then
  133.  
  134.       cd "${orgdir}"
  135.  
  136.       SEASONDIR=""
  137.       CDEXT=""
  138.  
  139.       isofile="`basename "$f"`"
  140.       orgfile="$isofile"
  141.       orgfile_nosuffix="`echo $orgfile | sed -e s/.${SUFFIX}$// |  sed -e s/.${UPPERSUFFIX}$//`"
  142.       isofile="`echo "$isofile" | tr '[a-z]' '[A-Z]' | tr '[-]' '(-)' | sed 's/.'"$UPPERSUFFIX"'//g'`"
  143.  
  144.       # remove brackets and other characters, turn dots and underscores into spaces.
  145.       isofile="`echo "$isofile" | tr -d '{},\!"' | tr '.' ' '  | tr '-' ' '  | tr '_' ' '`"
  146.  
  147.       # Remove some words and strip double spaces afterwards.
  148.       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'`"
  149.       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'`"
  150.       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'`"
  151.       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'`"
  152.       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 ' '`"
  153.  
  154.       # Replace CD1, cda,  part1 or DVD with -CD1
  155.       CDEXT="`echo "$isofile" | grep -o "[ _.-]CD[ _.-]*[0-9A-D]" | head -n1`"
  156.       [ -z "${CDEXT}" ] && CDEXT="`echo "$isofile" | grep -o "[ _.-]DVD[ _.-]*[0-9A-D]" | head -n1`"
  157.       [ -z "${CDEXT}" ] && CDEXT="`echo "$isofile" | grep -o "[ _.-]PART[ _.-]*[0-9A-D]" | head -n1`"
  158.       [ -z "${CDEXT}" ] && CDEXT="`echo "$isofile" | grep -o "[ _.-]DIS[CK][ _.-]*[0-9A-D]" | head -n1`"
  159.       if [ ! -z "${CDEXT}" ] ; then
  160.         isofile="`echo "${isofile}" | sed -e s/"${CDEXT}"//g`"
  161.         CDEXT="-${DISKTYPE}`echo "${CDEXT#${CDEXT%?}}" | tr [a-d] [1-4]`"
  162.       fi
  163.       isofile="`echo "${isofile}" | sed -e 's/DVD//g'`"
  164.  
  165.       # Turn into lowercase with capitalized first letters.
  166.       isofile="`echo "$isofile" | sed 's/\<\(.\)\([^ ]*\)/\u\1\L\2/g'`"
  167.  
  168.       # Remove some more words
  169.       isofile="`echo "$isofile" | sed -e 's/ Rip//g' -e 's/ Nl//g'`"
  170.  
  171.       # Change some words into complete lowercase as they look awkward otherwise.
  172.       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' `"
  173.       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' `"
  174.       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' `"
  175.       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' `"
  176.       isofile="`echo "$isofile" | sed 's/()//g' | tr -s ' '`"
  177.  
  178.       # Delete YEAR and everything behind it....
  179.       YEAR=0
  180.       YEARPOS=`echo "${isofile}" | awk '{ print match($0,"[[({,._ ][12][90][0-9][0-9]") }'`
  181.       if [ ${YEARPOS} -gt 5 ]; then
  182.         YEAR=`echo "${isofile}" | grep -o "[[({,._ ][12][90][0-9][0-9]" | cut -b2-`
  183.         let YEARPOS-=1
  184.         isofile=`echo "${isofile}" | cut -b1-${YEARPOS} | sed 's/[_ ]$//g'`
  185.         _logger "Movie is of the year: ${YEAR}"
  186.       fi
  187.  
  188.       SEASON_EP=`echo "${isofile} " | egrep -o '[Ss][0-9][0-9].?[EeXx][0-9][ .0-9]'`
  189.       if [ ! -z "${SEASON_EP}" ] ; then
  190.         DELIM=`echo "${SEASON_EP}" | tr -cd EeXx`
  191.         SEASON="`echo "${SEASON_EP}" | tr -cd 0-9EeXx | awk -F${DELIM} '{print $1}' | tr -cd 0-9 | awk '{printf "%02d\n",$0}'`"
  192.             EP="`echo "${SEASON_EP}" | tr -cd 0-9EeXx | awk -F${DELIM} '{print $2}' | tr -cd 0-9 | awk '{printf "%02d\n",$0}'`"
  193.       else
  194.         SEASON_EP=`echo "${isofile} " | egrep -o '[ .0-9][0-9][Xx][0-9][ .0-9]'`
  195.         if [ ! -z "${SEASON_EP}" ] ; then
  196.           DELIM=`echo "${SEASON_EP}" | tr -cd Xx`
  197.           SEASON="`echo "${SEASON_EP}" | tr -cd 0-9Xx | awk -F${DELIM} '{print $1}' | tr -cd 0-9 | awk '{printf "%02d\n",$0}'`"
  198.               EP="`echo "${SEASON_EP}" | tr -cd 0-9Xx | awk -F${DELIM} '{print $2}' | tr -cd 0-9 | awk '{printf "%02d\n",$0}'`"
  199.         else
  200.           SEASON_EP=`echo "${isofile} " | egrep -o '[ .][0-6][0-5][0-9][ .]'`
  201.           if [ ! -z "${SEASON_EP}" ] ; then
  202.             SEASON="`echo "${SEASON_EP}" | tr -cd 0-9 | cut -b1-1 | awk '{printf "%02d\n",$0}'`"
  203.                 EP="`echo "${SEASON_EP}" | tr -cd 0-9 | cut -b2-  | awk '{printf "%02d\n",$0}'`"
  204.           else
  205.             SEASON_EP=`echo "${isofile} " | egrep -o '[ .]0[0-6][0-5][0-9][ .]'`
  206.             if [ ! -z "${SEASON_EP}" ] ; then
  207.               SEASON="`echo "${SEASON_EP}" | tr -cd 0-9 | cut -b1-2 | awk '{printf "%02d\n",$0}'`"
  208.                   EP="`echo "${SEASON_EP}" | tr -cd 0-9 | cut -b3-  | awk '{printf "%02d\n",$0}'`"
  209.             else
  210.               SEASON_EP=`echo "${isofile} " | egrep -o '[Ss._ -]*[0-6][._ -]*[EeXx][0-9][ .]*'`
  211.               if [ ! -z "${SEASON_EP}" ] ; then
  212.                  DELIM=`echo "${SEASON_EP}" | tr -cd EeXx`
  213.                 SEASON="`echo "${SEASON_EP}" | tr -cd 0-9EeXx | awk -F${DELIM} '{print $1}' | tr -cd 0-9 | awk '{printf "%02d\n",$0}'`"
  214.                     EP="`echo "${SEASON_EP}" | tr -cd 0-9EeXx | awk -F${DELIM} '{print $2}' | tr -cd 0-9 | awk '{printf "%02d\n",$0}'`"
  215.               fi
  216.             fi
  217.           fi
  218.         fi
  219.       fi
  220.  
  221.       if [ ! -z "${SEASON_EP}" ] ; then
  222.          SEASON_EP=`echo "${SEASON_EP}" | sed 's/^ *//;s/ *$//'`
  223.          SEASEP=" S${SEASON}E$EP "
  224.          isofile="`echo "${isofile}" | sed "s/${SEASON_EP}/${SEASEP}/"`"
  225.          SEASONPOS=`echo "${isofile}" | awk '{ print match($0,"S[0-9][0-9]E[0-9][0-9]") }'`
  226.          if [ ${SEASONPOS} -gt 3 ]; then
  227.            let SEASONPOS-=1
  228.            SERIE="`echo "${isofile}" | cut -b-${SEASONPOS} | tr ' _' '.' | tr -s '.' | sed 's/\.$//g'`"
  229.            let SEASONPOS+=6
  230.            isofile="`echo "${isofile}" | cut -b-${SEASONPOS}`"
  231.            [ -z "${TV_PATH}" ] || SEASONDIR="${TV_PATH}/${SERIE}/${SERIE}.S${SEASON}"
  232.          fi
  233.       else
  234.  
  235.         # IMDB code
  236.         simdb="`echo "$isofile" | tr '-' ' ' | tr -s ' ' | sed -e 's/ /_/1' -e 's/ /_/1'`"
  237.  
  238.         # Search IMDB, delete ANSI codes and remove accents from foreign characters
  239.         _logger "Searching imdb for ${simdb}"
  240.         rm -rf "$imdbinfo"
  241.         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
  242.  
  243.         if [ -f "${imdbinfo}" ]; then
  244.           lines=`wc -l ${imdbinfo} | awk '{print $1}'`
  245.           if [ "$lines" == "0" ]; then
  246.             _logger "imdb returned no data"
  247.           else
  248.             full_title=`grep -il "Title\:" "$imdbinfo"`
  249.             if [ -n "$full_title" ]; then
  250.               isofile=`cat "$imdbinfo" | grep "Title\:" | cut -b8- | tr -d '\"' `
  251.               _logger "IMDB came up with $isofile"
  252.             else
  253.               if [ "$lines" -gt "6" ]; then
  254.                 _logger "IMDB returned $lines matches, I will leave it untouched"
  255.                 if [ "${YEAR}" -ne 0 ]; then
  256.                   WITHYEAR=`cat "$imdbinfo" | grep ${YEAR} | head -n1 | cut -b9- | tr -d '\"'`
  257.                   if [ ! "" == "${WITHYEAR}" ]; then
  258.                     isofile="${WITHYEAR}"
  259.                     _logger "IMDB came up with $lines entries.. I took the first one which contained ${YEAR}: $isofile"
  260.                   fi
  261.                 fi
  262.               else
  263.                 firstline="`cat "$imdbinfo" | sed q`"
  264.                 colon=`echo "$firstline" | cut -b8`
  265.                 if [ "$colon" == ":" ]; then
  266.                   isofile="`echo "$firstline" | cut -b9- | tr -d '\"' `"
  267.                   _logger "IMDB came up with $lines entries.. I took the first named: $isofile"
  268.                 else
  269.                   echo "colon test failed"
  270.                 fi
  271.               fi
  272.             fi
  273.           fi
  274.         else
  275.           _logger "Unable to write to file: ${imdbinfo}"
  276.         fi
  277.       fi
  278.  
  279.       # Delete year if it's from the 21st Century
  280.       isofile="`echo "$isofile" | sed 's/(20[012][0-9])//g' | tr -cd "\-\&[].,:;'() 0-9a-zA-Z"`"
  281.  
  282.       # Turn spaces into dots, remove repeating dots and remove square brackets with nothing in between.
  283.       isofile="`echo "${isofile}" | tr ':' ';' | tr ' ' '.' | tr -s '.' | sed -e 's/\[\]//g' -e 's/^\.//;s/\.$//g'`${CDEXT}"
  284.       destfile_nosuffix="${isofile}"
  285.       isofile="${isofile}.${SUFFIX}"
  286.  
  287.       if [ -z "${SEASONDIR}" ]  ; then
  288.         DEST_PATH="${DVD_PATH}"
  289.       else
  290.         DEST_PATH="${SEASONDIR}"
  291.         if [ ! -d "${SEASONDIR}" ] ; then
  292.           mkdir -p "${SEASONDIR}" 2>/dev/null
  293.           chmod ug-x+rwX "${TV_PATH}"
  294.           grep -q ${DATAGROUP} /etc/group && chgrp -R ${DATAGROUP} "${TV_PATH}"
  295.           grep -q ${DATAUSER} /etc/passwd && chown -R  ${DATAUSER} "${TV_PATH}"
  296.         fi
  297.       fi
  298.  
  299.       isopathfile="${DEST_PATH}/${isofile}"
  300.  
  301.       if [ ! -e "${isopathfile}" ]; then
  302.  
  303.         _logger "Moving \"${f}\" to \"${isopathfile}\""
  304.         if [ ! "$orgfile" == "$isofile" ]; then
  305.           _logger "I renamed $orgfile to $isofile for better reading"
  306.         fi
  307.  
  308.         # If a subtitle with same name exists... move it too
  309.         find "${DOWNLOAD_PATH}" -type f -iname "${orgfile_nosuffix}*.rar" >${FTMP2}
  310.         while read rarfile ; do
  311.           if [ `stat -c%s "${rarfile}"` -lt 3000000 ]; then
  312.             unrar x -y "${rarfile}" 2>&1 >/dev/null
  313.             unrarerror=${?}
  314.             if [ "$REMOVE" == "0" ] && [ ${unrarerror} -eq 0 ] ; then
  315.               rm -f "${rarfile}"
  316.             fi
  317.           fi
  318.         done <${FTMP2}
  319.  
  320.         find "${DOWNLOAD_PATH}" -type f -iname "${orgfile_nosuffix}*.sub" >${FTMP2}
  321.         while read subfile ; do
  322.           _logger "sub-file ${subfile} found!"
  323.           subfile_nosuffix="`echo "${subfile}" | sed 's/.\{4\}$//'`"
  324.           subfile_nosuffix="`basename "${subfile_nosuffix}"`"
  325.           idxfile="`find "${DOWNLOAD_PATH}" -type f -iname "${subfile_nosuffix}.idx"  | head -n1`"
  326.           if [ -z "${idxfile}" ] ; then
  327.             _logger "NO idx-file found!"
  328.           else
  329.             _logger "idx-file ${idxfile} found!"
  330.           fi
  331.  
  332.           LANG=""
  333.           if [ `head -c50000 "${subfile}"  | grep -c "the "` -gt 25 ] ; then
  334.             LANG="eng."
  335.           elif [ `head -c50000 "${subfile}"  | grep -c "de "` -gt 25 ] ; then
  336.             LANG="dut."
  337.           fi
  338.  
  339.           idxfiledest="${DEST_PATH}/${destfile_nosuffix}.${LANG}idx"
  340.           subfiledest="${DEST_PATH}/${destfile_nosuffix}.${LANG}sub"
  341.           [ -f "${idxfile}" ] && mv "${idxfile}" "${idxfiledest}"
  342.           mv "${subfile}" "${subfiledest}"
  343.  
  344.           if [ "$REMOVE" == "0" ] ; then
  345.             ln -s "${idxfiledest}" "${idxfile}"
  346.             ln -s "${subfiledest}" "${subfile}"
  347.           fi
  348.  
  349.         done <${FTMP2}
  350.  
  351.         find "${DOWNLOAD_PATH}" -type f -iname "${orgfile_nosuffix}*.srt" >${FTMP2}
  352.         while read subfile ; do
  353.           _logger "srt-file ${subfile} found!"
  354.  
  355.           LANG=""
  356.           if   [ `grep -c "the " "${subfile}"` -gt 25 ] ; then
  357.             LANG="eng."
  358.           elif [ `grep -c "de "  "${subfile}"` -gt 25 ] ; then
  359.             LANG="dut."
  360.           fi
  361.  
  362.           subfiledest="${DEST_PATH}/${destfile_nosuffix}.${LANG}srt"
  363.           mv "${subfile}" "${subfiledest}"
  364.  
  365.           if [ "$REMOVE" == "0" ] ; then
  366.             ln -s "${subfiledest}" "${subfile}"
  367.           fi
  368.  
  369.         done <${FTMP2}
  370.  
  371.         mv "${f}" "${isopathfile}"
  372.         [ "$REMOVE" == "0" ] && ln -s "${isopathfile}" "${f}"
  373.  
  374.         chmod -R ug-x+rwX "${DEST_PATH}"
  375.         grep -q ${DATAGROUP} /etc/group && chgrp -R ${DATAGROUP} "${DEST_PATH}"
  376.         grep -q ${DATAUSER} /etc/passwd && chown -R  ${DATAUSER} "${DEST_PATH}"
  377.  
  378.       else
  379.         _logger "$isopathfile already exists.... maybe a split movie?"
  380.       fi
  381.       cd "${DOWNLOAD_PATH}"
  382.     fi
  383.   fi
  384. done <$FTMP1
  385.  
  386. rm -f $FTMP1
  387. rm -f $FTMP2
  388.  
  389. cd "${DOWNLOAD_PATH}"
  390.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement