12345678910111213141516171819202122232425262728293031323334353637383940414243 |
- #! /usr/bin/env python
-
- import os
- import hashlib
- import bencode
- import torrententry
-
- class TorrentFile:
- data = ()
- info = ()
- def readFile(self, path):
- with open(path, 'rb') as f:
- bencoded = f.read()
- self.readBencoded(bencoded)
-
- def readBencoded(self, data):
- self.data = bencode.bdecode(data)
- self.info = self.data['info']
-
- def getName(self):
- return self.data['name']
-
- def getFiles(self):
- if 'files' in self.info:
- files = []
- for file in self.info['files']:
- files.append(torrententry.TorrentEntry(os.sep.join(file['path']), file['length']))
- return files
- else:
- return [torrententry.TorrentEntry(self.info['name'], self.info['length'])]
-
- def getSize(self):
- size = 0
- for file in self.getFiles():
- size += file.getSize()
- return size
-
- def getTracker(self):
- return self.data['announce']
-
- def getHash(self):
- bencoded = bencode.bencode(self.info)
- return hashlib.sha1(bencoded)
|