|  | @@ -1,35 +1,56 @@
 | 
		
	
		
			
			| 1 | 1 |  #! /usr/bin/env python
 | 
		
	
		
			
			| 2 | 2 |  
 | 
		
	
		
			
			|  | 3 | +from __future__ import print_function
 | 
		
	
		
			
			| 3 | 4 |  import sys
 | 
		
	
		
			
			| 4 | 5 |  import time
 | 
		
	
		
			
			|  | 6 | +import glob
 | 
		
	
		
			
			| 5 | 7 |  import torrentfile
 | 
		
	
		
			
			| 6 |  | -import torrenttracker
 | 
		
	
		
			
			| 7 | 8 |  import torrentmanager
 | 
		
	
		
			
			| 8 | 9 |  import config
 | 
		
	
		
			
			| 9 | 10 |  
 | 
		
	
		
			
			|  | 11 | +def warning(*objs):
 | 
		
	
		
			
			|  | 12 | +        print("WARNING:", *objs, file=sys.stderr)
 | 
		
	
		
			
			|  | 13 | +
 | 
		
	
		
			
			| 10 | 14 |  if len(sys.argv) >= 2:
 | 
		
	
		
			
			| 11 | 15 |      config = config.Config(sys.argv[1])
 | 
		
	
		
			
			| 12 | 16 |  else:
 | 
		
	
		
			
			| 13 | 17 |      config = config.Config()
 | 
		
	
		
			
			| 14 | 18 |  
 | 
		
	
		
			
			| 15 | 19 |  maxRatio = config.getMaxRatio()
 | 
		
	
		
			
			| 16 |  | -maxRate = config.getMaxRatio()
 | 
		
	
		
			
			| 17 |  | -
 | 
		
	
		
			
			| 18 |  | -trackers = []
 | 
		
	
		
			
			| 19 |  | -for file in ('torrent.torrent', 'torrent2.torrent'):
 | 
		
	
		
			
			| 20 |  | -    torrent = torrentfile.TorrentFile()
 | 
		
	
		
			
			| 21 |  | -    torrent.readFile(file)
 | 
		
	
		
			
			| 22 |  | -    trackers.append(torrent)
 | 
		
	
		
			
			|  | 20 | +maxRate = config.getMaxRate()
 | 
		
	
		
			
			|  | 21 | +torrentsPath = config.getTorrentsPath()
 | 
		
	
		
			
			| 23 | 22 |  
 | 
		
	
		
			
			| 24 |  | -manager = torrentmanager.TorrentManager(trackers)
 | 
		
	
		
			
			| 25 |  | -manager.start()
 | 
		
	
		
			
			|  | 23 | +manager = torrentmanager.TorrentManager(maxRatio, maxRate)
 | 
		
	
		
			
			| 26 | 24 |  run = True
 | 
		
	
		
			
			| 27 | 25 |  while run:
 | 
		
	
		
			
			|  | 26 | +    for file in glob.glob(torrentsPath + "/*.torrent"):
 | 
		
	
		
			
			|  | 27 | +        try:
 | 
		
	
		
			
			|  | 28 | +            torrent = torrentfile.TorrentFile()
 | 
		
	
		
			
			|  | 29 | +            torrent.readFile(file)
 | 
		
	
		
			
			|  | 30 | +            if not any(x.getName() == torrent.getName()
 | 
		
	
		
			
			|  | 31 | +                    for x in manager.getTorrents()):
 | 
		
	
		
			
			|  | 32 | +                print("Found", torrent.getName())
 | 
		
	
		
			
			|  | 33 | +                torrent.getTracker().start()
 | 
		
	
		
			
			|  | 34 | +                manager.appendTorrent(torrent)
 | 
		
	
		
			
			|  | 35 | +        except:
 | 
		
	
		
			
			|  | 36 | +            warning("could not load or start", file)
 | 
		
	
		
			
			|  | 37 | +
 | 
		
	
		
			
			|  | 38 | +    count = len(manager.getTorrents())
 | 
		
	
		
			
			|  | 39 | +
 | 
		
	
		
			
			|  | 40 | +    if count == 0:
 | 
		
	
		
			
			|  | 41 | +        warning("could not find/load/start any torrent. Waiting 5 minutes")
 | 
		
	
		
			
			|  | 42 | +        time.sleep(300)
 | 
		
	
		
			
			|  | 43 | +        continue
 | 
		
	
		
			
			|  | 44 | +
 | 
		
	
		
			
			| 28 | 45 |      t = manager.getNextTime()
 | 
		
	
		
			
			| 29 | 46 |      torrent = t['torrent']
 | 
		
	
		
			
			| 30 | 47 |      secs = t['time']
 | 
		
	
		
			
			| 31 |  | -    print(torrent.getName() + ": Next update in " + str(secs) + " seconds")
 | 
		
	
		
			
			| 32 |  | -    time.sleep(secs)
 | 
		
	
		
			
			| 33 |  | -    print("Updating...")
 | 
		
	
		
			
			| 34 |  | -    manager.updateNextTracker()
 | 
		
	
		
			
			|  | 48 | +    if secs > 0:
 | 
		
	
		
			
			|  | 49 | +        print(torrent.getName() + ": Next update in " + str(secs) + " seconds")
 | 
		
	
		
			
			|  | 50 | +        time.sleep(secs)
 | 
		
	
		
			
			|  | 51 | +    print("Updating", torrent.getName())
 | 
		
	
		
			
			|  | 52 | +    try:
 | 
		
	
		
			
			|  | 53 | +        manager.updateTorrent(torrent)
 | 
		
	
		
			
			|  | 54 | +    except:
 | 
		
	
		
			
			|  | 55 | +        warning("could not update")
 | 
		
	
		
			
			| 35 | 56 |  
 |