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.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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. filepath = ""
  12. def readFile(self, path):
  13. self.filepath = path
  14. with open(path, 'rb') as f:
  15. bencoded = f.read()
  16. self.readBencoded(bencoded)
  17. self.tracker = torrenttracker.TorrentTracker(self)
  18. def readBencoded(self, data):
  19. self.data = bencode.bdecode(data)
  20. self.info = self.data['info']
  21. def getFilepath(self):
  22. return self.filepath
  23. def getName(self):
  24. return self.info['name']
  25. def getFiles(self):
  26. if 'files' in self.info:
  27. files = []
  28. for file in self.info['files']:
  29. files.append(torrententry.TorrentEntry(os.sep.join(file['path']), file['length']))
  30. return files
  31. else:
  32. return [torrententry.TorrentEntry(self.info['name'], self.info['length'])]
  33. def getSize(self):
  34. size = 0
  35. for file in self.getFiles():
  36. size += file.getSize()
  37. return size
  38. def getTracker(self):
  39. return self.tracker
  40. def getAnnounce(self):
  41. return self.data['announce']
  42. def getHash(self):
  43. bencoded = bencode.bencode(self.info)
  44. return hashlib.sha1(bencoded)