Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

torrentfile.py 1.1KB

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