def validateRSS(self):  # pylint: disable=too-many-return-statements

        try:
            if self.cookies:
                cookie_validator = re.compile(r'^(\w+=\w+)(;\w+=\w+)*$')
                if not cookie_validator.match(self.cookies):
                    return False, 'Cookie is not correctly formatted: {0}'.format(
                        self.cookies)
                add_dict_to_cookiejar(
                    self.session.cookies,
                    dict(x.rsplit('=', 1) for x in self.cookies.split(';')))

            # pylint: disable=protected-access
            # Access to a protected member of a client class
            data = self.cache._getRSSData()['entries']
            if not data:
                return False, 'No items found in the RSS feed {0}'.format(
                    self.url)

            title, url = self._get_title_and_url(data[0])

            if not title:
                return False, 'Unable to get title from first item'

            if not url:
                return False, 'Unable to get torrent url from first item'

            if url.startswith('magnet:') and re.search(
                    r'urn:btih:([\w]{32,40})', url):
                return True, 'RSS feed Parsed correctly'
            else:
                torrent_file = self.get_url(url, returns='content')
                try:
                    Bencoder().decode(torrent_file)
                except (BencodeDecodeError, Exception) as error:
                    self.dumpHTML(torrent_file)
                    return False, 'Torrent link is not a valid torrent file: {0}'.format(
                        error)

            return True, 'RSS feed Parsed correctly'

        except Exception as error:
            return False, 'Error when trying to load RSS: {0}'.format(
                ex(error))
Exemple #2
0
    def _get_torrent_hash(result):

        if result.url.startswith('magnet'):
            result.hash = re.findall(r'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('Torrent without content')

            bencoder = Bencoder()
            try:
                torrent_bdecode = bencoder.decode(result.content)
            except (BencodeDecodeError, Exception) as error:
                logger.log(u'Unable to bdecode torrent', logger.ERROR)
                logger.log(u'Error is: {0!r}'.format(error), logger.DEBUG)
                logger.log(
                    u'Torrent bencoded data: {0!r}'.format(result.content),
                    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(bencoder.encode(info)).hexdigest()
            except (BencodeEncodeError, Exception) as error:
                logger.log(u'Unable to bencode torrent info', logger.ERROR)
                logger.log(u'Error is: {0!r}'.format(error), logger.DEBUG)
                logger.log(
                    u'Torrent bencoded data: {0!r}'.format(result.content),
                    logger.DEBUG)
                raise

        return result