lbry-sdk/lbrynet/daemon/Publisher.py

69 lines
3 KiB
Python
Raw Normal View History

import logging
import mimetypes
import os
from twisted.internet import defer
2017-04-03 21:58:20 +02:00
from lbrynet.core import file_utils
from lbrynet.file_manager.EncryptedFileCreator import create_lbry_file
from lbrynet.lbry_file.StreamDescriptor import publish_sd_blob
2016-06-07 10:19:51 +02:00
2016-01-21 04:00:28 +01:00
log = logging.getLogger(__name__)
class Publisher(object):
2017-04-07 02:45:05 +02:00
def __init__(self, session, lbry_file_manager, wallet, certificate_id):
2016-01-21 04:00:28 +01:00
self.session = session
self.lbry_file_manager = lbry_file_manager
self.wallet = wallet
2017-04-07 02:45:05 +02:00
self.certificate_id = certificate_id
2016-01-21 04:00:28 +01:00
self.lbry_file = None
@defer.inlineCallbacks
2017-06-12 19:32:01 +02:00
def create_and_publish_stream(self, name, bid, claim_dict, file_path, claim_address=None,
change_address=None):
2017-04-25 20:23:05 +02:00
"""Create lbry file and make claim"""
2016-08-11 02:04:03 +02:00
log.info('Starting publish for %s', name)
if not os.path.isfile(file_path):
raise Exception("File {} not found".format(file_path))
if os.path.getsize(file_path) == 0:
raise Exception("Cannot publish empty file {}".format(file_path))
file_name = os.path.basename(file_path)
with file_utils.get_read_handle(file_path) as read_handle:
stream_hash = yield create_lbry_file(self.session, self.lbry_file_manager, file_name,
read_handle)
sd_hash = yield publish_sd_blob(self.lbry_file_manager.stream_info_manager,
self.session.blob_manager, stream_hash)
self.lbry_file = yield self.lbry_file_manager.add_lbry_file(stream_hash)
2017-04-03 21:58:20 +02:00
if 'source' not in claim_dict['stream']:
claim_dict['stream']['source'] = {}
claim_dict['stream']['source']['source'] = sd_hash
claim_dict['stream']['source']['sourceType'] = 'lbry_sd_hash'
claim_dict['stream']['source']['contentType'] = get_content_type(file_path)
claim_dict['stream']['source']['version'] = "_0_0_1" # need current version here
2017-06-12 19:32:01 +02:00
claim_out = yield self.make_claim(name, bid, claim_dict, claim_address, change_address)
self.lbry_file.completed = True
yield self.lbry_file.save_status()
defer.returnValue(claim_out)
@defer.inlineCallbacks
2017-06-12 19:32:01 +02:00
def publish_stream(self, name, bid, claim_dict, claim_address=None, change_address=None):
2017-04-25 20:23:05 +02:00
"""Make a claim without creating a lbry file"""
2017-06-12 19:32:01 +02:00
claim_out = yield self.make_claim(name, bid, claim_dict, claim_address, change_address)
defer.returnValue(claim_out)
@defer.inlineCallbacks
2017-06-12 19:32:01 +02:00
def make_claim(self, name, bid, claim_dict, claim_address=None, change_address=None):
2017-04-07 02:45:05 +02:00
claim_out = yield self.wallet.claim_name(name, bid, claim_dict,
certificate_id=self.certificate_id,
2017-06-12 19:32:01 +02:00
claim_address=claim_address,
change_address=change_address)
defer.returnValue(claim_out)
2016-08-11 02:04:03 +02:00
def get_content_type(filename):
return mimetypes.guess_type(filename)[0] or 'application/octet-stream'