daemon settings

-set_settings accepts a dict of settings
    -adds upload_log field, defaulting on true, this uploads the lbry
log file to lbry.io to help figure out if and where things aren’t
working
    -default_download_directory is the key of the path where files are
saved

-gets publish working with sources

-adds check_first_run function, returns True/False

-previously only is_running would work during startup, check_first_run,
 get_time_behind_blockchain, and stop have been added

-requires six 1.9, 1.10.0 produced errors
This commit is contained in:
Jack 2016-04-07 03:12:09 -04:00
parent 7b653b90a8
commit a5362f3170
10 changed files with 238 additions and 95 deletions

View file

@ -4,5 +4,5 @@ import logging
logging.getLogger(__name__).addHandler(logging.NullHandler()) logging.getLogger(__name__).addHandler(logging.NullHandler())
version = (0, 2, 0) version = (0, 2, 1)
__version__ = ".".join([str(x) for x in version]) __version__ = ".".join([str(x) for x in version])

View file

@ -365,7 +365,8 @@ class LBRYWallet(object):
value['content_license'] = content_license value['content_license'] = content_license
if author is not None: if author is not None:
value['author'] = author value['author'] = author
if sources is not None: if isinstance(sources, dict):
sources['lbry_sd_hash'] = sd_hash
value['sources'] = sources value['sources'] = sources
d = self._send_name_claim(name, json.dumps(value), amount) d = self._send_name_claim(name, json.dumps(value), amount)
@ -436,7 +437,8 @@ class LBRYWallet(object):
d.addCallback(set_first_run) d.addCallback(set_first_run)
else: else:
d = defer.succeed(None) d = defer.succeed(self._FIRST_RUN_YES if self._first_run else self._FIRST_RUN_NO)
d.addCallback(lambda _: self._first_run == self._FIRST_RUN_YES) d.addCallback(lambda _: self._first_run == self._FIRST_RUN_YES)
return d return d
@ -1072,6 +1074,7 @@ class LBRYumWallet(LBRYWallet):
return decoded_tx return decoded_tx
def _send_abandon(self, txid, address, amount): def _send_abandon(self, txid, address, amount):
log.info("Abandon " + str(txid) + " " + str(address) + " " + str(amount))
cmd = known_commands['abandonclaim'] cmd = known_commands['abandonclaim']
func = getattr(self.cmd_runner, cmd.name) func = getattr(self.cmd_runner, cmd.name)
d = threads.deferToThread(func, txid, address, amount) d = threads.deferToThread(func, txid, address, amount)
@ -1079,6 +1082,7 @@ class LBRYumWallet(LBRYWallet):
return d return d
def _broadcast_transaction(self, raw_tx): def _broadcast_transaction(self, raw_tx):
log.info("Broadcast: " + str(raw_tx))
cmd = known_commands['broadcast'] cmd = known_commands['broadcast']
func = getattr(self.cmd_runner, cmd.name) func = getattr(self.cmd_runner, cmd.name)
d = threads.deferToThread(func, raw_tx) d = threads.deferToThread(func, raw_tx)

View file

@ -119,7 +119,7 @@ class ManagedLBRYFileDownloaderFactory(object):
def can_download(self, sd_validator): def can_download(self, sd_validator):
return True return True
def make_downloader(self, metadata, options, payment_rate_manager): def make_downloader(self, metadata, options, payment_rate_manager, download_directory=None):
data_rate = options[0] data_rate = options[0]
upload_allowed = options[1] upload_allowed = options[1]
@ -137,7 +137,8 @@ class ManagedLBRYFileDownloaderFactory(object):
d.addCallback(lambda stream_hash: self.lbry_file_manager.add_lbry_file(stream_hash, d.addCallback(lambda stream_hash: self.lbry_file_manager.add_lbry_file(stream_hash,
payment_rate_manager, payment_rate_manager,
data_rate, data_rate,
upload_allowed)) upload_allowed,
download_directory=download_directory))
return d return d
@staticmethod @staticmethod

View file

