Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- # read file
- # if video, process as video, continue
- # if folder, process as folder, continue
- # else move it to other folder
- working="/media/Data/torrents/completed"
- target="/media/Data/TV/past - not watched"
- other="/media/Data/torrents/other downloads"
- ending='\.\([mM][pP]4\|[mM][oO][vV]\|[aA][vV][iI]\|[mM][kK][vV]\|3[gG][pP]\)'
- shopt -s extglob
- cd "${working}"
- rename()
- {
- # the LOL group often does not use the SxxExx episode format
- # find the episode information and rewrite it
- the_file="$1"
- # assume words separated by dots
- # split the filename into words
- OFS=$IFS ; IFS="."
- array=(${the_file[@]})
- IFS="$OFS"
- # length of array
- b=${#array[*]}
- # initialize new name to last element of array, usually file suffix
- new_name="${array[b-1]}"
- # cycle through the words, last one first
- do_it=0
- for ((a=b-2; -1<a; a--))
- do
- word="${array[a]}"
- # only do this for one word, skip the rest
- if [ $do_it == 0 ] ; then
- # if array element is all numbers and length is 3 or 4, change the array element
- # strip all numbers from array element
- z=$(echo ${word} | sed "s/[[:digit:]]//g")
- # if length of resulting element is 0, it was all numbers
- if [ ${#z} == 0 ] ; then
- if [ ${#array[a]} == 3 ] ; then
- word="S0${word:0:1}E${word: -2}"
- do_it=1
- elif [ ${#array[a]} == 4 ] ; then
- word="S${word:0:2}E${word: -2}"
- do_it=1
- fi
- fi
- fi
- new_name="${word}.${new_name}"
- done
- echo "$new_name"
- }
- video()
- {
- the_video="$1"
- # get the suffix
- suffix="${the_video: -4}"
- # get short title without suffix
- short_title="${the_video: 0: -4}"
- # if short title contains LOL, rename the season, episode designator
- [ "$(echo "$short_title" | grep '[\.\-][lL][oO][lL]')" ] && short_title="$(rename "$short_title")"
- # get folder title from short title, minus episode info
- folder_title="$(echo "$short_title" | sed 's/\.[s|S][0|1|2|3].*$//')"
- # get episode info and rest of short title by deleting the folder title and uppercasing
- episode_title="$(echo "$short_title" | sed "s/^$folder_title//" | sed 's/./\U&/g')"
- # lowercase all of folder title and then uppercase first letters
- folder_title="$(echo "$folder_title" | sed 's/./\L&/g' | sed 's/^./\U&\E/' | sed 's/[\.\ ]./\U&\E/g')"
- # new video title is folder title + episode title + suffix
- video_title="$folder_title$episode_title$suffix"
- # if a folder for show doesn't exist, make it
- [ -d "${target}/${folder_title}" ] || mkdir "${target}/${folder_title}"
- # move the video to the show folder and rename
- mv "$the_video" "${target}/${folder_title}/${video_title}"
- return 0
- }
- folder()
- {
- the_folder="$1"
- # enter the folder and look for video file
- # if video file found, process it and flag folder for deletion
- # note: curly brackets preserve values of variables
- found_video=false
- cd "$working/$the_folder"
- ls -1 | \
- { while read j
- do
- suffix="${j: -4}"
- [ "$(echo "$suffix" | grep "$ending")" ] && found_video=true && video "$j"
- done
- # back out of folder and delete if video file found
- cd ../
- [ "$found_video" == true ] && rm -rf "$working/$the_folder"
- # if video file not found, move it to the other folder
- [ "$found_video" == false ] && mv "$working/$the_folder" "$other/"
- }
- return 0
- }
- remove-torrent()
- {
- task="$1"
- # if task exists, remove the task
- [ "$task" != "" ] && transmission-remote -t "$task" -r
- }
- main()
- {
- ls -1 | while read i
- do
- # is the file done seeding?
- ret="$(transmission-remote -l | grep -F "$i" | awk '{ print $1" "$5 }')"
- job="$(echo "$ret" | awk '{ print $1 }')"
- job="${job:0:1}"
- stat1="$(echo "$ret" | awk '{ print $2 }')"
- # if job is blank, no torrent exists, so process the file
- # if stat1 = Done, the torrent is done, so process the file
- [ "$job" == "" ] || [ "$stat1" == "Done" ] || continue
- # if name doesn't contain 'tv' then not sure what it is, so move it to other folder
- [ "$(echo "${i}" | grep -v '[tT][vV]')" ] && mv "${i}" "$other/" && remove-torrent "$job" && continue
- # is it a directory
- [ -d "${i}" ] && folder "$i" && remove-torrent "$job" && continue
- # is it a video file
- suffix="${i: -4}"
- [ "$(echo "$suffix" | grep "$ending")" ] && video "$i" && remove-torrent "$job" && continue
- # shouldn't get here, but if it does, just file it
- mv "${i}" "$other/" && remove-torrent "$job"
- done
- }
- main
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement