1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950 |
- #! /usr/bin/env python
-
- import copy
- import time
- import random
- import torrenttracker
-
- class TorrentManager:
- torrents = []
- next_torrents = []
- last_time = 0
- max_ratio = 0
- max_rate = 0
- def __init__(self, max_ratio = 1, max_rate = 120):
- self.max_ratio = max_ratio
- self.max_rate = max_rate * 1024
-
- def appendTorrent(self, torrent):
- self.torrents.append(torrent)
-
- def getTorrents(self):
- return self.torrents
-
- 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()
- uploaded = (self.max_rate / len(self.torrents)) * torrent.getTracker().getInterval()
- uploaded += random.randint(-uploaded / 10, uploaded / 10)
- self.next_torrents.remove(torrent)
- torrent.getTracker().update(uploaded)
|