#! /usr/bin/env python import os import hashlib import bencode import torrententry import torrenttracker class TorrentFile: data = () info = () tracker = None def readFile(self, 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 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)