@ -28,7 +28,7 @@ class LBRYFileManager(object):
Keeps track of currently opened LBRY Files, their options, and their LBRY File specific metadata. Keeps track of currently opened LBRY Files, their options, and their LBRY File specific metadata.
""" """
def __init__(self, session, stream_info_manager, sd_identifier, delete_data=False): def __init__(self, session, stream_info_manager, sd_identifier, delete_data=False, download_directory=None):
self.session = session self.session = session
self.stream_info_manager = stream_info_manager self.stream_info_manager = stream_info_manager
self.sd_identifier = sd_identifier self.sd_identifier = sd_identifier
@ -36,8 +36,8 @@ class LBRYFileManager(object):
self.sql_db = None self.sql_db = None
# self.delete_data = delete_data # self.delete_data = delete_data
# self.check_exists_loop = LoopingCall(self.check_files_exist) # self.check_exists_loop = LoopingCall(self.check_files_exist)
if sys.platform.startswith("darwin"): if download_directory:
self.download_directory = os.path.join(os.path.expanduser("~"), 'Downloads') self.download_directory = download_directory
else: else:
self.download_directory = os.getcwd() self.download_directory = os.getcwd()
log.debug("Download directory for LBRYFileManager: %s", str(self.download_directory)) log.debug("Download directory for LBRYFileManager: %s", str(self.download_directory))
@ -122,7 +122,10 @@ class LBRYFileManager(object):
d.addCallback(start_lbry_files) d.addCallback(start_lbry_files)
return d return d
def start_lbry_file(self, rowid, stream_hash, payment_rate_manager, blob_data_rate=None, upload_allowed=True): def start_lbry_file(self, rowid, stream_hash, payment_rate_manager, blob_data_rate=None, upload_allowed=True,
download_directory=None):
if not download_directory:
download_directory = self.download_directory
payment_rate_manager.min_blob_data_payment_rate = blob_data_rate payment_rate_manager.min_blob_data_payment_rate = blob_data_rate
lbry_file_downloader = ManagedLBRYFileDownloader(rowid, stream_hash, lbry_file_downloader = ManagedLBRYFileDownloader(rowid, stream_hash,
self.session.peer_finder, self.session.peer_finder,
@ -130,17 +133,17 @@ class LBRYFileManager(object):
self.session.blob_manager, self.session.blob_manager,
self.stream_info_manager, self, self.stream_info_manager, self,
payment_rate_manager, self.session.wallet, payment_rate_manager, self.session.wallet,
self.download_directory, download_directory,
upload_allowed) upload_allowed)
self.lbry_files.append(lbry_file_downloader) self.lbry_files.append(lbry_file_downloader)
d = lbry_file_downloader.set_stream_info() d = lbry_file_downloader.set_stream_info()
d.addCallback(lambda _: lbry_file_downloader) d.addCallback(lambda _: lbry_file_downloader)
return d return d
def add_lbry_file(self, stream_hash, payment_rate_manager, blob_data_rate=None, upload_allowed=True): def add_lbry_file(self, stream_hash, payment_rate_manager, blob_data_rate=None, upload_allowed=True, download_directory=None):
d = self._save_lbry_file(stream_hash, blob_data_rate) d = self._save_lbry_file(stream_hash, blob_data_rate)
d.addCallback(lambda rowid: self.start_lbry_file(rowid, stream_hash, payment_rate_manager, d.addCallback(lambda rowid: self.start_lbry_file(rowid, stream_hash, payment_rate_manager,
blob_data_rate, upload_allowed)) blob_data_rate, upload_allowed, download_directory))
return d return d
def delete_lbry_file(self, lbry_file): def delete_lbry_file(self, lbry_file):

View file

