'Gets the torrent hash from either the magnet or torrent file content
params: :result: an instance of the searchResult class'

@staticmethod
def _get_torrent_hash(result):

    if result.url.startswith(u'magnet'):
        result.hash = re.findall(u'urn:btih:([\\w]{32,40})', result.url)[0]
        if (len(result.hash) == 32):
            result.hash = b16encode(b32decode(result.hash)).lower()
    else:
        if (not result.content):
            logger.log(u'Torrent    without    content', logger.ERROR)
            raise Exception(u'Torrent    without    content')
        try:
            torrent_bdecode = bencode.bdecode(result.content)
        except (bencode.BTL.BTFailure, Exception) as error:
            logger.log(u'Unable    to    bdecode    torrent', logger.ERROR)
            logger.log(u'Error    is:    {0}'.format(error), logger.DEBUG)
            raise
        try:
            info = torrent_bdecode['info']
        except Exception:
            logger.log(u'Unable    to    find    info    field    in    torrent', logger.ERROR)
            raise
        try:
            result.hash = sha1(bencode.bencode(info)).hexdigest()
            logger.log(u'Result    Hash    is    {0}'.format(result.hash), logger.DEBUG)
        except (bencode.BTL.BTFailure, Exception) as error:
            logger.log(u'Unable    to    bencode    torrent    info', logger.ERROR)
            logger.log(u'Error    is:    {0}'.format(error), logger.DEBUG)
            raise
    return result
