1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859 |
- #! /usr/bin/env python
-
- import os
- 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, max_rate):
- self.max_ratio = max_ratio
- self.max_rate = max_rate * 1024
-
- def appendTorrent(self, torrent):
- self.torrents.append(torrent)
-
- def removeTorrent(self, torrent):
- self.torrents.remove(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 = self.getNextTorrent()
- uploaded = int(self.max_rate / len(self.torrents)) * torrent.getTracker().getInterval()
- uploaded += random.randint(-uploaded / 10, uploaded / 10)
- self.next_torrents.remove(torrent)
- if torrent.getSize() * self.max_ratio <= torrent.getTracker().getUploaded():
- self.torrents.remove(torrent)
- try:
- os.remove(torrent.getFilepath())
- except:
- print("WARNING: failed to remove torrent", torrent.getFilepath())
- torrent.getTracker().update(uploaded)
|