Advertisement
Guest User

transplex.sh

a guest
Mar 31st, 2016
582
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.61 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3.   ########################################################
  4.   #                                                      #
  5.   # transplex.sh                                         #
  6.   # Created by KronK0321                                 #
  7.   # Used for controlling Transmission's upstream limit   #
  8.   #  based on outgoing Plex streams.                     #
  9.   #                                                      #
  10.   ########################################################
  11.  
  12. # Desired Transmission upload speeds in KB/s
  13. maxupspeed=1850
  14. minupspeed=350
  15.  
  16. # Transmission info
  17. tr_hostname=localhost
  18. tr_port=9091
  19. tr_username=USERNAME
  20. tr_password=PASSWORD
  21.  
  22. # Plex account token
  23. token=REDACTED
  24.  
  25. # Local network
  26. local_ip=192.168.1
  27.  
  28. # How long, in seconds, between checking for outgoing plex streams
  29. delay=10
  30.  
  31. while true; do
  32.  
  33.         totalbitrate=0
  34.  
  35.         while read LINE; do
  36.  
  37.                 if [[ $LINE == \<Video* ]]; then
  38.                         # start of a video section
  39.                         bitrate=0
  40.                         isremote=0
  41.  
  42.                 elif [[ $LINE == \<Stream* ]]; then
  43.                         # each stream entry is read and parsed to extract the bitrate
  44.                         streambitrate=$(echo $LINE | grep -o bitrate=\"[0-9]* | sed 's/bitrate=\"//g')
  45.                         if [[ $streambitrate =~ [0-9]+$ ]]; then
  46.                                 let bitrate+=$streambitrate
  47.                         fi
  48.  
  49.                 elif [[ $LINE == \<Player* ]]; then
  50.                         # determine if is a remote stream
  51.                         ip=$(echo $LINE | grep -o address=\"[0-9]*\.[0-9]*\.[0-9]*\.[0-9]* | sed 's/address=\"//g')
  52.                         if [[ ! $ip =~ $local_ip ]]; then
  53.                                 isremote=1
  54.                         fi
  55.  
  56.                 elif [[ $LINE == *\<\/Video\>* ]]; then
  57.                         # end of a video section, count this if its remote
  58.                         if [[ $isremote = 1 ]]; then
  59.                                 let totalbitrate+=$bitrate
  60.                         fi
  61.                 fi
  62.  
  63.         # pull the information from plex web app
  64.         done < <(curl --silent localhost:32400/status/sessions?X-Plex-Token=$token)
  65.  
  66.         # need to work in Kb/s
  67.         upspeed=$(( $maxupspeed - ( $totalbitrate / 8 ) ))
  68.  
  69.         # never set the upload speed below specified value
  70.         if [[ $upspeed -lt $minupspeed ]]; then
  71.                 upspeed=$minupspeed
  72.         fi
  73.  
  74.         /usr/bin/transmission-remote $tr_hostname:$tr_port -n "$tr_username":"$tr_password" -u $upspeed
  75.  
  76.         sleep $delay
  77. done;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement