|  | @@ -1,5 +1,6 @@
 | 
		
	
		
			
			| 1 | 1 |  #! /usr/bin/env python
 | 
		
	
		
			
			| 2 | 2 |  
 | 
		
	
		
			
			|  | 3 | +import os
 | 
		
	
		
			
			| 3 | 4 |  import copy
 | 
		
	
		
			
			| 4 | 5 |  import time
 | 
		
	
		
			
			| 5 | 6 |  import random
 | 
		
	
	
		
			
			|  | @@ -11,13 +12,16 @@ class TorrentManager:
 | 
		
	
		
			
			| 11 | 12 |      last_time = 0
 | 
		
	
		
			
			| 12 | 13 |      max_ratio = 0
 | 
		
	
		
			
			| 13 | 14 |      max_rate = 0
 | 
		
	
		
			
			| 14 |  | -    def __init__(self, max_ratio = 1, max_rate = 120):
 | 
		
	
		
			
			|  | 15 | +    def __init__(self, max_ratio, max_rate):
 | 
		
	
		
			
			| 15 | 16 |          self.max_ratio = max_ratio
 | 
		
	
		
			
			| 16 | 17 |          self.max_rate = max_rate * 1024
 | 
		
	
		
			
			| 17 | 18 |  
 | 
		
	
		
			
			| 18 | 19 |      def appendTorrent(self, torrent):
 | 
		
	
		
			
			| 19 | 20 |          self.torrents.append(torrent)
 | 
		
	
		
			
			| 20 | 21 |  
 | 
		
	
		
			
			|  | 22 | +    def removeTorrent(self, torrent):
 | 
		
	
		
			
			|  | 23 | +        self.torrents.remove(torrent)
 | 
		
	
		
			
			|  | 24 | +
 | 
		
	
		
			
			| 21 | 25 |      def getTorrents(self):
 | 
		
	
		
			
			| 22 | 26 |          return self.torrents
 | 
		
	
		
			
			| 23 | 27 |  
 | 
		
	
	
		
			
			|  | @@ -40,11 +44,16 @@ class TorrentManager:
 | 
		
	
		
			
			| 40 | 44 |          t = max(torrent.getTracker().getInterval() - (int(time.time()) - self.last_time), 0)
 | 
		
	
		
			
			| 41 | 45 |          return {'time': t, 'torrent': torrent}
 | 
		
	
		
			
			| 42 | 46 |  
 | 
		
	
		
			
			| 43 |  | -    def updateTorrent(self, torrent = None):
 | 
		
	
		
			
			| 44 |  | -        if torrent == None:
 | 
		
	
		
			
			| 45 |  | -            torrent = self.getNextTorrent()
 | 
		
	
		
			
			| 46 |  | -        uploaded = (self.max_rate / len(self.torrents)) * torrent.getTracker().getInterval()
 | 
		
	
		
			
			|  | 47 | +    def updateTorrent(self):
 | 
		
	
		
			
			|  | 48 | +        torrent = self.getNextTorrent()
 | 
		
	
		
			
			|  | 49 | +        uploaded = int(self.max_rate / len(self.torrents)) * torrent.getTracker().getInterval()
 | 
		
	
		
			
			| 47 | 50 |          uploaded += random.randint(-uploaded / 10, uploaded / 10)
 | 
		
	
		
			
			| 48 | 51 |          self.next_torrents.remove(torrent)
 | 
		
	
		
			
			|  | 52 | +        if torrent.getSize() * self.max_ratio <= torrent.getTracker().getUploaded():
 | 
		
	
		
			
			|  | 53 | +            self.torrents.remove(torrent)
 | 
		
	
		
			
			|  | 54 | +            try:
 | 
		
	
		
			
			|  | 55 | +                os.remove(torrent.getFilepath())
 | 
		
	
		
			
			|  | 56 | +            except:
 | 
		
	
		
			
			|  | 57 | +                print("WARNING: failed to remove torrent", torrent.getFilepath())
 | 
		
	
		
			
			| 49 | 58 |          torrent.getTracker().update(uploaded)
 | 
		
	
		
			
			| 50 | 59 |  
 |