#! /usr/bin/env python from __future__ import print_function import sys import os import time import glob import torrentfile import torrentmanager import config def warning(*objs): print(time.strftime("%d/%m/%Y %H:%M:%S"), "WARNING:", *objs, file=sys.stderr) def debug(*objs): print(time.strftime("%d/%m/%Y %H:%M:%S"), *objs) 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"): torrent = torrentfile.TorrentFile() try: torrent.readFile(file) except Exception as e: os.remove(file) warning("could not load", file, e) try: if not any(x.getName() == torrent.getName() for x in manager.getTorrents()): torrent.getTracker().start() debug("started", torrent.getName()) manager.appendTorrent(torrent) except Exception as e: warning("could not start", torrent.getName(), e) 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: debug(torrent.getName() + ": Next update in " + str(secs) + " seconds") time.sleep(secs) debug("Updating", torrent.getName(), "ratio =", (torrent.getTracker().getUploaded() / float(torrent.getSize()))) try: manager.updateTorrent() except: warning("could not update")