예제 #1
0
    def start(self, stream_info, name):
        def _cause_timeout(err):
            log.info('Cancelling download')
            self.timeout_counter = self.timeout * 2

        def _set_status(x, status):
            log.info("Download lbry://%s status changed to %s" %
                     (self.resolved_name, status))
            self.code = next(s for s in STREAM_STAGES if s[0] == status)
            return x

        def get_downloader_factory(metadata):
            for factory in metadata.factories:
                if isinstance(factory, ManagedEncryptedFileDownloaderFactory):
                    return factory, metadata
            raise Exception('No suitable factory was found in {}'.format(
                metadata.factories))

        def make_downloader(args):
            factory, metadata = args
            return factory.make_downloader(
                metadata, [self.data_rate, True],
                self.payment_rate_manager,
                download_directory=self.download_directory,
                file_name=self.file_name)

        self.resolved_name = name
        self.stream_info = deepcopy(stream_info)
        self.description = self.stream_info['description']
        self.stream_hash = self.stream_info['sources']['lbry_sd_hash']

        if 'fee' in self.stream_info:
            self.fee = FeeValidator(self.stream_info['fee'])
            max_key_fee = self._convert_max_fee()
            converted_fee = self.exchange_rate_manager.to_lbc(self.fee).amount
            if converted_fee > self.wallet.get_balance():
                log.warning("Insufficient funds to download lbry://%s",
                            self.resolved_name)
                return defer.fail(InsufficientFundsError())
            if converted_fee > max_key_fee:
                log.warning(
                    "Key fee %f above limit of %f didn't download lbry://%s",
                    converted_fee, max_key_fee, self.resolved_name)
                return defer.fail(KeyFeeAboveMaxAllowed())
            log.info("Key fee %f below limit of %f, downloading lbry://%s",
                     converted_fee, max_key_fee, self.resolved_name)

        self.checker.start(1)

        self.d.addCallback(lambda _: _set_status(None, DOWNLOAD_METADATA_CODE))
        self.d.addCallback(lambda _: download_sd_blob(
            self.session, self.stream_hash, self.payment_rate_manager))
        self.d.addCallback(self.sd_identifier.get_metadata_for_sd_blob)
        self.d.addCallback(lambda r: _set_status(r, DOWNLOAD_RUNNING_CODE))
        self.d.addCallback(get_downloader_factory)
        self.d.addCallback(make_downloader)
        self.d.addCallbacks(self._start_download, _cause_timeout)
        self.d.callback(None)

        return self.finished
예제 #2
0
 def check_fee(self, fee):
     validated_fee = FeeValidator(fee)
     max_key_fee = self.convert_max_fee()
     converted_fee = self.exchange_rate_manager.to_lbc(validated_fee).amount
     if converted_fee > self.wallet.get_balance():
         raise InsufficientFundsError('Unable to pay the key fee of %s' % converted_fee)
     if converted_fee > max_key_fee:
         raise KeyFeeAboveMaxAllowed('Key fee %s above max allowed %s' % (converted_fee,
                                                                          max_key_fee))
     return validated_fee
예제 #3
0
 def check_fee_and_convert(self, fee):
     max_key_fee_amount = self.convert_max_fee()
     converted_fee_amount = self.exchange_rate_manager.convert_currency(fee.currency, "LBC",
                                                                        fee.amount)
     if converted_fee_amount > self.wallet.get_balance():
         raise InsufficientFundsError('Unable to pay the key fee of %s' % converted_fee_amount)
     if converted_fee_amount > max_key_fee_amount:
         raise KeyFeeAboveMaxAllowed('Key fee %s above max allowed %s' % (converted_fee_amount,
                                                                          max_key_fee_amount))
     converted_fee = {
         'currency': 'LBC',
         'amount': converted_fee_amount,
         'address': fee.address
     }
     return Fee(converted_fee)
예제 #4
0
 def check_fee_and_convert(self, fee):
     max_key_fee_amount = self.convert_max_fee()
     converted_fee_amount = self.exchange_rate_manager.convert_currency(
         fee.currency, "LBC", fee.amount)
     if converted_fee_amount > (yield f2d(
             self.wallet.default_account.get_balance())):
         raise InsufficientFundsError('Unable to pay the key fee of %s' %
                                      converted_fee_amount)
     if converted_fee_amount > max_key_fee_amount and not self.disable_max_key_fee:
         raise KeyFeeAboveMaxAllowed(
             'Key fee {} above max allowed {}'.format(
                 converted_fee_amount, max_key_fee_amount))
     converted_fee = {
         'currency': 'LBC',
         'amount': converted_fee_amount,
         'address': fee.address
     }
     return Fee(converted_fee)
예제 #5
0
    def start(self, stream_info, name):
        def _cancel(err):
            # this callback sequence gets cancelled in check_status if
            # it takes too long when that happens, we want the logic
            # to live in check_status
            if err.check(defer.CancelledError):
                return
            if self.checker:
                self.checker.stop()
            self.finished.errback(err)

        def _set_status(x, status):
            log.info("Download lbry://%s status changed to %s" % (self.resolved_name, status))
            self.code = next(s for s in STREAM_STAGES if s[0] == status)
            return x

        def get_downloader_factory(metadata):
            for factory in metadata.factories:
                if isinstance(factory, ManagedEncryptedFileDownloaderFactory):
                    return factory, metadata
            raise Exception('No suitable factory was found in {}'.format(metadata.factories))

        def make_downloader(args):
            factory, metadata = args
            return factory.make_downloader(metadata,
                                           [self.data_rate, True],
                                           self.payment_rate_manager,
                                           download_directory=self.download_directory,
                                           file_name=self.file_name)

        self.resolved_name = name
        self.stream_info = deepcopy(stream_info)
        self.description = self.stream_info['description']
        self.sd_hash = self.stream_info['sources']['lbry_sd_hash']

        if 'fee' in self.stream_info:
            self.fee = FeeValidator(self.stream_info['fee'])
            max_key_fee = self._convert_max_fee()
            converted_fee = self.exchange_rate_manager.to_lbc(self.fee).amount
            if converted_fee > self.wallet.get_balance():
                msg = "Insufficient funds to download lbry://{}. Need {:0.2f}, have {:0.2f}".format(
                    self.resolved_name, converted_fee, self.wallet.get_balance())
                raise InsufficientFundsError(msg)
            if converted_fee > max_key_fee:
                msg = "Key fee {:0.2f} above limit of {:0.2f} didn't download lbry://{}".format(
                    converted_fee, max_key_fee, self.resolved_name)
                raise KeyFeeAboveMaxAllowed(msg)
            log.info(
                "Key fee %f below limit of %f, downloading lbry://%s",
                converted_fee, max_key_fee, self.resolved_name)

        self.checker.start(1)

        self._d.addCallback(lambda _: _set_status(None, DOWNLOAD_METADATA_CODE))
        self._d.addCallback(lambda _: download_sd_blob(
            self.session, self.sd_hash, self.payment_rate_manager))
        self._d.addCallback(self.sd_identifier.get_metadata_for_sd_blob)
        self._d.addCallback(lambda r: _set_status(r, DOWNLOAD_RUNNING_CODE))
        self._d.addCallback(get_downloader_factory)
        self._d.addCallback(make_downloader)
        self._d.addCallbacks(self._start_download, _cancel)
        self._d.callback(None)

        return self.finished