123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354 |
- #! /usr/bin/env python
-
- import os
- import hashlib
- import bencode
- import torrententry
- import torrenttracker
-
- class TorrentFile:
- data = ()
- info = ()
- tracker = None
- filepath = ""
- def readFile(self, path):
- self.filepath = path
- with open(path, 'rb') as f:
- bencoded = f.read()
- self.readBencoded(bencoded)
- self.tracker = torrenttracker.TorrentTracker(self)
-
- def readBencoded(self, data):
- self.data = bencode.bdecode(data)
- self.info = self.data['info']
-
- def getFilepath(self):
- return self.filepath
-
- def getName(self):
- return self.info['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.tracker
-
- def getAnnounce(self):
- return self.data['announce']
-
- def getHash(self):
- bencoded = bencode.bencode(self.info)
- return hashlib.sha1(bencoded)
|