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.

torrentfile.py 1.2KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. #! /usr/bin/env python
  2. import os
  3. import hashlib
  4. import bencode
  5. import torrententry
  6. import torrenttracker
  7. class TorrentFile:
  8. data = ()
  9. info = ()
  10. tracker = None
  11. def readFile(self, path):
  12. with open(path, 'rb') as f:
  13. bencoded = f.read()
  14. self.readBencoded(bencoded)
  15. self.tracker = torrenttracker.TorrentTracker(self)
  16. def readBencoded(self, data):
  17. self.data = bencode.bdecode(data)
  18. self.info = self.data['info']
  19. def getName(self):
  20. return self.info['name']
  21. def getFiles(self):
  22. if 'files' in self.info:
  23. files = []
  24. for file in self.info['files']:
  25. files.append(torrententry.TorrentEntry(os.sep.join(file['path']), file['length']))
  26. return files
  27. else:
  28. return [torrententry.TorrentEntry(self.info['name'], self.info['length'])]
  29. def getSize(self):
  30. size = 0
  31. for file in self.getFiles():
  32. size += file.getSize()
  33. return size
  34. def getTracker(self):
  35. return self.tracker
  36. def getAnnounce(self):
  37. return self.data['announce']
  38. def getHash(self):
  39. bencoded = bencode.bencode(self.info)
  40. return hashlib.sha1(bencoded)