def getTorrentData(self, torrent): """Read a torrent data file from cache""" if torrent in self.torrentDataBuffer: return self.torrentDataBuffer[torrent] fname = os.path.join(self.dir_datacache, hexlify(torrent)) if not os.path.exists(fname): return None try: data = MetaInfo.read(fname) except (IOError, ValueError): data = None self.torrentDataBuffer[fname] = data return data
def getTorrent(self, torrent, version=-1): """Return the contents of a torrent file If version is -1 (default), get the most recent. If version is specified and > -1, retrieve specified version.""" torrent = hexlify(torrent) fname = os.path.join(self.dir_torrentcache, torrent) if version == -1: version = max(self.getTorrentVariations(torrent)) if version: fname += '.' + str(version) try: return MetaInfo.read(fname) except (IOError, ValueError): return None
def parse_torrent(path, return_metainfo=False): """Load and derive metadata from torrent file Parameters str - path of file to parse bool - parsed metadata to include full torrent data Returns {str: *} - torrent file metadata str - sha hash of encoded info dict """ fname = os.path.basename(path) data = MetaInfo.read(path) # Validate and hash info dict info = data['info'] check_info(info) infohash = hashlib.sha1(bencode(info)).digest() single = 'length' in info torrentinfo = { 'path': path, 'file': fname, 'name': info.get('name', fname), 'numfiles': 1 if single else len(info['files']), 'length': info['length'] if single else sum(li['length'] for li in info['files'] if 'length' in li) } for key in ('failure reason', 'warning message', 'announce-list'): if key in data: torrentinfo[key] = data[key] if return_metainfo: torrentinfo['metainfo'] = data return torrentinfo, infohash
def reannounce(fname, announce, announce_list=None, verbose=False): """Replace announce and announce-list in info file""" metainfo = MetaInfo.read(fname) if verbose: print 'old announce for %s: %s' % (fname, metainfo['announce']) metainfo['announce'] = announce if 'announce-list' in metainfo: if verbose: print 'old announce-list for {}: {}'.format( fname, '|'.join(','.join(tier) for tier in metainfo['announce-list'])) if announce_list is not None: metainfo['announce-list'] = announce_list else: metainfo.pop('announce-list', None) metainfo.write(fname)
def parse_torrent(path, return_metainfo=False): """Load and derive metadata from torrent file Parameters str - path of file to parse bool - parsed metadata to include full torrent data Returns {str: *} - torrent file metadata str - sha hash of encoded info dict """ fname = os.path.basename(path) data = MetaInfo.read(path) # Validate and hash info dict info = data["info"] check_info(info) infohash = hashlib.sha1(bencode(info)).digest() single = "length" in info torrentinfo = { "path": path, "file": fname, "name": info.get("name", fname), "numfiles": 1 if single else len(info["files"]), "length": info["length"] if single else sum(li["length"] for li in info["files"] if "length" in li), } for key in ("failure reason", "warning message", "announce-list"): if key in data: torrentinfo[key] = data[key] if return_metainfo: torrentinfo["metainfo"] = data return torrentinfo, infohash