@ -5,7 +5,12 @@ import simplejson as json
import binascii import binascii
import subprocess import subprocess
import logging import logging
import logging.handlers
import requests import requests
import base64
import base58
import platform
import json
from twisted.web import server, resource, static from twisted.web import server, resource, static
from twisted.internet import defer, threads, error, reactor from twisted.internet import defer, threads, error, reactor
@ -15,11 +20,10 @@ from txjsonrpc.web.jsonrpc import Handler
from datetime import datetime from datetime import datetime
from decimal import Decimal from decimal import Decimal
from StringIO import StringIO
from zipfile import ZipFile
from urllib import urlopen
from appdirs import user_data_dir from appdirs import user_data_dir
from urllib2 import urlopen
from lbrynet import __version__
from lbrynet.core.PaymentRateManager import PaymentRateManager from lbrynet.core.PaymentRateManager import PaymentRateManager
from lbrynet.core.server.BlobAvailabilityHandler import BlobAvailabilityHandlerFactory from lbrynet.core.server.BlobAvailabilityHandler import BlobAvailabilityHandlerFactory
from lbrynet.core.server.BlobRequestHandler import BlobRequestHandlerFactory from lbrynet.core.server.BlobRequestHandler import BlobRequestHandlerFactory
@ -42,8 +46,23 @@ from lbrynet.core.LBRYcrdWallet import LBRYcrdWallet, LBRYumWallet
from lbrynet.lbryfilemanager.LBRYFileManager import LBRYFileManager from lbrynet.lbryfilemanager.LBRYFileManager import LBRYFileManager
from lbrynet.lbryfile.LBRYFileMetadataManager import DBLBRYFileMetadataManager, TempLBRYFileMetadataManager from lbrynet.lbryfile.LBRYFileMetadataManager import DBLBRYFileMetadataManager, TempLBRYFileMetadataManager
if sys.platform != "darwin":
log_dir = os.path.join(os.path.expanduser("~"), ".lbrynet")
else:
log_dir = user_data_dir("LBRY")
if not os.path.isdir(log_dir):
os.mkdir(log_dir)
LOG_FILENAME = os.path.join(log_dir, 'lbrynet-daemon.log')
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
handler = logging.handlers.RotatingFileHandler(LOG_FILENAME, maxBytes=262144, backupCount=5)
log.addHandler(handler)
BAD_REQUEST = 400 BAD_REQUEST = 400
NOT_FOUND = 404 NOT_FOUND = 404
OK_CODE = 200 OK_CODE = 200
@ -81,7 +100,7 @@ class LBRYDaemon(jsonrpc.JSONRPC):
# versions... # versions...
if not self.announced_startup: if not self.announced_startup:
if functionPath != 'is_running': if functionPath not in ['is_running', 'check_first_run', 'get_time_behind_blockchain', 'stop']:
return server.failure return server.failure
try: try:
@ -129,6 +148,7 @@ class LBRYDaemon(jsonrpc.JSONRPC):
def _set_vars(wallet_type, check_for_updates): def _set_vars(wallet_type, check_for_updates):
reactor.addSystemEventTrigger('before', 'shutdown', self._shutdown) reactor.addSystemEventTrigger('before', 'shutdown', self._shutdown)
self.log_file = LOG_FILENAME
self.fetcher = None self.fetcher = None
self.current_db_revision = 1 self.current_db_revision = 1
self.run_server = True self.run_server = True
@ -142,7 +162,7 @@ class LBRYDaemon(jsonrpc.JSONRPC):
self.blobfile_dir = os.path.join(self.db_dir, "blobfiles") self.blobfile_dir = os.path.join(self.db_dir, "blobfiles")
self.peer_port = 3333 self.peer_port = 3333
self.dht_node_port = 4444 self.dht_node_port = 4444
self.first_run = False self.first_run = "Loading"
if os.name == "nt": if os.name == "nt":
from lbrynet.winhelpers.knownpaths import get_path, FOLDERID, UserHandle from lbrynet.winhelpers.knownpaths import get_path, FOLDERID, UserHandle
self.download_directory = get_path(FOLDERID.Downloads, UserHandle.current) self.download_directory = get_path(FOLDERID.Downloads, UserHandle.current)
@ -154,12 +174,12 @@ class LBRYDaemon(jsonrpc.JSONRPC):
else: else:
self.wallet_dir = user_data_dir("LBRY") self.wallet_dir = user_data_dir("LBRY")
else: else:
self.download_directory = os.getcwd()
if wallet_type == "lbrycrd": if wallet_type == "lbrycrd":
self.wallet_dir = os.path.join(os.path.expanduser("~"), ".lbrycrd") self.wallet_dir = os.path.join(os.path.expanduser("~"), ".lbrycrd")
else: else:
self.wallet_dir = os.path.join(os.path.expanduser("~"), ".lbryum") self.wallet_dir = os.path.join(os.path.expanduser("~"), ".lbryum")
self.download_directory = os.getcwd() self.daemon_conf = os.path.join(self.db_dir, 'daemon_settings.json')
self.daemon_conf = os.path.join(self.wallet_dir, 'daemon_settings.conf')
self.wallet_conf = os.path.join(self.wallet_dir, "lbrycrd.conf") self.wallet_conf = os.path.join(self.wallet_dir, "lbrycrd.conf")
self.wallet_user = None self.wallet_user = None
self.wallet_password = None self.wallet_password = None
@ -197,21 +217,43 @@ class LBRYDaemon(jsonrpc.JSONRPC):
self.search_timeout = 3.0 self.search_timeout = 3.0
self.query_handlers = {} self.query_handlers = {}
self.default_settings = { self.default_settings = {
'run_on_startup': False, 'run_on_startup': False,
'data_rate': MIN_BLOB_DATA_PAYMENT_RATE, 'data_rate': MIN_BLOB_DATA_PAYMENT_RATE,
'max_key_fee': 10.0, 'max_key_fee': 10.0,
'default_download_directory': self.download_directory, 'default_download_directory': self.download_directory,
'max_upload': 0.0, 'max_upload': 0.0,
'max_download': 0.0 'max_download': 0.0,
'upload_log': True
} }
return defer.succeed(None) return defer.succeed(None)
def _log_starting_vals():
def _get_lbry_files_json():
r = []
for f in self.lbry_file_manager.lbry_files:
if f.key:
t = {'completed': f.completed, 'file_name': f.file_name, 'key': binascii.b2a_hex(f.key),
'points_paid': f.points_paid, 'stopped': f.stopped, 'stream_hash': f.stream_hash,
'stream_name': f.stream_name, 'suggested_file_name': f.suggested_file_name,
'upload_allowed': f.upload_allowed}
else:
t = {'completed': f.completed, 'file_name': f.file_name, 'key': None,
'points_paid': f.points_paid,
'stopped': f.stopped, 'stream_hash': f.stream_hash, 'stream_name': f.stream_name,
'suggested_file_name': f.suggested_file_name, 'upload_allowed': f.upload_allowed}
r.append(t)
return json.dumps(r)
log.info("LBRY Files: " + _get_lbry_files_json())
log.info("Starting balance: " + str(self.session.wallet.wallet_balance))
return defer.succeed(None)
def _announce_startup(): def _announce_startup():
self.announced_startup = True self.announced_startup = True
return defer.succeed(None)
def _disp_startup():
log.info("[" + str(datetime.now()) + "] Started lbrynet-daemon") log.info("[" + str(datetime.now()) + "] Started lbrynet-daemon")
return defer.succeed(None) return defer.succeed(None)
@ -219,7 +261,8 @@ class LBRYDaemon(jsonrpc.JSONRPC):
d = defer.Deferred() d = defer.Deferred()
d.addCallback(lambda _:_set_vars(wallet_type, check_for_updates)) d.addCallback(lambda _:_set_vars(wallet_type, check_for_updates))
d.addCallback(lambda _: self._setup_daemon_settings()) d.addCallback(lambda _: self._initial_setup())
d.addCallback(self._set_daemon_settings)
d.addCallback(lambda _: threads.deferToThread(self._setup_data_directory)) d.addCallback(lambda _: threads.deferToThread(self._setup_data_directory))
d.addCallback(lambda _: self._check_db_migration()) d.addCallback(lambda _: self._check_db_migration())
d.addCallback(lambda _: self._get_settings()) d.addCallback(lambda _: self._get_settings())
@ -232,17 +275,45 @@ class LBRYDaemon(jsonrpc.JSONRPC):
d.addCallback(lambda _: self._setup_query_handlers()) d.addCallback(lambda _: self._setup_query_handlers())
d.addCallback(lambda _: self._setup_server()) d.addCallback(lambda _: self._setup_server())
d.addCallback(lambda _: self._setup_fetcher()) d.addCallback(lambda _: self._setup_fetcher())
d.addCallback(lambda _: _log_starting_vals())
d.addCallback(lambda _: _announce_startup()) d.addCallback(lambda _: _announce_startup())
d.addCallback(lambda _: _disp_startup())
d.callback(None) d.callback(None)
return defer.succeed(None) return defer.succeed(None)
def _initial_setup(self): def _initial_setup(self):
return defer.fail(NotImplementedError()) def _log_platform():
msg = {
"processor": platform.processor(),
"python version: ": platform.python_version(),
"lbrynet version: ": __version__,
# 'ip': json.load(urlopen('http://jsonip.com'))['ip'],
}
if sys.platform == "darwin":
msg['osx version'] = platform.mac_ver()[0] + " " + platform.mac_ver()[2]
else:
msg['platform'] = platform.platform()
def _setup_daemon_settings(self): log.info("Platform: " + json.dumps(msg))
self.session_settings = self.default_settings return defer.succeed(None)
def _load_daemon_conf():
if os.path.isfile(self.daemon_conf):
return json.loads(open(self.daemon_conf, "r").read())
else:
log.info("Writing default settings : " + json.dumps(self.default_settings) + " --> " + str(self.daemon_conf))
f = open(self.daemon_conf, "w")
f.write(json.dumps(self.default_settings))
f.close()
return self.default_settings
d = _log_platform()
d.addCallback(lambda _: _load_daemon_conf())
return d
def _set_daemon_settings(self, settings):
self.session_settings = settings
return defer.succeed(None) return defer.succeed(None)
def _start_server(self): def _start_server(self):
@ -317,33 +388,72 @@ class LBRYDaemon(jsonrpc.JSONRPC):
dl.addCallback(_set_query_handlers) dl.addCallback(_set_query_handlers)
return dl return dl
def _upload_log(self):
if self.session_settings['upload_log']:
LOG_URL = "https://lbry.io/log-upload"
f = open(self.log_file, "r")
t = datetime.now()
log_name = base58.b58encode(self.lbryid)[:20] + "-" + str(t.month) + "-" + str(t.day) + "-" + str(t.year) + "-" + str(t.hour) + "-" + str(t.minute)
params = {'name': log_name, 'log': f.read()}
f.close()
requests.post(LOG_URL, params)
return defer.succeed(None)
def _shutdown(self): def _shutdown(self):
log.info("Closing lbrynet session") log.info("Closing lbrynet session")
d = self._stop_server() d = self._upload_log()
d.addCallback(lambda _: self._stop_server())
d.addErrback(lambda err: log.info("Bad server shutdown: " + err.getTraceback()))
if self.session is not None: if self.session is not None:
d.addCallback(lambda _: self.session.shut_down()) d.addCallback(lambda _: self.session.shut_down())
d.addErrback(lambda err: log.info("Bad session shutdown: " + err.getTraceback()))
return d return d
def _update_settings(self, settings): def _update_settings(self, settings):
if not isinstance(settings['run_on_startup'], bool): for k in settings.keys():
return defer.fail() if k == 'run_on_startup':
elif not isinstance(settings['data_rate'], float): if type(settings['run_on_startup']) is bool:
return defer.fail() self.session_settings['run_on_startup'] = settings['run_on_startup']
elif not isinstance(settings['max_key_fee'], float): else:
return defer.fail() return defer.fail()
elif not isinstance(settings['default_download_directory'], unicode): elif k == 'data_rate':
return defer.fail() if type(settings['data_rate']) is float:
elif not isinstance(settings['max_upload'], float): self.session_settings['data_rate'] = settings['data_rate']
return defer.fail() else:
elif not isinstance(settings['max_download'], float): return defer.fail()
return defer.fail() elif k == 'max_key_fee':
if type(settings['max_key_fee']) is float:
self.session_settings['max_key_fee'] = settings['max_key_fee']
else:
return defer.fail()
elif k == 'default_download_directory':
if type(settings['default_download_directory']) is unicode:
if os.path.isdir(settings['default_download_directory']):
self.session_settings['default_download_directory'] = settings['default_download_directory']
else:
pass
else:
return defer.fail()
elif k == 'max_upload':
if type(settings['max_upload']) is float:
self.session_settings['max_upload'] = settings['max_upload']
else:
return defer.fail()
elif k == 'max_download':
if type(settings['max_download']) is float:
self.session_settings['max_download'] = settings['max_download']
else:
return defer.fail()
elif k == 'upload_log':
if type(settings['upload_log']) is bool:
self.session_settings['upload_log'] = settings['upload_log']
else:
return defer.fail()
self.session_settings['run_on_startup'] = settings['run_on_startup'] f = open(self.daemon_conf, "w")
self.session_settings['data_rate'] = settings['data_rate'] f.write(json.dumps(self.session_settings))
self.session_settings['max_key_fee'] = settings['max_key_fee'] f.close()
self.session_settings['default_download_directory'] = settings['default_download_directory']
self.session_settings['max_upload'] = settings['max_upload']
self.session_settings['max_download'] = settings['max_download']
return defer.succeed(True) return defer.succeed(True)
@ -398,10 +508,12 @@ class LBRYDaemon(jsonrpc.JSONRPC):
if lbryid is None: if lbryid is None:
return self._make_lbryid() return self._make_lbryid()
else: else:
log.info("LBRY ID: " + base58.b58encode(lbryid))
self.lbryid = lbryid self.lbryid = lbryid
def _make_lbryid(self): def _make_lbryid(self):
self.lbryid = generate_id() self.lbryid = generate_id()
log.info("Generated new LBRY ID: " + base58.b58encode(self.lbryid))
d = self.settings.save_lbryid(self.lbryid) d = self.settings.save_lbryid(self.lbryid)
return d return d
@ -474,11 +586,17 @@ class LBRYDaemon(jsonrpc.JSONRPC):
return dl return dl
def _check_first_run(self): def _check_first_run(self):
def _set_first_run_false():
self.first_run = False
return 0.0
d = self.session.wallet.is_first_run() d = self.session.wallet.is_first_run()
d.addCallback(lambda is_first_run: self._do_first_run() if is_first_run else 0.0)
d.addCallback(lambda is_first_run: self._do_first_run() if is_first_run else _set_first_run_false())
return d return d
def _do_first_run(self): def _do_first_run(self):
self.first_run = True
d = self.session.wallet.get_new_address() d = self.session.wallet.get_new_address()
def send_request(url, data): def send_request(url, data):
@ -530,7 +648,7 @@ class LBRYDaemon(jsonrpc.JSONRPC):
def _setup_stream_identifier(self): def _setup_stream_identifier(self):
file_saver_factory = LBRYFileSaverFactory(self.session.peer_finder, self.session.rate_limiter, file_saver_factory = LBRYFileSaverFactory(self.session.peer_finder, self.session.rate_limiter,
self.session.blob_manager, self.stream_info_manager, self.session.blob_manager, self.stream_info_manager,
self.session.wallet, self.download_directory) self.session.wallet, self.session_settings['default_download_directory'])
self.sd_identifier.add_stream_downloader_factory(LBRYFileStreamType, file_saver_factory) self.sd_identifier.add_stream_downloader_factory(LBRYFileStreamType, file_saver_factory)
file_opener_factory = LBRYFileOpenerFactory(self.session.peer_finder, self.session.rate_limiter, file_opener_factory = LBRYFileOpenerFactory(self.session.peer_finder, self.session.rate_limiter,
self.session.blob_manager, self.stream_info_manager, self.session.blob_manager, self.stream_info_manager,
@ -548,7 +666,7 @@ class LBRYDaemon(jsonrpc.JSONRPC):
def _download_name(self, name, timeout=DEFAULT_TIMEOUT): def _download_name(self, name, timeout=DEFAULT_TIMEOUT):
def _disp_file(f): def _disp_file(f):
file_path = os.path.join(self.download_directory, f.file_name) file_path = os.path.join(self.session_settings['default_download_directory'], f.file_name)
log.info("[" + str(datetime.now()) + "] Already downloaded: " + str(f.stream_hash) + " --> " + file_path) log.info("[" + str(datetime.now()) + "] Already downloaded: " + str(f.stream_hash) + " --> " + file_path)
return defer.succeed(f) return defer.succeed(f)
@ -563,7 +681,8 @@ class LBRYDaemon(jsonrpc.JSONRPC):
d = self.session.wallet.get_stream_info_for_name(name) d = self.session.wallet.get_stream_info_for_name(name)
stream = GetStream(self.sd_identifier, self.session, self.session.wallet, self.lbry_file_manager, stream = GetStream(self.sd_identifier, self.session, self.session.wallet, self.lbry_file_manager,
max_key_fee=self.max_key_fee, data_rate=self.data_rate, timeout=timeout) max_key_fee=self.max_key_fee, data_rate=self.data_rate, timeout=timeout,
download_directory=self.session_settings['default_download_directory'])
d.addCallback(_disp) d.addCallback(_disp)
d.addCallback(lambda stream_info: stream.start(stream_info)) d.addCallback(lambda stream_info: stream.start(stream_info))
d.addCallback(lambda _: self._path_from_name(name)) d.addCallback(lambda _: self._path_from_name(name))
@ -603,7 +722,7 @@ class LBRYDaemon(jsonrpc.JSONRPC):
for lbry_file in self.lbry_file_manager.lbry_files: for lbry_file in self.lbry_file_manager.lbry_files:
if lbry_file.stream_name == file_name: if lbry_file.stream_name == file_name:
if sys.platform == "darwin": if sys.platform == "darwin":
if os.path.isfile(os.path.join(self.download_directory, lbry_file.stream_name)): if os.path.isfile(os.path.join(self.session_settings['default_download_directory'], lbry_file.stream_name)):
return lbry_file return lbry_file
else: else:
return False return False
@ -644,8 +763,8 @@ class LBRYDaemon(jsonrpc.JSONRPC):
d = self.lbry_file_manager.get_count_for_stream_hash(s_h) d = self.lbry_file_manager.get_count_for_stream_hash(s_h)
# TODO: could possibly be a timing issue here # TODO: could possibly be a timing issue here
d.addCallback(lambda c: self.stream_info_manager.delete_stream(s_h) if c == 0 else True) d.addCallback(lambda c: self.stream_info_manager.delete_stream(s_h) if c == 0 else True)
d.addCallback(lambda _: os.remove(os.path.join(self.download_directory, lbry_file.file_name)) if d.addCallback(lambda _: os.remove(os.path.join(self.session_settings['default_download_directory'], lbry_file.file_name)) if
os.path.isfile(os.path.join(self.download_directory, lbry_file.file_name)) else defer.succeed(None)) os.path.isfile(os.path.join(self.session_settings['default_download_directory'], lbry_file.file_name)) else defer.succeed(None))
return d return d
d.addCallback(lambda _: finish_deletion(lbry_file)) d.addCallback(lambda _: finish_deletion(lbry_file))
@ -654,14 +773,14 @@ class LBRYDaemon(jsonrpc.JSONRPC):
def _path_from_name(self, name): def _path_from_name(self, name):
d = self._check_history(name) d = self._check_history(name)
d.addCallback(lambda lbry_file: {'stream_hash': lbry_file.stream_hash, d.addCallback(lambda lbry_file: {'stream_hash': lbry_file.stream_hash,
'path': os.path.join(self.download_directory, lbry_file.file_name)} 'path': os.path.join(self.session_settings['default_download_directory'], lbry_file.file_name)}
if lbry_file else defer.fail(UnknownNameError)) if lbry_file else defer.fail(UnknownNameError))
return d return d
def _path_from_lbry_file(self, lbry_file): def _path_from_lbry_file(self, lbry_file):
if lbry_file: if lbry_file:
r = {'stream_hash': lbry_file.stream_hash, r = {'stream_hash': lbry_file.stream_hash,
'path': os.path.join(self.download_directory, lbry_file.file_name)} 'path': os.path.join(self.session_settings['default_download_directory'], lbry_file.file_name)}
return defer.succeed(r) return defer.succeed(r)
else: else:
return defer.fail(UnknownNameError) return defer.fail(UnknownNameError)
@ -697,11 +816,6 @@ class LBRYDaemon(jsonrpc.JSONRPC):
def _render_response(self, result, code): def _render_response(self, result, code):
return defer.succeed({'result': result, 'code': code}) return defer.succeed({'result': result, 'code': code})
# def _log_to_slack(self, msg):
# URL = "https://hooks.slack.com/services/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"
# h = httplib2.Http()
# h.request(URL, 'POST', json.dumps({"text": msg}), headers={'Content-Type': 'application/json'})
def jsonrpc_is_running(self): def jsonrpc_is_running(self):
""" """
Returns true if daemon completed startup, otherwise returns false Returns true if daemon completed startup, otherwise returns false
@ -712,6 +826,16 @@ class LBRYDaemon(jsonrpc.JSONRPC):
else: else:
return self._render_response(False, OK_CODE) return self._render_response(False, OK_CODE)
def jsonrpc_check_first_run(self):
try:
d = self.session.wallet.is_first_run()
except:
d = defer.fail(None)
d.addCallbacks(lambda r: self._render_response(r, OK_CODE), lambda _: self._render_response("Loading", OK_CODE))
return d
def jsonrpc_get_settings(self): def jsonrpc_get_settings(self):
""" """
Get LBRY payment settings Get LBRY payment settings
@ -733,10 +857,14 @@ class LBRYDaemon(jsonrpc.JSONRPC):
'max_download': float (0.0 for unlimited)} 'max_download': float (0.0 for unlimited)}
""" """
d = self._update_settings(p) def _log_settings_change(params):
log.info("[" + str(datetime.now()) + "] Set daemon settings to " + str(params))
log.info("[" + str(datetime.now()) + "] Set daemon settings") d = self._update_settings(p)
return self._render_response(True, OK_CODE) d.addCallback(lambda _: _log_settings_change(p))
d.addCallback(lambda _: self._render_response(self.session_settings, OK_CODE))
return d
def jsonrpc_start_fetcher(self): def jsonrpc_start_fetcher(self):
""" """
@ -961,18 +1089,16 @@ class LBRYDaemon(jsonrpc.JSONRPC):
@return: confirmation message @return: confirmation message
""" """
params = Bunch(p)
def _disp(file_name): def _disp(file_name):
log.info("[" + str(datetime.now()) + "] Deleted: " + file_name) log.info("[" + str(datetime.now()) + "] Deleted: " + file_name)
return self._render_response("Deleted: " + file_name, OK_CODE) return self._render_response("Deleted: " + file_name, OK_CODE)
lbry_files = [self._delete_lbry_file(f) for f in self.lbry_file_manager.lbry_files if "file_name" in p.keys():
if str(params.file_name) == str(f.file_name)] lbry_files = [self._delete_lbry_file(f) for f in self.lbry_file_manager.lbry_files
d = defer.DeferredList(lbry_files) if p['file_name'] == f.file_name]
d.addCallback(lambda _: _disp(params.file_name)) d = defer.DeferredList(lbry_files)
d.addCallback(lambda _: _disp(p['file_name']))
return d return d
def jsonrpc_publish(self, p): def jsonrpc_publish(self, p):
""" """
@ -1018,14 +1144,13 @@ class LBRYDaemon(jsonrpc.JSONRPC):
""" """
params = Bunch(p) params = Bunch(p)
def _disp(txid, tx): def _disp(x):
log.info("[" + str(datetime.now()) + "] Abandoned name claim tx " + txid) log.info("[" + str(datetime.now()) + "] Abandoned name claim tx " + str(x))
return self._render_response(txid, OK_CODE) return self._render_response(x, OK_CODE)
d = defer.Deferred() d = defer.Deferred()
d.addCallback(lambda _: self.session.wallet.abandon_name(params.txid)) d.addCallback(lambda _: self.session.wallet.abandon_name(params.txid))
d.addCallback(lambda tx: _disp(params.txid, tx)) d.addCallback(_disp)
d.addErrback(lambda err: self._render_response(err.getTraceback(), BAD_REQUEST))
d.callback(None) d.callback(None)
return d return d
@ -1055,10 +1180,17 @@ class LBRYDaemon(jsonrpc.JSONRPC):
@return: time behind blockchain @return: time behind blockchain
""" """
d = self.session.wallet.get_most_recent_blocktime()
d.addCallback(get_time_behind_blockchain) def _get_time_behind():
d.addCallbacks(lambda result: self._render_response(str(result), OK_CODE), try:
lambda result: self._render_response(result, BAD_REQUEST)) local_height = self.session.wallet.network.get_local_height()
remote_height = self.session.wallet.network.get_server_height()
return defer.succeed(remote_height - local_height)
except:
return defer.fail()
d = _get_time_behind()
d.addCallback(lambda r: self._render_response(r, OK_CODE))
return d return d

View file

@ -1,5 +1,6 @@
import argparse import argparse
import logging import logging
import logging.handlers
import subprocess import subprocess
import os import os
import shutil import shutil
@ -18,6 +19,13 @@ from jsonrpc.proxy import JSONRPCProxy
from lbrynet.lbrynet_daemon.LBRYDaemon import LBRYDaemon, LBRYindex, LBRYFileRender from lbrynet.lbrynet_daemon.LBRYDaemon import LBRYDaemon, LBRYindex, LBRYFileRender
from lbrynet.conf import API_CONNECTION_STRING, API_INTERFACE, API_ADDRESS, API_PORT, DEFAULT_WALLET, UI_ADDRESS from lbrynet.conf import API_CONNECTION_STRING, API_INTERFACE, API_ADDRESS, API_PORT, DEFAULT_WALLET, UI_ADDRESS
if sys.platform != "darwin":
log_dir = os.path.join(os.path.expanduser("~"), ".lbrynet")
else:
log_dir = user_data_dir("LBRY")
LOG_FILENAME = os.path.join(log_dir, 'lbrynet-daemon.log')
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
logging.basicConfig(level=logging.INFO) logging.basicConfig(level=logging.INFO)

View file

@ -17,7 +17,7 @@ log = logging.getLogger(__name__)
class GetStream(object): class GetStream(object):
def __init__(self, sd_identifier, session, wallet, lbry_file_manager, max_key_fee, pay_key=True, data_rate=0.5, def __init__(self, sd_identifier, session, wallet, lbry_file_manager, max_key_fee, pay_key=True, data_rate=0.5,
timeout=DEFAULT_TIMEOUT): timeout=DEFAULT_TIMEOUT, download_directory=None):
self.wallet = wallet self.wallet = wallet
self.resolved_name = None self.resolved_name = None
self.description = None self.description = None
@ -37,6 +37,7 @@ class GetStream(object):
self.d = defer.Deferred(None) self.d = defer.Deferred(None)
self.timeout = timeout self.timeout = timeout
self.timeout_counter = 0 self.timeout_counter = 0
self.download_directory = download_directory
self.download_path = None self.download_path = None
self.checker = LoopingCall(self.check_status) self.checker = LoopingCall(self.check_status)
@ -88,7 +89,10 @@ class GetStream(object):
self.d.addCallback(lambda _: download_sd_blob(self.session, self.stream_hash, self.payment_rate_manager)) 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(self.sd_identifier.get_metadata_for_sd_blob)
self.d.addCallback(lambda metadata: (next(factory for factory in metadata.factories if isinstance(factory, ManagedLBRYFileDownloaderFactory)), metadata)) self.d.addCallback(lambda metadata: (next(factory for factory in metadata.factories if isinstance(factory, ManagedLBRYFileDownloaderFactory)), metadata))
self.d.addCallback(lambda (factory, metadata): factory.make_downloader(metadata, [self.data_rate, True], self.payment_rate_manager)) self.d.addCallback(lambda (factory, metadata): factory.make_downloader(metadata,
[self.data_rate, True],
self.payment_rate_manager,
download_directory=self.download_directory))
self.d.addErrback(lambda err: err.trap(defer.CancelledError)) self.d.addErrback(lambda err: err.trap(defer.CancelledError))
self.d.addErrback(lambda err: log.error("An exception occurred attempting to load the stream descriptor: %s", err.getTraceback())) self.d.addErrback(lambda err: log.error("An exception occurred attempting to load the stream descriptor: %s", err.getTraceback()))
self.d.addCallback(self._start_download) self.d.addCallback(self._start_download)

View file

@ -103,10 +103,6 @@ class Publisher(object):
def set_sd_hash(sd_hash): def set_sd_hash(sd_hash):
self.sd_hash = sd_hash self.sd_hash = sd_hash
if isinstance(self.sources, dict):
self.sources['lbry_sd_hash'] = sd_hash
else:
self.sources = {'lbry_sd_hash': sd_hash}
d.addCallback(set_sd_hash) d.addCallback(set_sd_hash)
return d return d

View file

@ -19,7 +19,7 @@ qrcode==5.2.2
requests==2.9.1 requests==2.9.1
seccure==0.3.1.3 seccure==0.3.1.3
simplejson==3.8.2 simplejson==3.8.2
six==1.10.0 six==1.9.0
slowaes==0.1a1 slowaes==0.1a1
txJSON-RPC==0.3.1 txJSON-RPC==0.3.1
unqlite==0.2.0 unqlite==0.2.0

View file

@ -26,12 +26,7 @@ console_scripts = ['lbrynet-console = lbrynet.lbrynet_console.LBRYConsole:launch
requires = ['pycrypto', 'twisted', 'miniupnpc', 'yapsy', 'seccure', requires = ['pycrypto', 'twisted', 'miniupnpc', 'yapsy', 'seccure',
'python-bitcoinrpc==0.1', 'txJSON-RPC', 'requests>=2.4.2', 'unqlite==0.2.0', 'python-bitcoinrpc==0.1', 'txJSON-RPC', 'requests>=2.4.2', 'unqlite==0.2.0',
'leveldb', 'lbryum', 'jsonrpc', 'simplejson', 'appdirs'] 'leveldb', 'lbryum', 'jsonrpc', 'simplejson', 'appdirs', 'six==1.9.0']
if sys.platform == 'darwin':
requires.append('six==1.9.0')
else:
requires.append('six>=1.9.0')
gui_data_files = ['close2.gif', 'lbry-dark-242x80.gif', 'lbry-dark-icon.xbm', 'lbry-dark-icon.ico', gui_data_files = ['close2.gif', 'lbry-dark-242x80.gif', 'lbry-dark-icon.xbm', 'lbry-dark-icon.ico',
'drop_down.gif', 'show_options.gif', 'hide_options.gif', 'lbry.conf'] 'drop_down.gif', 'show_options.gif', 'hide_options.gif', 'lbry.conf']