2016-05-05 05:27:40 +02:00
|
|
|
import logging
|
2016-06-27 23:07:59 +02:00
|
|
|
import mimetypes
|
2016-05-05 05:27:40 +02:00
|
|
|
import os
|
|
|
|
|
2017-02-09 22:12:30 +01:00
|
|
|
from twisted.internet import defer
|
|
|
|
from lbrynet.core import file_utils
|
2016-09-27 20:18:16 +02:00
|
|
|
from lbrynet.lbryfilemanager.EncryptedFileCreator import create_lbry_file
|
2016-01-21 04:00:28 +01:00
|
|
|
from lbrynet.lbryfile.StreamDescriptor import publish_sd_blob
|
2016-09-27 20:18:35 +02:00
|
|
|
from lbrynet.metadata.Metadata import Metadata
|
2016-06-07 10:19:51 +02:00
|
|
|
|
|
|
|
|
2016-01-21 04:00:28 +01:00
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
class Publisher(object):
|
|
|
|
def __init__(self, session, lbry_file_manager, wallet):
|
|
|
|
self.session = session
|
|
|
|
self.lbry_file_manager = lbry_file_manager
|
|
|
|
self.wallet = wallet
|
|
|
|
self.lbry_file = None
|
|
|
|
|
2017-03-30 21:12:07 +02:00
|
|
|
"""
|
|
|
|
Create lbry file and make claim
|
|
|
|
"""
|
2017-02-09 22:12:30 +01:00
|
|
|
@defer.inlineCallbacks
|
2017-03-30 21:12:07 +02:00
|
|
|
def create_and_publish_stream(self, name, bid, metadata, file_path):
|
2016-08-11 02:04:03 +02:00
|
|
|
log.info('Starting publish for %s', name)
|
2017-02-09 22:12:30 +01:00
|
|
|
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)
|
2016-09-27 19:52:44 +02:00
|
|
|
prm = self.session.payment_rate_manager
|
2017-02-09 22:12:30 +01:00
|
|
|
self.lbry_file = yield self.lbry_file_manager.add_lbry_file(stream_hash, prm)
|
|
|
|
sd_hash = yield publish_sd_blob(self.lbry_file_manager.stream_info_manager,
|
|
|
|
self.session.blob_manager, self.lbry_file.stream_hash)
|
|
|
|
if 'sources' not in metadata:
|
|
|
|
metadata['sources'] = {}
|
|
|
|
metadata['sources']['lbry_sd_hash'] = sd_hash
|
|
|
|
metadata['content_type'] = get_content_type(file_path)
|
|
|
|
metadata['ver'] = Metadata.current_version
|
|
|
|
claim_out = yield self.make_claim(name, bid, metadata)
|
2017-02-16 15:09:21 +01:00
|
|
|
self.lbry_file.completed = True
|
|
|
|
yield self.lbry_file.load_file_attributes()
|
|
|
|
yield self.lbry_file.save_status()
|
2017-02-09 22:12:30 +01:00
|
|
|
defer.returnValue(claim_out)
|
|
|
|
|
2017-03-30 21:12:07 +02:00
|
|
|
"""
|
|
|
|
Make a claim without creating a lbry file
|
|
|
|
"""
|
|
|
|
@defer.inlineCallbacks
|
|
|
|
def publish_stream(self, name, bid, metadata):
|
|
|
|
claim_out = yield self.make_claim(name, bid, metadata)
|
|
|
|
defer.returnValue(claim_out)
|
|
|
|
|
2017-02-09 22:12:30 +01:00
|
|
|
@defer.inlineCallbacks
|
|
|
|
def update_stream(self, name, bid, metadata):
|
|
|
|
my_claim = yield self.wallet.get_my_claim(name)
|
|
|
|
updated_metadata = my_claim['value']
|
|
|
|
for meta_key in metadata:
|
|
|
|
updated_metadata[meta_key] = metadata[meta_key]
|
|
|
|
claim_out = yield self.make_claim(name, bid, updated_metadata)
|
|
|
|
defer.returnValue(claim_out)
|
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
|
|
|
def make_claim(self, name, bid, metadata):
|
|
|
|
validated_metadata = Metadata(metadata)
|
|
|
|
claim_out = yield self.wallet.claim_name(name, bid, validated_metadata)
|
|
|
|
defer.returnValue(claim_out)
|
2016-08-11 02:04:03 +02:00
|
|
|
|
|
|
|
|
|
|
|
def get_content_type(filename):
|
2016-10-14 15:55:45 +02:00
|
|
|
return mimetypes.guess_type(filename)[0] or 'application/octet-stream'
|