Guest User

Sab automated priority

a guest
Apr 11th, 2012
236
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 2.75 KB | None | 0 0
  1. #!/usr/bin/perl
  2.  
  3. use Proc::Daemon;
  4. use HTTP::Tiny;
  5. use JSON;
  6.  
  7.  
  8. $daemon = Proc::Daemon->new();
  9.  
  10. $sabSlowchecker = $daemon->Init;
  11.  
  12. unless ( $sabSlowchecker ) {
  13.  
  14.  
  15.     my $settings = {
  16.         sabIP       => '10.0.0.10',
  17.         sabPort     => '8080',
  18.         sabAPIKey   => 'asdasdc5debca44fc195cb46asdasdasd',
  19.         minkbSec    => 2000, #~ threshold in Kb/s before flagging download as slow
  20.         threshold   => 2, #~ needs to be below minkbSec for at least this amount of loops (interval * threshold = time in seconds), 0 for immediate
  21.         interval    => 15, #~ time in seconds between checks
  22.        
  23.     };
  24.  
  25.  
  26.     my $httpClient = HTTP::Tiny->new;
  27.     my $markedAsSlow = 0;
  28.     my $SlowIncr = 0;
  29.  
  30.  
  31.  
  32.     while(1){
  33.         checkSpeed();
  34.         sleep $settings->{interval}
  35.     }
  36.  
  37.  
  38.  
  39.     sub checkSpeed
  40.     {
  41.            
  42.         my $queue = getData();
  43.        
  44.         if(!$queue){
  45.             #~ paused, no que, not reachable, bla bla
  46.             return 0;
  47.         }
  48.        
  49.        
  50.         #~ no need to check if we are were we need to be when we already are where we are supposed to be.
  51.         if($queue->{speedlimit} ne ''){
  52.            
  53.             #~ dont want to trigger too soon
  54.             $threshold = int(0.9 * $queue->{speedlimit});
  55.            
  56.             if(int($queue->{kbpersec}) >= $threshold){
  57.                 #~ we're at max speed
  58.                 return 0;
  59.             }
  60.            
  61.         }  
  62.        
  63.        
  64.        
  65.         my $first = $queue->{slots}[0];    
  66.        
  67.         if(!$first || $first->{priority} eq 'Low'){
  68.             #~ we need faster downloads!
  69.             return 0;
  70.         }
  71.        
  72.         my @his = split(':',$first->{timeleft});
  73.         my $seconds = ( $his[0] * 3600 ) + ( $his[1] * 60 ) + $his[2];
  74.        
  75.         if($seconds < 1){
  76.             #~ yes, I like it shaved
  77.             return 0;
  78.         }
  79.  
  80.  
  81.         my $kbsec = int(($first->{mbleft} / $seconds) * 1024);
  82.        
  83.        
  84.         if($kbsec < $settings->{minkbSec}){
  85.            
  86.             #~ ok, it's slow now..
  87.             if($markedAsSlow && $markedAsSlow eq $first->{nzo_id}){
  88.                
  89.                 $SlowIncr++;
  90.                
  91.                 if(($SlowIncr) < $settings->{threshold}){
  92.                     return 1;
  93.                 }
  94.                
  95.                 my $set = setPriority($first->{nzo_id});
  96.                
  97.                 #~ if ok, free result
  98.                 if($set){
  99.                     $markedAsSlow = 0;
  100.                     $SlowIncr = 0;
  101.                 }
  102.                 return 1;
  103.             }
  104.            
  105.             $markedAsSlow = $first->{nzo_id};      
  106.             return 0;
  107.         }
  108.        
  109.     }
  110.  
  111.  
  112.     sub getData
  113.     {
  114.        
  115.         my $response = $httpClient->get('http://' . $settings->{sabIP} . ':' . $settings->{sabPort} . '/api?mode=queue&start=0&limit=1&output=json&apikey=' . $settings->{sabAPIKey});
  116.        
  117.         if(!$response->{success}){
  118.             return 0;      
  119.         }
  120.        
  121.         my $jsonDecode = decode_json($response->{content});
  122.        
  123.        
  124.         if($jsonDecode->{queue}->{paused_all}){
  125.             return 0;
  126.         }
  127.         return $jsonDecode->{queue};
  128.  
  129.     }
  130.  
  131.     sub setPriority
  132.     {
  133.         my ($nzo_id) = @_;
  134.         my $response = $httpClient->get('http://' . $settings->{sabIP} . ':' . $settings->{sabPort} . '/api?mode=queue&name=priority&value=' . $nzo_id . '&value2=-1&apikey=' . $settings->{sabAPIKey});
  135.        
  136.         return $response->{success};
  137.     }
  138.  
  139. }
Advertisement
Add Comment
Please, Sign In to add comment