diff --git a/lbry/lbry/extras/daemon/json_response_encoder.py b/lbry/lbry/extras/daemon/json_response_encoder.py index 496ceb145..3283b1fb7 100644 --- a/lbry/lbry/extras/daemon/json_response_encoder.py +++ b/lbry/lbry/extras/daemon/json_response_encoder.py @@ -218,15 +218,44 @@ class JSONResponseEncoder(JSONEncoder): return result def encode_file(self, managed_stream): - file = managed_stream.as_dict() + output_exists = managed_stream.output_file_exists tx_height = managed_stream.stream_claim_info.height best_height = self.ledger.headers.height - file.update({ + return { + 'streaming_url': managed_stream.stream_url, + 'completed': managed_stream.completed, + 'file_name': managed_stream.file_name if output_exists else None, + 'download_directory': managed_stream.download_directory if output_exists else None, + 'download_path': managed_stream.full_path if output_exists else None, + 'points_paid': 0.0, + 'stopped': not managed_stream.running, + 'stream_hash': managed_stream.stream_hash, + 'stream_name': managed_stream.descriptor.stream_name, + 'suggested_file_name': managed_stream.descriptor.suggested_file_name, + 'sd_hash': managed_stream.descriptor.sd_hash, + 'mime_type': managed_stream.mime_type, + 'key': managed_stream.descriptor.key, + 'total_bytes_lower_bound': managed_stream.descriptor.lower_bound_decrypted_length(), + 'total_bytes': managed_stream.descriptor.upper_bound_decrypted_length(), + 'written_bytes': managed_stream.written_bytes, + 'blobs_completed': managed_stream.blobs_completed, + 'blobs_in_stream': managed_stream.blobs_in_stream, + 'blobs_remaining': managed_stream.blobs_remaining, + 'status': managed_stream.status, + 'claim_id': managed_stream.claim_id, + 'txid': managed_stream.txid, + 'nout': managed_stream.nout, + 'outpoint': managed_stream.outpoint, + 'metadata': managed_stream.metadata, + 'protobuf': managed_stream.metadata_protobuf, + 'channel_claim_id': managed_stream.channel_claim_id, + 'channel_name': managed_stream.channel_name, + 'claim_name': managed_stream.claim_name, + 'content_fee': managed_stream.content_fee, 'height': tx_height, - 'confirmations': (best_height+1) - tx_height if tx_height > 0 else tx_height, + 'confirmations': (best_height + 1) - tx_height if tx_height > 0 else tx_height, 'timestamp': self.ledger.headers[tx_height]['timestamp'] if 0 < tx_height <= best_height else None - }) - return file + } def encode_claim(self, claim): encoded = getattr(claim, claim.claim_type).to_dict() diff --git a/lbry/lbry/stream/managed_stream.py b/lbry/lbry/stream/managed_stream.py index 67cf66fb2..a3ad25794 100644 --- a/lbry/lbry/stream/managed_stream.py +++ b/lbry/lbry/stream/managed_stream.py @@ -123,6 +123,14 @@ class ManagedStream: def written_bytes(self) -> int: return 0 if not self.output_file_exists else os.stat(self.full_path).st_size + @property + def completed(self): + return self.written_bytes >= self.descriptor.lower_bound_decrypted_length() + + @property + def stream_url(self): + return f"http://{self.config.streaming_host}:{self.config.streaming_port}/stream/{self.sd_hash}" + async def update_status(self, status: str): assert status in [self.STATUS_RUNNING, self.STATUS_STOPPED, self.STATUS_FINISHED] self._status = status @@ -203,47 +211,6 @@ class ManagedStream: def mime_type(self): return guess_media_type(os.path.basename(self.descriptor.suggested_file_name))[0] - def as_dict(self) -> typing.Dict: - full_path = self.full_path - file_name = self.file_name - download_directory = self.download_directory - if not self.output_file_exists: - full_path = None - file_name = None - download_directory = None - return { - 'streaming_url': f"http://{self.config.streaming_host}:{self.config.streaming_port}/stream/{self.sd_hash}", - 'completed': self.written_bytes >= self.descriptor.lower_bound_decrypted_length(), - 'file_name': file_name, - 'download_directory': download_directory, - 'points_paid': 0.0, - 'stopped': not self.running, - 'stream_hash': self.stream_hash, - 'stream_name': self.descriptor.stream_name, - 'suggested_file_name': self.descriptor.suggested_file_name, - 'sd_hash': self.descriptor.sd_hash, - 'download_path': full_path, - 'mime_type': self.mime_type, - 'key': self.descriptor.key, - 'total_bytes_lower_bound': self.descriptor.lower_bound_decrypted_length(), - 'total_bytes': self.descriptor.upper_bound_decrypted_length(), - 'written_bytes': self.written_bytes, - 'blobs_completed': self.blobs_completed, - 'blobs_in_stream': self.blobs_in_stream, - 'blobs_remaining': self.blobs_remaining, - 'status': self.status, - 'claim_id': self.claim_id, - 'txid': self.txid, - 'nout': self.nout, - 'outpoint': self.outpoint, - 'metadata': self.metadata, - 'protobuf': self.metadata_protobuf, - 'channel_claim_id': self.channel_claim_id, - 'channel_name': self.channel_name, - 'claim_name': self.claim_name, - 'content_fee': self.content_fee - } - @classmethod async def create(cls, loop: asyncio.AbstractEventLoop, config: 'Config', blob_manager: 'BlobManager', file_path: str, key: typing.Optional[bytes] = None, diff --git a/lbry/tests/unit/stream/test_managed_stream.py b/lbry/tests/unit/stream/test_managed_stream.py index d91a1dea8..8a881d8d3 100644 --- a/lbry/tests/unit/stream/test_managed_stream.py +++ b/lbry/tests/unit/stream/test_managed_stream.py @@ -43,11 +43,11 @@ class TestManagedStream(BlobExchangeTestBase): async def test_status_file_completed(self): await self._test_transfer_stream(10) self.assertTrue(self.stream.output_file_exists) - self.assertTrue(self.stream.as_dict()['completed']) + self.assertTrue(self.stream.completed) with open(self.stream.full_path, 'w+b') as outfile: outfile.truncate(1) self.assertTrue(self.stream.output_file_exists) - self.assertFalse(self.stream.as_dict()['completed']) + self.assertFalse(self.stream.completed) async def _test_transfer_stream(self, blob_count: int, mock_accumulate_peers=None, stop_when_done=True): await self.setup_stream(blob_count)