2015-12-14 21:54:33 +01:00
|
|
|
import json
|
|
|
|
import logging
|
|
|
|
import os
|
2016-04-24 23:51:24 +02:00
|
|
|
import sys
|
2016-03-17 15:19:36 +01:00
|
|
|
|
2016-04-24 23:51:24 +02:00
|
|
|
from appdirs import user_data_dir
|
2015-12-14 21:54:33 +01:00
|
|
|
from datetime import datetime
|
|
|
|
from twisted.internet import defer
|
|
|
|
from twisted.internet.task import LoopingCall
|
2016-03-17 15:19:36 +01:00
|
|
|
|
2015-12-14 21:54:33 +01:00
|
|
|
from lbrynet.core.Error import InvalidStreamInfoError, InsufficientFundsError
|
|
|
|
from lbrynet.core.PaymentRateManager import PaymentRateManager
|
|
|
|
from lbrynet.core.StreamDescriptor import download_sd_blob
|
2016-03-08 18:15:49 +01:00
|
|
|
from lbrynet.lbryfilemanager.LBRYFileDownloader import ManagedLBRYFileDownloaderFactory
|
2016-03-17 15:19:36 +01:00
|
|
|
from lbrynet.conf import DEFAULT_TIMEOUT
|
2015-12-14 21:54:33 +01:00
|
|
|
|
2016-04-24 23:51:24 +02:00
|
|
|
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')
|
2015-12-14 21:54:33 +01:00
|
|
|
log = logging.getLogger(__name__)
|
2016-04-24 23:51:24 +02:00
|
|
|
handler = logging.handlers.RotatingFileHandler(LOG_FILENAME, maxBytes=2097152, backupCount=5)
|
|
|
|
log.addHandler(handler)
|
2016-04-23 00:18:17 +02:00
|
|
|
log.setLevel(logging.INFO)
|
2015-12-14 21:54:33 +01:00
|
|
|
|
|
|
|
|
|
|
|
class GetStream(object):
|
2016-03-17 05:44:04 +01:00
|
|
|
def __init__(self, sd_identifier, session, wallet, lbry_file_manager, max_key_fee, pay_key=True, data_rate=0.5,
|
2016-04-07 09:12:09 +02:00
|
|
|
timeout=DEFAULT_TIMEOUT, download_directory=None):
|
2015-12-14 21:54:33 +01:00
|
|
|
self.wallet = wallet
|
|
|
|
self.resolved_name = None
|
|
|
|
self.description = None
|
|
|
|
self.key_fee = None
|
|
|
|
self.key_fee_address = None
|
2016-01-20 03:07:16 +01:00
|
|
|
self.data_rate = data_rate
|
|
|
|
self.pay_key = pay_key
|
2015-12-14 21:54:33 +01:00
|
|
|
self.name = None
|
|
|
|
self.session = session
|
|
|
|
self.payment_rate_manager = PaymentRateManager(self.session.base_payment_rate_manager)
|
|
|
|
self.lbry_file_manager = lbry_file_manager
|
|
|
|
self.sd_identifier = sd_identifier
|
|
|
|
self.stream_hash = None
|
|
|
|
self.max_key_fee = max_key_fee
|
|
|
|
self.stream_info = None
|
|
|
|
self.stream_info_manager = None
|
2016-03-17 05:44:04 +01:00
|
|
|
self.d = defer.Deferred(None)
|
|
|
|
self.timeout = timeout
|
|
|
|
self.timeout_counter = 0
|
2016-04-07 09:12:09 +02:00
|
|
|
self.download_directory = download_directory
|
2016-03-17 05:44:04 +01:00
|
|
|
self.download_path = None
|
2016-04-24 10:42:42 +02:00
|
|
|
self.downloader = None
|
2016-04-23 00:18:17 +02:00
|
|
|
self.finished = defer.Deferred()
|
2016-03-17 05:44:04 +01:00
|
|
|
self.checker = LoopingCall(self.check_status)
|
2016-01-20 03:07:16 +01:00
|
|
|
|
2016-03-17 05:44:04 +01:00
|
|
|
def check_status(self):
|
|
|
|
self.timeout_counter += 1
|
|
|
|
|
2016-04-23 00:18:17 +02:00
|
|
|
if self.download_path:
|
2016-03-17 05:44:04 +01:00
|
|
|
self.checker.stop()
|
2016-04-23 00:18:17 +02:00
|
|
|
self.finished.callback((self.stream_hash, self.download_path))
|
2016-03-17 05:44:04 +01:00
|
|
|
|
|
|
|
elif self.timeout_counter >= self.timeout:
|
2016-04-23 00:18:17 +02:00
|
|
|
log.info("Timeout downloading lbry://" + self.resolved_name + ", " + str(self.stream_info))
|
2016-03-17 05:44:04 +01:00
|
|
|
self.checker.stop()
|
|
|
|
self.d.cancel()
|
2016-04-23 00:18:17 +02:00
|
|
|
self.finished.callback(False)
|
2016-03-17 05:44:04 +01:00
|
|
|
|
2016-04-23 00:18:17 +02:00
|
|
|
def start(self, stream_info, name):
|
|
|
|
self.resolved_name = name
|
2015-12-14 21:54:33 +01:00
|
|
|
self.stream_info = stream_info
|
|
|
|
if 'stream_hash' in self.stream_info.keys():
|
|
|
|
self.description = self.stream_info['description']
|
|
|
|
if 'key_fee' in self.stream_info.keys():
|
|
|
|
self.key_fee = float(self.stream_info['key_fee'])
|
|
|
|
if 'key_fee_address' in self.stream_info.keys():
|
|
|
|
self.key_fee_address = self.stream_info['key_fee_address']
|
|
|
|
else:
|
|
|
|
self.key_fee_address = None
|
|
|
|
else:
|
|
|
|
self.key_fee = None
|
|
|
|
self.key_fee_address = None
|
2016-01-20 03:07:16 +01:00
|
|
|
|
2015-12-14 21:54:33 +01:00
|
|
|
self.stream_hash = self.stream_info['stream_hash']
|
2016-04-02 04:11:11 +02:00
|
|
|
if isinstance(self.stream_hash, dict):
|
|
|
|
self.stream_hash = self.stream_hash['sd_hash']
|
2016-01-20 03:07:16 +01:00
|
|
|
|
2015-12-14 21:54:33 +01:00
|
|
|
else:
|
2016-03-14 17:30:22 +01:00
|
|
|
log.error("InvalidStreamInfoError in autofetcher: ", stream_info)
|
2015-12-14 21:54:33 +01:00
|
|
|
raise InvalidStreamInfoError(self.stream_info)
|
|
|
|
|
|
|
|
if self.key_fee > self.max_key_fee:
|
|
|
|
if self.pay_key:
|
2016-03-14 17:30:22 +01:00
|
|
|
log.info("Key fee (" + str(self.key_fee) + ") above limit of " + str(
|
|
|
|
self.max_key_fee) + ", didn't download lbry://" + str(self.resolved_name))
|
2016-01-20 03:07:16 +01:00
|
|
|
return defer.fail(None)
|
2015-12-14 21:54:33 +01:00
|
|
|
else:
|
|
|
|
pass
|
|
|
|
|
2016-04-23 00:18:17 +02:00
|
|
|
def _cause_timeout():
|
|
|
|
self.timeout_counter = self.timeout * 2
|
|
|
|
|
2016-03-17 05:44:04 +01:00
|
|
|
self.checker.start(1)
|
2016-01-20 03:07:16 +01:00
|
|
|
|
2016-03-17 05:44:04 +01:00
|
|
|
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)
|
2016-04-23 00:18:17 +02:00
|
|
|
self.d.addCallback(lambda metadata: (
|
|
|
|
next(factory for factory in metadata.factories if isinstance(factory, ManagedLBRYFileDownloaderFactory)),
|
|
|
|
metadata))
|
2016-04-07 09:12:09 +02:00
|
|
|
self.d.addCallback(lambda (factory, metadata): factory.make_downloader(metadata,
|
|
|
|
[self.data_rate, True],
|
|
|
|
self.payment_rate_manager,
|
|
|
|
download_directory=self.download_directory))
|
2016-04-23 00:18:17 +02:00
|
|
|
self.d.addCallbacks(self._start_download, lambda _: _cause_timeout())
|
2016-03-17 05:44:04 +01:00
|
|
|
self.d.callback(None)
|
|
|
|
|
2016-04-23 00:18:17 +02:00
|
|
|
return self.finished
|
2016-01-20 03:07:16 +01:00
|
|
|
|
|
|
|
def _start_download(self, downloader):
|
|
|
|
def _pay_key_fee():
|
|
|
|
if self.key_fee is not None and self.key_fee_address is not None:
|
|
|
|
reserved_points = self.wallet.reserve_points(self.key_fee_address, self.key_fee)
|
|
|
|
if reserved_points is None:
|
|
|
|
return defer.fail(InsufficientFundsError())
|
2016-03-14 17:30:22 +01:00
|
|
|
log.info("Key fee: " + str(self.key_fee) + " | " + str(self.key_fee_address))
|
2016-01-20 03:07:16 +01:00
|
|
|
return self.wallet.send_points_to_address(reserved_points, self.key_fee)
|
|
|
|
return defer.succeed(None)
|
2015-12-14 21:54:33 +01:00
|
|
|
|
|
|
|
if self.pay_key:
|
2016-01-20 03:07:16 +01:00
|
|
|
d = _pay_key_fee()
|
2015-12-14 21:54:33 +01:00
|
|
|
else:
|
2016-01-20 03:07:16 +01:00
|
|
|
d = defer.Deferred()
|
2016-04-24 10:42:42 +02:00
|
|
|
self.downloader = downloader
|
2016-03-17 05:44:04 +01:00
|
|
|
self.download_path = os.path.join(downloader.download_directory, downloader.file_name)
|
2016-04-24 23:51:24 +02:00
|
|
|
d.addCallback(lambda _: log.info("[" + str(datetime.now()) + "] Downloading " + str(self.stream_hash) + " --> " + str(self.download_path)))
|
2016-04-24 10:42:42 +02:00
|
|
|
d.addCallback(lambda _: self.downloader.start())
|
|
|
|
|
2015-12-14 21:54:33 +01:00
|
|
|
|
|
|
|
class FetcherDaemon(object):
|
2016-01-27 17:02:57 +01:00
|
|
|
def __init__(self, session, lbry_file_manager, lbry_file_metadata_manager, wallet, sd_identifier, autofetcher_conf,
|
|
|
|
verbose=False):
|
2015-12-14 21:54:33 +01:00
|
|
|
self.autofetcher_conf = autofetcher_conf
|
|
|
|
self.max_key_fee = 0.0
|
|
|
|
self.sd_identifier = sd_identifier
|
|
|
|
self.wallet = wallet
|
|
|
|
self.session = session
|
|
|
|
self.lbry_file_manager = lbry_file_manager
|
|
|
|
self.lbry_metadata_manager = lbry_file_metadata_manager
|
|
|
|
self.seen = []
|
|
|
|
self.lastbestblock = None
|
|
|
|
self.search = None
|
|
|
|
self.first_run = True
|
|
|
|
self.is_running = False
|
2016-01-27 17:02:57 +01:00
|
|
|
self.verbose = verbose
|
2015-12-14 21:54:33 +01:00
|
|
|
self._get_autofetcher_conf()
|
|
|
|
|
|
|
|
def start(self):
|
|
|
|
if not self.is_running:
|
|
|
|
self.is_running = True
|
|
|
|
self.search = LoopingCall(self._looped_search)
|
|
|
|
self.search.start(1)
|
2016-03-14 17:30:22 +01:00
|
|
|
log.info("Starting autofetcher")
|
2015-12-14 21:54:33 +01:00
|
|
|
else:
|
2016-03-14 17:30:22 +01:00
|
|
|
log.info("Autofetcher is already running")
|
2015-12-14 21:54:33 +01:00
|
|
|
|
|
|
|
def stop(self):
|
|
|
|
if self.is_running:
|
|
|
|
self.search.stop()
|
|
|
|
self.is_running = False
|
|
|
|
else:
|
2016-03-14 17:30:22 +01:00
|
|
|
log.info("Autofetcher isn't running, there's nothing to stop")
|
2015-12-14 21:54:33 +01:00
|
|
|
|
|
|
|
def check_if_running(self):
|
|
|
|
if self.is_running:
|
|
|
|
msg = "Autofetcher is running\n"
|
2016-02-19 06:44:08 +01:00
|
|
|
msg += "Last block hash: " + str(self.lastbestblock)
|
2015-12-14 21:54:33 +01:00
|
|
|
else:
|
|
|
|
msg = "Autofetcher is not running"
|
|
|
|
return msg
|
|
|
|
|
|
|
|
def _get_names(self):
|
2016-02-19 06:44:08 +01:00
|
|
|
d = self.wallet.get_best_blockhash()
|
|
|
|
d.addCallback(lambda blockhash: get_new_streams(blockhash) if blockhash != self.lastbestblock else [])
|
2016-02-17 05:10:26 +01:00
|
|
|
|
2016-02-19 06:44:08 +01:00
|
|
|
def get_new_streams(blockhash):
|
|
|
|
self.lastbestblock = blockhash
|
|
|
|
d = self.wallet.get_block(blockhash)
|
|
|
|
d.addCallback(lambda block: get_new_streams_in_txes(block['tx'], blockhash))
|
2016-02-17 05:10:26 +01:00
|
|
|
return d
|
|
|
|
|
2016-02-19 06:44:08 +01:00
|
|
|
def get_new_streams_in_txes(txids, blockhash):
|
2016-02-17 05:10:26 +01:00
|
|
|
ds = []
|
|
|
|
for t in txids:
|
|
|
|
d = self.wallet.get_claims_from_tx(t)
|
2016-02-19 06:44:08 +01:00
|
|
|
d.addCallback(get_new_streams_in_tx, t, blockhash)
|
2016-02-17 05:10:26 +01:00
|
|
|
ds.append(d)
|
|
|
|
d = defer.DeferredList(ds, consumeErrors=True)
|
|
|
|
d.addCallback(lambda result: [r[1] for r in result if r[0]])
|
|
|
|
d.addCallback(lambda stream_lists: [stream for streams in stream_lists for stream in streams])
|
|
|
|
return d
|
|
|
|
|
2016-02-19 06:44:08 +01:00
|
|
|
def get_new_streams_in_tx(claims, t, blockhash):
|
2016-02-17 05:10:26 +01:00
|
|
|
rtn = []
|
|
|
|
if claims:
|
|
|
|
for claim in claims:
|
|
|
|
if claim not in self.seen:
|
|
|
|
msg = "[" + str(datetime.now()) + "] New claim | lbry://" + str(claim['name']) + \
|
|
|
|
" | stream hash: " + str(json.loads(claim['value'])['stream_hash'])
|
2016-03-14 17:30:22 +01:00
|
|
|
log.info(msg)
|
|
|
|
if self.verbose:
|
|
|
|
print msg
|
2016-02-17 05:10:26 +01:00
|
|
|
rtn.append((claim['name'], t))
|
|
|
|
self.seen.append(claim)
|
|
|
|
else:
|
|
|
|
if self.verbose:
|
2016-02-19 06:44:08 +01:00
|
|
|
print "[" + str(datetime.now()) + "] No claims in block", blockhash
|
2016-02-17 05:10:26 +01:00
|
|
|
return rtn
|
|
|
|
|
|
|
|
d.addCallback(lambda streams: defer.DeferredList(
|
|
|
|
[self.wallet.get_stream_info_from_txid(name, t) for name, t in streams]))
|
|
|
|
return d
|
2015-12-14 21:54:33 +01:00
|
|
|
|
|
|
|
def _download_claims(self, claims):
|
|
|
|
if claims:
|
|
|
|
for claim in claims:
|
|
|
|
stream = GetStream(self.sd_identifier, self.session, self.wallet, self.lbry_file_manager,
|
|
|
|
self.max_key_fee, pay_key=False)
|
2016-02-17 05:10:26 +01:00
|
|
|
stream.start(claim[1])
|
2015-12-14 21:54:33 +01:00
|
|
|
|
|
|
|
return defer.succeed(None)
|
|
|
|
|
|
|
|
def _looped_search(self):
|
2016-02-17 05:10:26 +01:00
|
|
|
d = self._get_names()
|
2015-12-14 21:54:33 +01:00
|
|
|
d.addCallback(self._download_claims)
|
2016-02-17 05:10:26 +01:00
|
|
|
return d
|
2015-12-14 21:54:33 +01:00
|
|
|
|
|
|
|
def _get_autofetcher_conf(self):
|
|
|
|
settings = {"maxkey": "0.0"}
|
|
|
|
if os.path.exists(self.autofetcher_conf):
|
|
|
|
conf = open(self.autofetcher_conf)
|
|
|
|
for l in conf:
|
|
|
|
if l.startswith("maxkey="):
|
|
|
|
settings["maxkey"] = float(l[7:].rstrip('\n'))
|
2016-03-14 17:30:22 +01:00
|
|
|
conf.close()
|
2015-12-14 21:54:33 +01:00
|
|
|
else:
|
2016-03-14 17:30:22 +01:00
|
|
|
conf = open(self.autofetcher_conf, "w")
|
|
|
|
conf.write("maxkey=10.0")
|
|
|
|
conf.close()
|
|
|
|
settings["maxkey"] = 10.0
|
|
|
|
log.info("No autofetcher conf file found, making one with max key fee of 10.0")
|
2015-12-14 21:54:33 +01:00
|
|
|
|
|
|
|
self.max_key_fee = settings["maxkey"]
|