#! /usr/bin/env python from __future__ import print_function import sys import time import glob import torrentfile import torrentmanager import config def warning(*objs): print("WARNING:", *objs, file=sys.stderr) if len(sys.argv) >= 2: config = config.Config(sys.argv[1]) else: config = config.Config() maxRatio = config.getMaxRatio() maxRate = config.getMaxRate() torrentsPath = config.getTorrentsPath() print("Max ratio:", maxRatio) print("Max rate:", maxRate, "kb/s") print("Torrents path:", torrentsPath) manager = torrentmanager.TorrentManager(maxRatio, maxRate) run = True while run: for file in glob.glob(torrentsPath + "/*.torrent"): try: torrent = torrentfile.TorrentFile() torrent.readFile(file) if not any(x.getName() == torrent.getName() for x in manager.getTorrents()): print("Found", torrent.getName()) torrent.getTracker().start() manager.appendTorrent(torrent) except: warning("could not load or start", file) count = len(manager.getTorrents()) if count == 0: warning("could not find/load/start any torrent. Waiting 5 minutes") time.sleep(300) continue t = manager.getNextTime() torrent = t['torrent'] secs = t['time'] if secs > 0: print(torrent.getName() + ": Next update in " + str(secs) + " seconds") time.sleep(secs) print("Updating", torrent.getName(), "ratio =", (torrent.getTracker().getUploaded() / float(torrent.getSize()))) try: manager.updateTorrent() except: warning("could not update")