You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

fake-tracker.py 1.9KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. #! /usr/bin/env python
  2. from __future__ import print_function
  3. import sys
  4. import os
  5. import time
  6. import glob
  7. import torrentfile
  8. import torrentmanager
  9. import config
  10. def warning(*objs):
  11. print(time.strftime("%d/%m/%Y %H:%M:%S"), "WARNING:", *objs, file=sys.stderr)
  12. def debug(*objs):
  13. print(time.strftime("%d/%m/%Y %H:%M:%S"), *objs)
  14. if len(sys.argv) >= 2:
  15. config = config.Config(sys.argv[1])
  16. else:
  17. config = config.Config()
  18. maxRatio = config.getMaxRatio()
  19. maxRate = config.getMaxRate()
  20. torrentsPath = config.getTorrentsPath()
  21. print("Max ratio:", maxRatio)
  22. print("Max rate:", maxRate, "kb/s")
  23. print("Torrents path:", torrentsPath)
  24. manager = torrentmanager.TorrentManager(maxRatio, maxRate)
  25. run = True
  26. while run:
  27. for file in glob.glob(torrentsPath + "/*.torrent"):
  28. torrent = torrentfile.TorrentFile()
  29. try:
  30. torrent.readFile(file)
  31. except Exception as e:
  32. os.remove(file)
  33. warning("could not load", file, e)
  34. try:
  35. if not any(x.getName() == torrent.getName()
  36. for x in manager.getTorrents()):
  37. torrent.getTracker().start()
  38. debug("started", torrent.getName())
  39. manager.appendTorrent(torrent)
  40. except Exception as e:
  41. warning("could not start", torrent.getName(), e)
  42. count = len(manager.getTorrents())
  43. if count == 0:
  44. warning("could not find/load/start any torrent. Waiting 5 minutes")
  45. time.sleep(300)
  46. continue
  47. t = manager.getNextTime()
  48. torrent = t['torrent']
  49. secs = t['time']
  50. if secs > 0:
  51. debug(torrent.getName() + ": Next update in " + str(secs) + " seconds")
  52. time.sleep(secs)
  53. debug("Updating", torrent.getName(),
  54. "ratio =", (torrent.getTracker().getUploaded() / float(torrent.getSize())))
  55. try:
  56. manager.updateTorrent()
  57. except:
  58. warning("could not update")