def get_response(file, url, errorfunc): try: if file: h = open(file, 'rb') try: # quick test to see if responsefile contains a dict line = h.read(10) front = line.split(':', 1)[0] assert front[0] == 'd' int(front[1:]) except: errorfunc(file + ' is not a valid responsefile') return None try: h.seek(0) except: try: h.close() except: pass h = open(file, 'rb') else: try: h = urlopen(url) except: errorfunc(url + ' bad url') return None response = h.read() except IOError as e: errorfunc('problem getting response info - ' + str(e)) return None try: h.close() except: pass try: try: response = bdecode(response) except: errorfunc("warning: bad data in responsefile") response = bdecode(response, sloppy=1) check_type(response, dict) check_info(response.get('info')) check_type(response.get('announce'), str) except ValueError as e: errorfunc("got bad file info - " + str(e)) return None return response
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 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