123456789101112131415161718192021222324252627282930313233343536373839404142 |
- #! /usr/bin/env python
-
- import copy
- import time
- import torrenttracker
-
- class TorrentManager:
- torrents = []
- next_torrents = []
- last_time = 0
- def __init__(self, torrents):
- self.torrents = torrents
-
- def start(self):
- for torrent in self.torrents:
- torrent.getTracker().start()
-
- def resetNextTorrents(self):
- self.last_time = int(time.time())
- self.next_torrents = copy.copy(self.torrents)
-
- def getNextTorrent(self):
- if not any(self.next_torrents):
- self.resetNextTorrents()
- minTorrent = self.next_torrents[0]
- for torrent in self.next_torrents:
- if (torrent.getTracker().getInterval()
- < minTorrent.getTracker().getInterval()):
- minTorrent = torrent
- return minTorrent
-
- def getNextTime(self):
- torrent = self.getNextTorrent()
- t = max(torrent.getTracker().getInterval() - (int(time.time()) - self.last_time), 0)
- return {'time': t, 'torrent': torrent}
-
- def updateTorrent(self, torrent = None):
- if torrent == None:
- torrent = self.getNextTorrent()
- torrent.getTracker().interval(0, 0, 0)
- self.next_torrents.remove(torrent)
|