Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

torrentmanager.py 1.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. #! /usr/bin/env python
  2. import copy
  3. import time
  4. import random
  5. import torrenttracker
  6. class TorrentManager:
  7. torrents = []
  8. next_torrents = []
  9. last_time = 0
  10. max_ratio = 0
  11. max_rate = 0
  12. def __init__(self, max_ratio = 1, max_rate = 120):
  13. self.max_ratio = max_ratio
  14. self.max_rate = max_rate * 1024
  15. def appendTorrent(self, torrent):
  16. self.torrents.append(torrent)
  17. def getTorrents(self):
  18. return self.torrents
  19. def resetNextTorrents(self):
  20. self.last_time = int(time.time())
  21. self.next_torrents = copy.copy(self.torrents)
  22. def getNextTorrent(self):
  23. if not any(self.next_torrents):
  24. self.resetNextTorrents()
  25. minTorrent = self.next_torrents[0]
  26. for torrent in self.next_torrents:
  27. if (torrent.getTracker().getInterval()
  28. < minTorrent.getTracker().getInterval()):
  29. minTorrent = torrent
  30. return minTorrent
  31. def getNextTime(self):
  32. torrent = self.getNextTorrent()
  33. t = max(torrent.getTracker().getInterval() - (int(time.time()) - self.last_time), 0)
  34. return {'time': t, 'torrent': torrent}
  35. def updateTorrent(self, torrent = None):
  36. if torrent == None:
  37. torrent = self.getNextTorrent()
  38. uploaded = (self.max_rate / len(self.torrents)) * torrent.getTracker().getInterval()
  39. uploaded += random.randint(-uploaded / 10, uploaded / 10)
  40. self.next_torrents.remove(torrent)
  41. torrent.getTracker().update(uploaded)