Advertisement
Guest User

Untitled

a guest
Nov 28th, 2014
256
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.43 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. # read file
  4. # if video, process as video, continue
  5. # if folder, process as folder, continue
  6. # else move it to other folder
  7.  
  8.  
  9. working="/media/Data/torrents/completed"
  10. target="/media/Data/TV/past - not watched"
  11. other="/media/Data/torrents/other downloads"
  12. ending='\.\([mM][pP]4\|[mM][oO][vV]\|[aA][vV][iI]\|[mM][kK][vV]\|3[gG][pP]\)'
  13.  
  14. shopt -s extglob
  15. cd "${working}"
  16.  
  17.  
  18. rename()
  19. {
  20. # the LOL group often does not use the SxxExx episode format
  21. # find the episode information and rewrite it
  22. the_file="$1"
  23. # assume words separated by dots
  24. # split the filename into words
  25.  
  26. OFS=$IFS ; IFS="."
  27. array=(${the_file[@]})
  28. IFS="$OFS"
  29.  
  30. # length of array
  31. b=${#array[*]}
  32.  
  33. # initialize new name to last element of array, usually file suffix
  34. new_name="${array[b-1]}"
  35.  
  36. # cycle through the words, last one first
  37. do_it=0
  38. for ((a=b-2; -1<a; a--))
  39. do
  40.  
  41. word="${array[a]}"
  42. # only do this for one word, skip the rest
  43. if [ $do_it == 0 ] ; then
  44.  
  45. # if array element is all numbers and length is 3 or 4, change the array element
  46. # strip all numbers from array element
  47. z=$(echo ${word} | sed "s/[[:digit:]]//g")
  48.  
  49. # if length of resulting element is 0, it was all numbers
  50. if [ ${#z} == 0 ] ; then
  51. if [ ${#array[a]} == 3 ] ; then
  52. word="S0${word:0:1}E${word: -2}"
  53. do_it=1
  54. elif [ ${#array[a]} == 4 ] ; then
  55. word="S${word:0:2}E${word: -2}"
  56. do_it=1
  57. fi
  58. fi
  59. fi
  60.  
  61. new_name="${word}.${new_name}"
  62. done
  63.  
  64. echo "$new_name"
  65. }
  66.  
  67.  
  68. video()
  69. {
  70. the_video="$1"
  71.  
  72. # get the suffix
  73. suffix="${the_video: -4}"
  74.  
  75. # get short title without suffix
  76. short_title="${the_video: 0: -4}"
  77.  
  78. # if short title contains LOL, rename the season, episode designator
  79. [ "$(echo "$short_title" | grep '[\.\-][lL][oO][lL]')" ] && short_title="$(rename "$short_title")"
  80.  
  81. # get folder title from short title, minus episode info
  82. folder_title="$(echo "$short_title" | sed 's/\.[s|S][0|1|2|3].*$//')"
  83.  
  84. # get episode info and rest of short title by deleting the folder title and uppercasing
  85. episode_title="$(echo "$short_title" | sed "s/^$folder_title//" | sed 's/./\U&/g')"
  86.  
  87. # lowercase all of folder title and then uppercase first letters
  88. folder_title="$(echo "$folder_title" | sed 's/./\L&/g' | sed 's/^./\U&\E/' | sed 's/[\.\ ]./\U&\E/g')"
  89.  
  90. # new video title is folder title + episode title + suffix
  91. video_title="$folder_title$episode_title$suffix"
  92.  
  93. # if a folder for show doesn't exist, make it
  94. [ -d "${target}/${folder_title}" ] || mkdir "${target}/${folder_title}"
  95.  
  96. # move the video to the show folder and rename
  97. mv "$the_video" "${target}/${folder_title}/${video_title}"
  98.  
  99. return 0
  100. }
  101.  
  102. folder()
  103. {
  104. the_folder="$1"
  105.  
  106. # enter the folder and look for video file
  107. # if video file found, process it and flag folder for deletion
  108. # note: curly brackets preserve values of variables
  109. found_video=false
  110. cd "$working/$the_folder"
  111. ls -1 | \
  112. { while read j
  113. do
  114. suffix="${j: -4}"
  115. [ "$(echo "$suffix" | grep "$ending")" ] && found_video=true && video "$j"
  116. done
  117. # back out of folder and delete if video file found
  118. cd ../
  119. [ "$found_video" == true ] && rm -rf "$working/$the_folder"
  120. # if video file not found, move it to the other folder
  121. [ "$found_video" == false ] && mv "$working/$the_folder" "$other/"
  122. }
  123. return 0
  124. }
  125.  
  126. remove-torrent()
  127. {
  128. task="$1"
  129. # if task exists, remove the task
  130. [ "$task" != "" ] && transmission-remote -t "$task" -r
  131. }
  132.  
  133.  
  134. main()
  135. {
  136. ls -1 | while read i
  137. do
  138. # is the file done seeding?
  139. ret="$(transmission-remote -l | grep -F "$i" | awk '{ print $1" "$5 }')"
  140. job="$(echo "$ret" | awk '{ print $1 }')"
  141. job="${job:0:1}"
  142. stat1="$(echo "$ret" | awk '{ print $2 }')"
  143. # if job is blank, no torrent exists, so process the file
  144. # if stat1 = Done, the torrent is done, so process the file
  145. [ "$job" == "" ] || [ "$stat1" == "Done" ] || continue
  146.  
  147. # if name doesn't contain 'tv' then not sure what it is, so move it to other folder
  148. [ "$(echo "${i}" | grep -v '[tT][vV]')" ] && mv "${i}" "$other/" && remove-torrent "$job" && continue
  149.  
  150. # is it a directory
  151. [ -d "${i}" ] && folder "$i" && remove-torrent "$job" && continue
  152.  
  153. # is it a video file
  154. suffix="${i: -4}"
  155. [ "$(echo "$suffix" | grep "$ending")" ] && video "$i" && remove-torrent "$job" && continue
  156.  
  157. # shouldn't get here, but if it does, just file it
  158. mv "${i}" "$other/" && remove-torrent "$job"
  159. done
  160. }
  161.  
  162. main
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement