Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

torrentmanager.py 1.8KB

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