Advertisement
Guest User

Untitled

a guest
Feb 12th, 2017
305
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 6.24 KB | None | 0 0
  1. #!/usr/bin/env bash
  2.  
  3. # Redirect STDOUT/STDERR to a separate file so we can see errors
  4. exec >> /tmp/plex-post.err
  5. exec 2>&1
  6.  
  7. # Set the GroupMe Bot ID
  8. . /etc/groupme.conf
  9. logfile=/tmp/plex-post.log
  10. # Set DEBUG to 0 for no debugging, 1 for log only, 2 for full alerting
  11. DEBUG=2
  12.  
  13. # I placed these in my home directory for easy access, make sure the "plex"
  14. # user has read access to them. We expect the following files:
  15. #   safe_to_cut: We don't care about backups.
  16. #   chapter_only: We want to set chapter breaks instead of cutting.
  17. #   skip_processing: We don't want to process at all.
  18. #   backup_and_cut: We want the original and a second file without commercials.
  19. #
  20. # In the default config, backup_and_cut is the default behavior but this can be
  21. # changed.
  22.  
  23. config_path="/home/jdbower/plex_post"
  24.  
  25. function log () {
  26.   logline="$1"
  27.   # Log it locally.
  28.   echo "$logline" >> $logfile
  29.   # JSON expects \n to be a literal '\n' and doesn't like double quotes.
  30.   logline=$(echo "$logline" | sed -E ':a;N;$!ba;s/\r{0,1}\n/\\n/g' | sed 's/\"/'\''/g')
  31.   # This posts to a GroupMe based logging mechanism, it can be swapped for
  32.   # Twitter, an email client, etc., as you see fit.
  33.   curl --silent -X POST -d '{"bot_id": "'$bot_id'", "text": "'"$logline"'"}' -H 'Content-Type: application/json' https://api.groupme.com/v3/bots/post
  34. }
  35.  
  36. function debug () {
  37.   case $DEBUG in
  38.     1)
  39.       # Just log locally.
  40.       echo "$1" >> $logfile
  41.     ;;
  42.     2)
  43.       # Run the full logging function.
  44.       log "$1"
  45.     ;;
  46.   esac
  47. }
  48.  
  49. # To get the appropriate scripts for commercial cutting use:
  50. # git clone git://github.com/BrettSheleski/comchap
  51. # https://www.ebower.com/wiki/Plex#Commercial_Detection_and_Removal
  52. #
  53. # cut_commercials=2: backup the original and try to skip the commercials.
  54. # cut_commercials=1: skip the commercials.
  55. # cut_commercials=0: create chapter marks for the commercials.
  56. # cut_commercials=-1: don't post process the commercials
  57.  
  58. cut_commercials=2
  59.  
  60. # The full path filename
  61. input=$1
  62. # Just the input filename.
  63. filename=$(basename "$input")
  64. # Remove the extension for the full show+episode name.
  65. title=${filename%.*}
  66. # The path to the library
  67. library_path=$(echo "$input" | awk -F'/.grab/' '{print $1}')
  68. # The directory in the library path where the show will live, also the show name
  69. dir=$(echo "$title" | awk -F') - ' '{print $1")"}')
  70. # For movies we need to strip the last )
  71. dir=$(echo "$dir" | sed 's/))/)/')
  72. # The season plus episode, don't use this directly as movies or odd values may
  73. # produce unexpected results.
  74. season_episode=$(echo "$title" | awk -F') - ' '{print $2")"}' | awk '{print $1}')
  75. # Split the season from the episode number.
  76. if [[ "$season_episode" =~ ^S[0-9]+E[0-9]+$ ]]; then
  77.   debug "Found normal season of the format SnEn for '$season_episode'"
  78.   season=$(echo $season_episode | awk -FE '{print $1}' | sed 's/^S//')
  79.   episode=$(echo $season_episode | awk -FE '{print $2}')
  80. elif [[ "$season_episode" =~ ^[0-9]{4}-[0-9]{2}-[0-9]{2}$ ]]; then
  81.   debug "Found date season of the format yyyy-mm-dd for '$season_episode'"
  82.   season=$(echo $season_episode | awk -F\- '{print $1}')
  83.   episode=$(echo $season_episode | awk -F\- '{print $2"-"$3}')
  84. else
  85.   debug "No season found for '$season_episode'"
  86.   season=""
  87.   episode=""
  88. fi
  89.  
  90.  
  91. # The format of the following files is one show per line, we search for:
  92. #   *Show Name* across the show name, excluding episode name.
  93. # When a show appears in multiple files, the least likely to cause content
  94. # loss will win. For example, we'd rather skip processing than set chapter
  95. # boundaries. Default behavior is to backup and cut, increasing storage costs.
  96.  
  97. for filename in backup_and_cut safe_to_cut chapter_only skip_processing; do
  98.   if [[ -f "${config_path}/$filename" ]]; then
  99.     while IFS='' read -r curr_title; do
  100.       if [[ "$curr_title" != "" ]]; then
  101.         if [[ "$dir" == *"$curr_title"* ]]; then
  102.            debug "Changing processing for '$dir' to $filename"
  103.            case $filename in
  104.              "backup_and_cut")
  105.                cut_commercials=2
  106.              ;;
  107.              "safe_to_cut")
  108.                cut_commercials=1
  109.              ;;
  110.              "chapter_only")
  111.                cut_commercials=0
  112.              ;;
  113.              "skip_processing")
  114.                cut_commercials=-1
  115.              ;;
  116.            esac
  117.         fi
  118.       fi
  119.     done < "${config_path}/$filename"
  120.   else
  121.     debug "'${config_path}/$filename' not found"
  122.   fi
  123. done
  124.  
  125. # Do a variable dump to the debug log.
  126. debug "input: '$@'
  127. filename: '$filename'
  128. title: '$title'
  129. library_path: '$library_path'
  130. dir: '$dir'
  131. season_episode: '$season_episode'
  132. season: '$season'
  133. episode: '$episode'
  134. cut_commercials: '$cut_commercials'"
  135.  
  136. # Some shows have false positives and skip content instead of commercials
  137. function chap_skip () {
  138.   log "Setting chapter boundaries for commercials on '$title'"
  139.   comchap "$input"
  140. }
  141.  
  142. # Some shows don't need processing
  143. function do_nothing () {
  144.   log "Skipping commercial cuts on '$title'"
  145. }
  146.  
  147. # Remove commercials
  148. function cut_comm () {
  149.   log "Cutting commercials on '$title'"
  150.   comcut "$input"
  151. }
  152.  
  153. # Make a backup for shows we're not certain of
  154. function backup_and_cut () {
  155.   # If the season variable is an integer use a season folder, otherwise don't
  156.   if [[ "$season" =~ ^[0-9]+$ ]]; then
  157.     dest_path="${library_path}/${dir}/Season ${season}"
  158.   else
  159.     dest_path="${library_path}/${dir}"
  160.   fi
  161.   log "Backing up ${title} to '${dest_path}/${title}-orig.ts'"
  162.   if [ ! -d "$dest_path" ]; then
  163.     debug "Making '$dest_path'"
  164.     mkdir -p "$dest_path"
  165.   fi
  166.   cp "$input" "${dest_path}/${title}-orig.ts"
  167.   cut_comm
  168. }
  169.  
  170. case $cut_commercials in
  171.   -1)
  172.     do_nothing
  173.   ;;
  174.   0)
  175.     chap_skip
  176.   ;;
  177.   2)
  178.     backup_and_cut
  179.   ;;
  180.   1)
  181.     cut_comm
  182.   ;;
  183. esac
  184.  
  185. # You can uncomment these to change the permissions on the files
  186. # debug "Setting permissions in '${library_path}/${dir}'"
  187. #find ${library_path}/${dir} -type f -exec chmod 664 {} \;
  188. #find ${library_path}/${dir} -type d -exec chmod 775 {} \;
  189.  
  190. # You can uncomment these to scan the library automatically
  191. # export LD_LIBRARY_PATH="${LD_LIBRARY_PATH}:/usr/lib/plexmediaserver"
  192. # debug "Rescanning Library"
  193. # /usr/lib/plexmediaserver/Plex\ Media\ Scanner -s &
  194.  
  195. debug "Finished processing '$title'"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement