def calculate_torrent_hash(link, data=None):
 if link.startswith('magnet:'):
  torrent_hash = re.findall('urn:btih:([\\w]{32,40})', link)[0]
  if (len(torrent_hash) == 32):
   torrent_hash = b16encode(b32decode(torrent_hash)).lower()
 elif data:
  info = bdecode(data)['info']
  torrent_hash = sha1(bencode(info)).hexdigest()
 else:
  raise ValueError('Cannot calculate torrent hash without magnet link or data')
 return torrent_hash.upper()

def calculate_torrent_hash ( link , data = None ) : if link . startswith ( 'magnet:' ) : torrent_hash = re . findall ( 'urn:btih:([\\w]{32,40})' , link ) [ 0 ] if ( len ( torrent_hash ) == 32 ) : torrent_hash = b16encode ( b32decode ( torrent_hash ) ) . lower ( ) elif data : info = bdecode ( data ) [ 'info' ] torrent_hash = sha1 ( bencode ( info ) ) . hexdigest ( ) else : raise ValueError ( 'Cannot calculate torrent hash without magnet link or data' ) return torrent_hash . upper ( )
calculate the torrent hash from a magnet link or data .