def get_metainfo(fname, url, errorfunc): with WarningLock(lambda *args: errorfunc("warning: bad data in metafile")): if fname: try: metainfo = MetaInfo.read(fname) except (OSError, TypeError, KeyError, ValueError): errorfunc(fname + ' is not a valid metafile') return None else: try: with urlopen(url) as handle: metainfo = MetaInfo(bdecode(handle.read())) except IOError as e: errorfunc('problem getting response info - ' + str(e)) return None except (TypeError, KeyError, ValueError): errorfunc(fname + ' is not a valid metafile') return None try: check_info(metainfo.get('info')) except ValueError as e: errorfunc("got bad file info - " + str(e)) return None return metainfo
def get_metainfo(fname, url, errorfunc): with WarningLock(lambda *args: errorfunc("warning: bad data in metafile")): if fname: try: metainfo = MetaInfo.read(fname) except (OSError, TypeError, KeyError, ValueError): errorfunc(fname + ' is not a valid metafile') return None else: try: metainfo = MetaInfo(bdecode(geturl(url))) except IOError as e: errorfunc('problem getting response info - ' + str(e)) return None except (TypeError, KeyError, ValueError): errorfunc(fname + ' is not a valid metafile') return None try: check_info(metainfo.get('info')) except ValueError as e: errorfunc("got bad file info - " + str(e)) return None return metainfo
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 (AssertionError, IOError): errorfunc(file + ' is not a valid responsefile') return None try: h.seek(0) except IOError: try: h.close() except IOError: pass h = open(file, 'rb') else: try: h = urlopen(url) except socket.error: 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 (IOError, socket.error): pass try: try: response = bdecode(response) except ValueError: 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