From 42c26102d82a5615e371aec4243c67706dc99a7d Mon Sep 17 00:00:00 2001 From: Jack Date: Mon, 11 Jan 2016 15:52:38 -0500 Subject: [PATCH 01/10] Make daemon shut down properly Makes the daemon shutdown properly and adds better start/stop messages. --- lbrynet/lbrynet_daemon/LBRYDaemon.py | 23 +++++++++++++++++++++-- lbrynet/lbrynet_daemon/LBRYDownloader.py | 1 + 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/lbrynet/lbrynet_daemon/LBRYDaemon.py b/lbrynet/lbrynet_daemon/LBRYDaemon.py index e827059bf..a503668c3 100644 --- a/lbrynet/lbrynet_daemon/LBRYDaemon.py +++ b/lbrynet/lbrynet_daemon/LBRYDaemon.py @@ -84,6 +84,10 @@ class LBRYDaemon(xmlrpc.XMLRPC): self.max_key_fee = 100.0 return defer.succeed(None) + def _disp_startup(): + print "Started LBRYnet daemon" + return defer.succeed(None) + d = defer.Deferred() d.addCallback(lambda _: _set_vars()) d.addCallback(lambda _: threads.deferToThread(self._setup_data_directory)) @@ -96,10 +100,19 @@ class LBRYDaemon(xmlrpc.XMLRPC): d.addCallback(lambda _: self._setup_lbry_file_manager()) d.addCallback(lambda _: self._setup_lbry_file_opener()) d.addCallback(lambda _: self._setup_fetcher()) + d.addCallback(lambda _: _disp_startup()) d.callback(None) return defer.succeed(None) + def _shutdown(self): + print 'Closing lbrynet session' + if self.session is not None: + d = self.session.shut_down() + else: + d = defer.Deferred() + return d + def _update_settings(self): self.data_rate = self.session_settings['data_rate'] self.max_key_fee = self.session_settings['max_key_fee'] @@ -366,8 +379,14 @@ class LBRYDaemon(xmlrpc.XMLRPC): Stop the reactor """ - reactor.stop() - return defer.succeed('Stopping') + def _disp_shutdown(): + print 'Shutting down lbrynet daemon' + + d = self._shutdown() + d.addCallback(lambda _: _disp_shutdown()) + d.addCallback(lambda _: reactor.stop()) + + return d def xmlrpc_get_lbry_files(self): """ diff --git a/lbrynet/lbrynet_daemon/LBRYDownloader.py b/lbrynet/lbrynet_daemon/LBRYDownloader.py index 42559dadd..2ca70506c 100644 --- a/lbrynet/lbrynet_daemon/LBRYDownloader.py +++ b/lbrynet/lbrynet_daemon/LBRYDownloader.py @@ -231,6 +231,7 @@ class FetcherDaemon(object): for l in conf: if l.startswith("maxkey="): settings["maxkey"] = float(l[7:].rstrip('\n')) + print "Autofetcher using max key price of", settings["maxkey"], ", to start call start_fetcher()" else: print "Autofetcher using default max key price of 0.0" print "To change this create the file:" From 12cd06cae550d0045ef7e46aec2b0c393b330247 Mon Sep 17 00:00:00 2001 From: Jack Date: Mon, 11 Jan 2016 18:01:46 -0500 Subject: [PATCH 02/10] Add download history logging Logs downloads as to not make duplicate files --- lbrynet/lbrynet_daemon/LBRYDaemon.py | 102 +++++++++++++++++++++++---- 1 file changed, 87 insertions(+), 15 deletions(-) diff --git a/lbrynet/lbrynet_daemon/LBRYDaemon.py b/lbrynet/lbrynet_daemon/LBRYDaemon.py index a503668c3..377a64423 100644 --- a/lbrynet/lbrynet_daemon/LBRYDaemon.py +++ b/lbrynet/lbrynet_daemon/LBRYDaemon.py @@ -1,3 +1,4 @@ +import sqlite3 from lbrynet.lbryfile.StreamDescriptor import LBRYFileStreamType from lbrynet.lbryfile.client.LBRYFileDownloader import LBRYFileSaverFactory, LBRYFileOpenerFactory from lbrynet.lbryfile.client.LBRYFileOptions import add_lbry_file_to_sd_identifier @@ -20,6 +21,20 @@ import sys log = logging.getLogger(__name__) +class DummyDownloader(object): + def __init__(self, directory, file_name): + self.download_directory = directory + self.file_name = file_name + +class DummyStream(object): + def __init__(self, row): + download_directory = os.path.join(*row[2].split('/')[:-1]) + file_name = row[2].split('/')[len(row[2].split('/')) - 1] + + self.stream_hash = row[0] + self.downloader = DummyDownloader(download_directory, file_name) + self.is_dummy = True + class LBRYDaemon(xmlrpc.XMLRPC): """ @@ -282,23 +297,28 @@ class LBRYDaemon(xmlrpc.XMLRPC): self.sd_identifier.add_stream_downloader_factory(LBRYFileStreamType, downloader_factory) return defer.succeed(True) - def _download_name(self, name): + def _download_name(self, history, name): def _disp(stream): print '[' + str(datetime.now()) + ']' + ' Downloading: ' + str(stream.stream_hash) log.debug('[' + str(datetime.now()) + ']' + ' Downloading: ' + str(stream.stream_hash)) return defer.succeed(None) - 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) - self.downloads.append(stream) + if not history: + 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) - d = self.session.wallet.get_stream_info_for_name(name) - d.addCallback(lambda stream_info: stream.start(stream_info)) - d.addCallback(lambda _: _disp(stream)) - d.addCallback(lambda _: {'ts': datetime.now(),'name': name}) - d.addErrback(lambda err: str(err.getTraceback())) + self.downloads.append(stream) - return d + d = self.session.wallet.get_stream_info_for_name(name) + d.addCallback(lambda stream_info: stream.start(stream_info)) + d.addCallback(lambda _: _disp(stream)) + d.addCallback(lambda _: {'ts': datetime.now(),'name': name}) + d.addErrback(lambda err: str(err.getTraceback())) + return d + + else: + self.downloads.append(DummyStream(history[0])) + return defer.succeed(None) def _path_from_name(self, name): d = self.session.wallet.get_stream_info_for_name(name) @@ -327,6 +347,57 @@ class LBRYDaemon(xmlrpc.XMLRPC): d.callback(None) return d + def _check_history(self, name, metadata): + con = sqlite3.connect(os.path.join(self.db_dir, 'daemon.sqlite')) + cur = con.cursor() + + query = "create table if not exists history \ + (stream_hash char(96) primary key not null,\ + uri text not null, \ + path text not null);" + + cur.execute(query) + con.commit() + r = cur.execute("select * from history where stream_hash='" + metadata['stream_hash'] + "'") + files = r.fetchall() + + if files: + if not os.path.isfile(files[0][2]): + print "Couldn't find", files[0][2], ", trying to redownload it" + cur.execute("delete from history where stream_hash='" + files[0][0] + "'") + con.commit() + con.close() + return [] + else: + con.close() + return files + else: + con.close() + return files + + def _add_to_history(self, name, path): + con = sqlite3.connect(os.path.join(self.db_dir, 'daemon.sqlite')) + cur = con.cursor() + + query = "create table if not exists history \ + (stream_hash char(96) primary key not null,\ + uri text not null, \ + path text not null);" + + cur.execute(query) + con.commit() + + r = cur.execute("select * from history where stream_hash='" + path['stream_hash'] + "'") + files = r.fetchall() + if not files: + vals = path['stream_hash'], name, path['path'] + r = cur.execute("insert into history values (?, ?, ?)", vals) + con.commit() + else: + print 'Already downloaded', path['stream_hash'], '->', path['path'] + con.close() + return path['path'] + def xmlrpc_get_settings(self): """ Get LBRY payment settings @@ -447,7 +518,7 @@ class LBRYDaemon(xmlrpc.XMLRPC): return defer.succeed(None) 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) + max_key_fee=self.max_key_fee, data_rate=self.data_rate) self.downloads.append(stream) @@ -456,7 +527,6 @@ class LBRYDaemon(xmlrpc.XMLRPC): d.addCallback(lambda _: _disp(stream)) d.addCallback(lambda _: {'ts': datetime.now(),'name': name}) d.addErrback(lambda err: str(err.getTraceback())) - return d def xmlrpc_path_from_name(self, name): @@ -484,10 +554,12 @@ class LBRYDaemon(xmlrpc.XMLRPC): @return: {stream_hash, path}: """ - d = defer.Deferred(None) - d.addCallback(lambda _: self._download_name(name)) + d = self._resolve_name(name) + d.addCallback(lambda metadata: self._check_history(name, metadata)) + d.addCallback(lambda hist: self._download_name(hist, name)) + d.addCallback(lambda _: self._path_from_name(name)) + d.addCallback(lambda path: self._add_to_history(name, path)) d.addCallback(lambda _: self._path_from_name(name)) - d.callback(None) return d From 120d7edf12d3164ff5a1cfe4c9a96b9a42a5496f Mon Sep 17 00:00:00 2001 From: Jack Date: Tue, 12 Jan 2016 12:42:32 -0500 Subject: [PATCH 03/10] Better handle bad names, improve browser uri handler Play videos in browser rather than in a separate application window --- lbrynet/lbrynet_daemon/LBRYDaemon.py | 86 +++++++++++-------- .../LBRYURIHandler/LBRYURIHandler.py | 34 +++++++- 2 files changed, 83 insertions(+), 37 deletions(-) diff --git a/lbrynet/lbrynet_daemon/LBRYDaemon.py b/lbrynet/lbrynet_daemon/LBRYDaemon.py index 377a64423..1679be8d2 100644 --- a/lbrynet/lbrynet_daemon/LBRYDaemon.py +++ b/lbrynet/lbrynet_daemon/LBRYDaemon.py @@ -1,4 +1,4 @@ -import sqlite3 +from lbrynet.core.Error import UnknownNameError from lbrynet.lbryfile.StreamDescriptor import LBRYFileStreamType from lbrynet.lbryfile.client.LBRYFileDownloader import LBRYFileSaverFactory, LBRYFileOpenerFactory from lbrynet.lbryfile.client.LBRYFileOptions import add_lbry_file_to_sd_identifier @@ -18,14 +18,17 @@ from datetime import datetime import logging import os import sys +import sqlite3 log = logging.getLogger(__name__) + class DummyDownloader(object): def __init__(self, directory, file_name): self.download_directory = directory self.file_name = file_name + class DummyStream(object): def __init__(self, row): download_directory = os.path.join(*row[2].split('/')[:-1]) @@ -97,6 +100,8 @@ class LBRYDaemon(xmlrpc.XMLRPC): self.session_settings = None self.data_rate = 0.5 self.max_key_fee = 100.0 + self.db = None + self.cur = None return defer.succeed(None) def _disp_startup(): @@ -115,6 +120,7 @@ class LBRYDaemon(xmlrpc.XMLRPC): d.addCallback(lambda _: self._setup_lbry_file_manager()) d.addCallback(lambda _: self._setup_lbry_file_opener()) d.addCallback(lambda _: self._setup_fetcher()) + d.addCallback(lambda _: self._setup_daemon_db()) d.addCallback(lambda _: _disp_startup()) d.callback(None) @@ -172,6 +178,33 @@ class LBRYDaemon(xmlrpc.XMLRPC): return d return defer.succeed(True) + def _setup_daemon_db(self): + self.db = sqlite3.connect(os.path.join(self.db_dir, 'daemon.sqlite')) + self.cur = self.db.cursor() + + query = "create table if not exists history \ + (stream_hash char(96) primary key not null,\ + uri text not null, \ + path text not null);" + + self.cur.execute(query) + self.db.commit() + + r = self.cur.execute("select * from history") + files = r.fetchall() + + print "Checking files in download history still exist, pruning records of those that don't" + + for file in files: + if not os.path.isfile(file[2]): + print "Couldn't find", file[2], ", removing record" + self.cur.execute("delete from history where stream_hash='" + file[0] + "'") + self.db.commit() + + print "Done checking records" + + return defer.succeed(None) + def _get_settings(self): d = self.settings.start() d.addCallback(lambda _: self.settings.get_lbryid()) @@ -303,6 +336,9 @@ class LBRYDaemon(xmlrpc.XMLRPC): log.debug('[' + str(datetime.now()) + ']' + ' Downloading: ' + str(stream.stream_hash)) return defer.succeed(None) + if history == 'UnknownNameError': + return 'UnknownNameError' + if not history: 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) @@ -348,60 +384,43 @@ class LBRYDaemon(xmlrpc.XMLRPC): return d def _check_history(self, name, metadata): - con = sqlite3.connect(os.path.join(self.db_dir, 'daemon.sqlite')) - cur = con.cursor() + if metadata == 'UnknownNameError': + return 'UnknownNameError' - query = "create table if not exists history \ - (stream_hash char(96) primary key not null,\ - uri text not null, \ - path text not null);" - - cur.execute(query) - con.commit() - r = cur.execute("select * from history where stream_hash='" + metadata['stream_hash'] + "'") + r = self.cur.execute("select * from history where stream_hash='" + metadata['stream_hash'] + "'") files = r.fetchall() if files: if not os.path.isfile(files[0][2]): print "Couldn't find", files[0][2], ", trying to redownload it" - cur.execute("delete from history where stream_hash='" + files[0][0] + "'") - con.commit() - con.close() + self.cur.execute("delete from history where stream_hash='" + files[0][0] + "'") + self.db.commit() return [] else: - con.close() return files else: - con.close() return files def _add_to_history(self, name, path): - con = sqlite3.connect(os.path.join(self.db_dir, 'daemon.sqlite')) - cur = con.cursor() + if path == 'UnknownNameError': + return 'UnknownNameError' - query = "create table if not exists history \ - (stream_hash char(96) primary key not null,\ - uri text not null, \ - path text not null);" - - cur.execute(query) - con.commit() - - r = cur.execute("select * from history where stream_hash='" + path['stream_hash'] + "'") + r = self.cur.execute("select * from history where stream_hash='" + path['stream_hash'] + "'") files = r.fetchall() if not files: vals = path['stream_hash'], name, path['path'] - r = cur.execute("insert into history values (?, ?, ?)", vals) - con.commit() + self.cur.execute("insert into history values (?, ?, ?)", vals) + self.db.commit() else: print 'Already downloaded', path['stream_hash'], '->', path['path'] - con.close() - return path['path'] + + return path def xmlrpc_get_settings(self): """ Get LBRY payment settings """ + if not self.session_settings: self.session_settings = {'data_rate': self.data_rate, 'max_key_fee': self.max_key_fee} @@ -454,6 +473,7 @@ class LBRYDaemon(xmlrpc.XMLRPC): print 'Shutting down lbrynet daemon' d = self._shutdown() + d.addCallback(lambda _: self.db.close()) d.addCallback(lambda _: _disp_shutdown()) d.addCallback(lambda _: reactor.stop()) @@ -482,8 +502,7 @@ class LBRYDaemon(xmlrpc.XMLRPC): d = defer.Deferred() d.addCallback(lambda _: self.session.wallet.get_stream_info_for_name(name)) - d.addErrback(lambda _: 'UnknownNameError') - d.addCallback(_disp) + d.addCallbacks(_disp, lambda _: str('UnknownNameError')) d.callback(None) return d @@ -559,7 +578,6 @@ class LBRYDaemon(xmlrpc.XMLRPC): d.addCallback(lambda hist: self._download_name(hist, name)) d.addCallback(lambda _: self._path_from_name(name)) d.addCallback(lambda path: self._add_to_history(name, path)) - d.addCallback(lambda _: self._path_from_name(name)) return d diff --git a/lbrynet/lbrynet_daemon/LBRYURIHandler/LBRYURIHandler.py b/lbrynet/lbrynet_daemon/LBRYURIHandler/LBRYURIHandler.py index 81bc0fb83..34ee2b5f5 100644 --- a/lbrynet/lbrynet_daemon/LBRYURIHandler/LBRYURIHandler.py +++ b/lbrynet/lbrynet_daemon/LBRYURIHandler/LBRYURIHandler.py @@ -1,6 +1,11 @@ +import os +import json import webbrowser import xmlrpclib, sys +def render_video(path): + r = r'
' + return r def main(args): if len(args) == 0: @@ -12,9 +17,32 @@ def main(args): print 'Too many args', args else: - daemon.download_name(str(args[0])[7:]) - path = daemon.path_from_name(args[0][7:])[0]['path'] - webbrowser.open('file://' + path) + if args[0][7:] != 'settings': + r = daemon.get(args[0][7:]) + print r + path = r['path'] + if path[0] != '/': + path = '/' + path + + print path + filename = path.split('/')[len(path.split('/')) - 1] + extension = path.split('.')[len(path.split('.')) - 1] + + if extension in ['mp4', 'flv', 'mov']: + h = str(render_video(path)) + f = open('lbry.html', 'w') + f.write(h) + f.close() + webbrowser.open('file://' + os.path.join(os.getcwd(), 'lbry.html')) + + else: + webbrowser.open('file://' + path) + else: + r = daemon.get_settings() + f = open('lbry.html', 'w') + f.write("" + json.dumps(r) + "") + f.close() + webbrowser.open('file://' + os.path.join(os.getcwd(), 'lbry.html')) if __name__ == "__main__": main(sys.argv[1:]) \ No newline at end of file From 905274d6d4137507d49a0986bf466e456a8d5b0c Mon Sep 17 00:00:00 2001 From: Jack Date: Tue, 12 Jan 2016 13:40:51 -0500 Subject: [PATCH 04/10] Use same downloader for daemon as is used for console Use lbrynet.lbryfilemanager.LBRYFileDownloader.ManagedLBRYFileDownloaderFact ory instead of lbrynet.lbryfile.client.LBRYFileDownloader.LBRYFileSaverFactory --- lbrynet/lbrynet_daemon/LBRYDaemon.py | 2 +- lbrynet/lbrynet_daemon/LBRYDownloader.py | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/lbrynet/lbrynet_daemon/LBRYDaemon.py b/lbrynet/lbrynet_daemon/LBRYDaemon.py index 1679be8d2..92a0e51d8 100644 --- a/lbrynet/lbrynet_daemon/LBRYDaemon.py +++ b/lbrynet/lbrynet_daemon/LBRYDaemon.py @@ -412,7 +412,7 @@ class LBRYDaemon(xmlrpc.XMLRPC): self.cur.execute("insert into history values (?, ?, ?)", vals) self.db.commit() else: - print 'Already downloaded', path['stream_hash'], '->', path['path'] + print 'Already downloaded', path['stream_hash'], '-->', path['path'] return path diff --git a/lbrynet/lbrynet_daemon/LBRYDownloader.py b/lbrynet/lbrynet_daemon/LBRYDownloader.py index 2ca70506c..15c5eafde 100644 --- a/lbrynet/lbrynet_daemon/LBRYDownloader.py +++ b/lbrynet/lbrynet_daemon/LBRYDownloader.py @@ -117,7 +117,7 @@ class GetStream(object): def _handle_metadata(self, metadata): self.metadata = metadata - self.factory = self.metadata.factories[0] + self.factory = self.metadata.factories[1] return defer.succeed(None) def _handle_download_error(self, err): @@ -134,9 +134,9 @@ class GetStream(object): self.downloader.file_name) return self.downloader - self.downloader = self.factory.make_downloader(self.metadata, [self.data_rate, True], self.payment_rate_manager) - self.downloader.addCallback(_set_downloader) - return defer.succeed(self.downloader) + downloader = self.factory.make_downloader(self.metadata, [self.data_rate, True], self.payment_rate_manager) + downloader.addCallback(_set_downloader) + return downloader class FetcherDaemon(object): From ebb495ab8b4d5177d31a1e740adadf8e785d17b9 Mon Sep 17 00:00:00 2001 From: Jack Date: Tue, 12 Jan 2016 13:41:20 -0500 Subject: [PATCH 05/10] Fix error at shutdown Fixed messy error message that sometimes appeared at shutdown --- lbrynet/core/Session.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lbrynet/core/Session.py b/lbrynet/core/Session.py index cc31c08ff..545415cd9 100644 --- a/lbrynet/core/Session.py +++ b/lbrynet/core/Session.py @@ -267,4 +267,6 @@ class LBRYSession(object): log.info("Removed UPnP redirect for %s %d.", protocol, port) self.upnp_redirects = [] - return threads.deferToThread(threaded_unset_upnp) \ No newline at end of file + d = threads.deferToThread(threaded_unset_upnp) + d.addErrback(lambda err: str(err)) + return d \ No newline at end of file From 2085012874b50057752c3fc163dbf8c204632727 Mon Sep 17 00:00:00 2001 From: Jack Date: Wed, 13 Jan 2016 09:42:30 -0500 Subject: [PATCH 06/10] Get managed lbry files --- lbrynet/lbrynet_daemon/LBRYDaemon.py | 39 ++++++++++++++++++++++++---- x.sh | 3 +++ 2 files changed, 37 insertions(+), 5 deletions(-) create mode 100755 x.sh diff --git a/lbrynet/lbrynet_daemon/LBRYDaemon.py b/lbrynet/lbrynet_daemon/LBRYDaemon.py index 92a0e51d8..7c96b73a3 100644 --- a/lbrynet/lbrynet_daemon/LBRYDaemon.py +++ b/lbrynet/lbrynet_daemon/LBRYDaemon.py @@ -1,3 +1,4 @@ +import binascii from lbrynet.core.Error import UnknownNameError from lbrynet.lbryfile.StreamDescriptor import LBRYFileStreamType from lbrynet.lbryfile.client.LBRYFileDownloader import LBRYFileSaverFactory, LBRYFileOpenerFactory @@ -19,6 +20,7 @@ import logging import os import sys import sqlite3 +import json log = logging.getLogger(__name__) @@ -392,7 +394,7 @@ class LBRYDaemon(xmlrpc.XMLRPC): if files: if not os.path.isfile(files[0][2]): - print "Couldn't find", files[0][2], ", trying to redownload it" + print "[" + str(datetime.now()) + "] Couldn't find", files[0][2], ", trying to redownload it" self.cur.execute("delete from history where stream_hash='" + files[0][0] + "'") self.db.commit() return [] @@ -412,7 +414,7 @@ class LBRYDaemon(xmlrpc.XMLRPC): self.cur.execute("insert into history values (?, ?, ?)", vals) self.db.commit() else: - print 'Already downloaded', path['stream_hash'], '-->', path['path'] + print '[' + str(datetime.now()) + '] Already downloaded', path['stream_hash'], '-->', path['path'] return path @@ -424,12 +426,14 @@ class LBRYDaemon(xmlrpc.XMLRPC): if not self.session_settings: self.session_settings = {'data_rate': self.data_rate, 'max_key_fee': self.max_key_fee} + print '[' + str(datetime.now()) + '] Get daemon settings' return self.session_settings def xmlrpc_set_settings(self, settings): self.session_settings = settings self._update_settings() + print '[' + str(datetime.now()) + '] Set daemon settings' return 'Set' def xmlrpc_start_fetcher(self): @@ -438,7 +442,7 @@ class LBRYDaemon(xmlrpc.XMLRPC): """ self.fetcher.start() - + print '[' + str(datetime.now()) + '] Start autofetcher' return str('Started autofetching') def xmlrpc_stop_fetcher(self): @@ -447,7 +451,7 @@ class LBRYDaemon(xmlrpc.XMLRPC): """ self.fetcher.stop() - + print '[' + str(datetime.now()) + '] Stop autofetcher' return str('Started autofetching') def xmlrpc_fetcher_status(self): @@ -455,6 +459,7 @@ class LBRYDaemon(xmlrpc.XMLRPC): Start autofetcher """ + print '[' + str(datetime.now()) + '] Get fetcher status' return str(self.fetcher.check_if_running()) def xmlrpc_get_balance(self): @@ -462,6 +467,7 @@ class LBRYDaemon(xmlrpc.XMLRPC): Get LBC balance """ + print '[' + str(datetime.now()) + '] Get balance' return str(self.session.wallet.wallet_balance) def xmlrpc_stop(self): @@ -486,7 +492,24 @@ class LBRYDaemon(xmlrpc.XMLRPC): @return: Managed LBRY files """ - return [[str(i), str(dir(i))] for i in self.lbry_file_manager.lbry_files] + r = [] + for f in self.lbry_file_manager.lbry_files: + if f.key: + t = {'completed': f.completed, 'file_handle': f.file_handle, '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_handle': f.file_handle, '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(json.dumps(t)) + + print '[' + str(datetime.now()) + '] Get LBRY files' + return r def xmlrpc_resolve_name(self, name): """ @@ -522,6 +545,7 @@ class LBRYDaemon(xmlrpc.XMLRPC): except: pass + print '[' + str(datetime.now()) + '] Get downloads' return downloads def xmlrpc_download_name(self, name): @@ -580,6 +604,11 @@ class LBRYDaemon(xmlrpc.XMLRPC): d.addCallback(lambda path: self._add_to_history(name, path)) return d + def xmlrpc_toggle_lbry_file_status(self, stream_hash): + d = self.lbry_file_manager.toggle_lbry_file_running(stream_hash) + d.addErrback(lambda err: str(err)) + return d + def main(): daemon = LBRYDaemon() diff --git a/x.sh b/x.sh new file mode 100755 index 000000000..1174512d8 --- /dev/null +++ b/x.sh @@ -0,0 +1,3 @@ +python setup.py install +clear +lbrynet-daemon From 885a0882f27da614fde75ada511715248fe77b48 Mon Sep 17 00:00:00 2001 From: Jack Date: Sat, 16 Jan 2016 23:06:24 -0500 Subject: [PATCH 07/10] Update daemon and uri handler --- lbrynet/lbrynet_daemon/LBRYDaemon.py | 93 ++++++++++++++++++- .../LBRYURIHandler/LBRYURIHandler.py | 31 ++++--- 2 files changed, 109 insertions(+), 15 deletions(-) diff --git a/lbrynet/lbrynet_daemon/LBRYDaemon.py b/lbrynet/lbrynet_daemon/LBRYDaemon.py index 7c96b73a3..64a20cb9b 100644 --- a/lbrynet/lbrynet_daemon/LBRYDaemon.py +++ b/lbrynet/lbrynet_daemon/LBRYDaemon.py @@ -1,4 +1,5 @@ import binascii +import webbrowser from lbrynet.core.Error import UnknownNameError from lbrynet.lbryfile.StreamDescriptor import LBRYFileStreamType from lbrynet.lbryfile.client.LBRYFileDownloader import LBRYFileSaverFactory, LBRYFileOpenerFactory @@ -25,6 +26,9 @@ import json log = logging.getLogger(__name__) +#TODO add login credentials in a conf file + + class DummyDownloader(object): def __init__(self, directory, file_name): self.download_directory = directory @@ -482,6 +486,7 @@ class LBRYDaemon(xmlrpc.XMLRPC): d.addCallback(lambda _: self.db.close()) d.addCallback(lambda _: _disp_shutdown()) d.addCallback(lambda _: reactor.stop()) + d.callback(None) return d @@ -609,6 +614,92 @@ class LBRYDaemon(xmlrpc.XMLRPC): d.addErrback(lambda err: str(err)) return d + def xmlrpc_render_html(self, html): + def _make_file(html, path): + f = open(path, 'w') + f.write(html) + f.close() + return defer.succeed(None) + + def _disp_err(err): + print str(err.getTraceback()) + return err + + path = os.path.join(self.download_directory, 'lbry.html') + + d = defer.Deferred() + d.addCallback(lambda _: _make_file(html, path)) + d.addCallback(lambda _: webbrowser.open('file://' + path)) + d.addErrback(_disp_err) + d.callback(None) + + return d + + def xmlrpc_render_gui(self): + def _disp_err(err): + print str(err.getTraceback()) + return err + d = defer.Deferred() + d.addCallback(lambda _: webbrowser.open("https://rawgit.com/jackrobison/lbry.io/local/view/page/demo.html")) + d.addErrback(_disp_err) + d.callback(None) + + return d + + def xmlrpc_search_nametrie(self, search): + def _return_d(x): + d = defer.Deferred() + d.addCallback(lambda _: x) + d.callback(None) + + return d + + def _clean(n): + t = [] + for i in n: + if i[0]: + if i[1][0][0] and i[1][1][0]: + i[1][0][1]['value'] = str(i[1][0][1]['value']) + t.append([i[1][0][1], i[1][1][1]]) + return t + + def _parse(results): + f = [] + for chain, meta in results: + t = {} + if 'name' in chain.keys(): + t['name'] = chain['name'] + if 'thumbnail' in meta.keys(): + t['img'] = meta['thumbnail'] + if 'name' in meta.keys(): + t['title'] = meta['name'] + if 'description' in meta.keys(): + t['description'] = meta['description'] + if 'key_fee' in meta.keys(): + t['cost_est'] = meta['key_fee'] + else: + t['cost_est'] = 0.0 + f.append(t) + + return f + + def _disp(results): + print '[' + str(datetime.now()) + '] Found ' + str(len(results)) + ' results' + return results + + print '[' + str(datetime.now()) + '] Search nametrie: ' + search + + filtered_results = [n for n in self.rpc_conn.getnametrie() if n['name'].startswith(search)] + filtered_results = [n for n in filtered_results if 'txid' in n.keys()] + resolved_results = [defer.DeferredList([_return_d(n), self._resolve_name(n['name'])]) for n in filtered_results] + + d = defer.DeferredList(resolved_results) + d.addCallback(_clean) + d.addCallback(_parse) + d.addCallback(_disp) + + return d + def main(): daemon = LBRYDaemon() @@ -617,4 +708,4 @@ def main(): reactor.run() if __name__ == '__main__': - main() \ No newline at end of file + main() diff --git a/lbrynet/lbrynet_daemon/LBRYURIHandler/LBRYURIHandler.py b/lbrynet/lbrynet_daemon/LBRYURIHandler/LBRYURIHandler.py index 34ee2b5f5..153b6eb95 100644 --- a/lbrynet/lbrynet_daemon/LBRYURIHandler/LBRYURIHandler.py +++ b/lbrynet/lbrynet_daemon/LBRYURIHandler/LBRYURIHandler.py @@ -3,10 +3,12 @@ import json import webbrowser import xmlrpclib, sys + def render_video(path): - r = r'
' + r = r'
' return r + def main(args): if len(args) == 0: args.append('lbry://wonderfullife') @@ -17,7 +19,16 @@ def main(args): print 'Too many args', args else: - if args[0][7:] != 'settings': + + if args[0][7:] == 'lbry': + daemon.render_gui() + + elif args[0][7:] == 'settings': + r = daemon.get_settings() + html = "" + json.dumps(r) + "" + r = daemon.render_html(html) + + else: r = daemon.get(args[0][7:]) print r path = r['path'] @@ -29,20 +40,12 @@ def main(args): extension = path.split('.')[len(path.split('.')) - 1] if extension in ['mp4', 'flv', 'mov']: - h = str(render_video(path)) - f = open('lbry.html', 'w') - f.write(h) - f.close() - webbrowser.open('file://' + os.path.join(os.getcwd(), 'lbry.html')) + html = render_video(path) + daemon.render_html(html) else: - webbrowser.open('file://' + path) - else: - r = daemon.get_settings() - f = open('lbry.html', 'w') - f.write("" + json.dumps(r) + "") - f.close() - webbrowser.open('file://' + os.path.join(os.getcwd(), 'lbry.html')) + webbrowser.open('file://' + str(path)) + if __name__ == "__main__": main(sys.argv[1:]) \ No newline at end of file From e2ae1ca866e96a722232f3a94e700ed0c371c00d Mon Sep 17 00:00:00 2001 From: Jack Date: Sun, 17 Jan 2016 01:49:17 -0500 Subject: [PATCH 08/10] Remove file handle --- lbrynet/lbrynet_daemon/LBRYDaemon.py | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/lbrynet/lbrynet_daemon/LBRYDaemon.py b/lbrynet/lbrynet_daemon/LBRYDaemon.py index 64a20cb9b..aa21d434a 100644 --- a/lbrynet/lbrynet_daemon/LBRYDaemon.py +++ b/lbrynet/lbrynet_daemon/LBRYDaemon.py @@ -500,16 +500,15 @@ class LBRYDaemon(xmlrpc.XMLRPC): r = [] for f in self.lbry_file_manager.lbry_files: if f.key: - t = {'completed': f.completed, 'file_handle': f.file_handle, '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} + 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_handle': f.file_handle, '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} + 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(json.dumps(t)) From 888d77c96c6880aaaee1f6987390c74433c382d9 Mon Sep 17 00:00:00 2001 From: Jack Date: Tue, 19 Jan 2016 21:07:16 -0500 Subject: [PATCH 09/10] Update lbrynet-daemon -Get rid of separate db for daemon, instead use already existing files -Simplify the get function -Add start/stop functions for lbry files --- lbrynet/lbrynet_daemon/LBRYDaemon.py | 340 ++++++++++------------- lbrynet/lbrynet_daemon/LBRYDownloader.py | 110 +++----- 2 files changed, 178 insertions(+), 272 deletions(-) diff --git a/lbrynet/lbrynet_daemon/LBRYDaemon.py b/lbrynet/lbrynet_daemon/LBRYDaemon.py index aa21d434a..8c7eede53 100644 --- a/lbrynet/lbrynet_daemon/LBRYDaemon.py +++ b/lbrynet/lbrynet_daemon/LBRYDaemon.py @@ -1,5 +1,3 @@ -import binascii -import webbrowser from lbrynet.core.Error import UnknownNameError from lbrynet.lbryfile.StreamDescriptor import LBRYFileStreamType from lbrynet.lbryfile.client.LBRYFileDownloader import LBRYFileSaverFactory, LBRYFileOpenerFactory @@ -20,29 +18,24 @@ from datetime import datetime import logging import os import sys -import sqlite3 import json +import binascii +import webbrowser log = logging.getLogger(__name__) #TODO add login credentials in a conf file +#issues with delete: +#TODO when stream is complete the generated file doesn't delete, but blobs do +#TODO when stream is stopped the generated file is deleted -class DummyDownloader(object): - def __init__(self, directory, file_name): - self.download_directory = directory - self.file_name = file_name - - -class DummyStream(object): - def __init__(self, row): - download_directory = os.path.join(*row[2].split('/')[:-1]) - file_name = row[2].split('/')[len(row[2].split('/')) - 1] - - self.stream_hash = row[0] - self.downloader = DummyDownloader(download_directory, file_name) - self.is_dummy = True +#functions to add: +#TODO publish +#TODO send credits to address +#TODO get new address +#TODO alert if your copy of a lbry file is out of date with the name record class LBRYDaemon(xmlrpc.XMLRPC): @@ -62,7 +55,6 @@ class LBRYDaemon(xmlrpc.XMLRPC): self.peer_port = 3333 self.dht_node_port = 4444 self.first_run = False - self.current_db_revision = 1 if os.name == "nt": from lbrynet.winhelpers.knownpaths import get_path, FOLDERID, UserHandle self.download_directory = get_path(FOLDERID.Downloads, UserHandle.current) @@ -106,8 +98,6 @@ class LBRYDaemon(xmlrpc.XMLRPC): self.session_settings = None self.data_rate = 0.5 self.max_key_fee = 100.0 - self.db = None - self.cur = None return defer.succeed(None) def _disp_startup(): @@ -119,14 +109,13 @@ class LBRYDaemon(xmlrpc.XMLRPC): d.addCallback(lambda _: threads.deferToThread(self._setup_data_directory)) d.addCallback(lambda _: self._check_db_migration()) d.addCallback(lambda _: self._get_settings()) - d.addCallback(lambda _: self.get_lbrycrdd_path()) + d.addCallback(lambda _: self._get_lbrycrdd_path()) d.addCallback(lambda _: self._get_session()) d.addCallback(lambda _: add_lbry_file_to_sd_identifier(self.sd_identifier)) d.addCallback(lambda _: self._setup_stream_identifier()) d.addCallback(lambda _: self._setup_lbry_file_manager()) d.addCallback(lambda _: self._setup_lbry_file_opener()) d.addCallback(lambda _: self._setup_fetcher()) - d.addCallback(lambda _: self._setup_daemon_db()) d.addCallback(lambda _: _disp_startup()) d.callback(None) @@ -184,38 +173,11 @@ class LBRYDaemon(xmlrpc.XMLRPC): return d return defer.succeed(True) - def _setup_daemon_db(self): - self.db = sqlite3.connect(os.path.join(self.db_dir, 'daemon.sqlite')) - self.cur = self.db.cursor() - - query = "create table if not exists history \ - (stream_hash char(96) primary key not null,\ - uri text not null, \ - path text not null);" - - self.cur.execute(query) - self.db.commit() - - r = self.cur.execute("select * from history") - files = r.fetchall() - - print "Checking files in download history still exist, pruning records of those that don't" - - for file in files: - if not os.path.isfile(file[2]): - print "Couldn't find", file[2], ", removing record" - self.cur.execute("delete from history where stream_hash='" + file[0] + "'") - self.db.commit() - - print "Done checking records" - - return defer.succeed(None) - def _get_settings(self): d = self.settings.start() d.addCallback(lambda _: self.settings.get_lbryid()) d.addCallback(self.set_lbryid) - d.addCallback(lambda _: self.get_lbrycrdd_path()) + d.addCallback(lambda _: self._get_lbrycrdd_path()) return d def set_lbryid(self, lbryid): @@ -285,7 +247,7 @@ class LBRYDaemon(xmlrpc.XMLRPC): dl.addCallback(lambda _: self.session.setup()) return dl - def get_lbrycrdd_path(self): + def _get_lbrycrdd_path(self): def get_lbrycrdd_path_conf_file(): lbrycrdd_path_conf_path = os.path.join(os.path.expanduser("~"), ".lbrycrddpath.conf") if not os.path.exists(lbrycrdd_path_conf_path): @@ -336,91 +298,107 @@ class LBRYDaemon(xmlrpc.XMLRPC): self.sd_identifier.add_stream_downloader_factory(LBRYFileStreamType, downloader_factory) return defer.succeed(True) - def _download_name(self, history, name): - def _disp(stream): - print '[' + str(datetime.now()) + ']' + ' Downloading: ' + str(stream.stream_hash) - log.debug('[' + str(datetime.now()) + ']' + ' Downloading: ' + str(stream.stream_hash)) - return defer.succeed(None) - - if history == 'UnknownNameError': - return 'UnknownNameError' - - if not history: - 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) - - self.downloads.append(stream) - - d = self.session.wallet.get_stream_info_for_name(name) - d.addCallback(lambda stream_info: stream.start(stream_info)) - d.addCallback(lambda _: _disp(stream)) - d.addCallback(lambda _: {'ts': datetime.now(),'name': name}) - d.addErrback(lambda err: str(err.getTraceback())) + def _download_name(self, name): + def _disp_file(file): + print '[' + str(datetime.now()) + ']' + ' Already downloaded: ' + str(file.stream_hash) + d = self._path_from_lbry_file(file) return d - else: - self.downloads.append(DummyStream(history[0])) - return defer.succeed(None) + def _get_stream(name): + def _disp(stream): + print '[' + str(datetime.now()) + ']' + ' Start stream: ' + stream['stream_hash'] + return stream + + d = self.session.wallet.get_stream_info_for_name(name) + 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) + d.addCallback(_disp) + d.addCallback(lambda stream_info: stream.start(stream_info)) + d.addCallback(lambda _: self._path_from_name(name)) + + return d + + d = self._check_history(name) + d.addCallback(lambda lbry_file: _get_stream(name) if not lbry_file else _disp_file(lbry_file)) + d.addCallback(lambda _: self._check_history(name)) + d.addCallback(lambda lbry_file: self._path_from_lbry_file(lbry_file) if lbry_file else 'Not found') + d.addErrback(lambda err: str(err)) - def _path_from_name(self, name): - d = self.session.wallet.get_stream_info_for_name(name) - d.addCallback(lambda stream_info: stream_info['stream_hash']) - d.addCallback(lambda stream_hash: [{'stream_hash': stream.stream_hash, - 'path': os.path.join(stream.downloader.download_directory, - stream.downloader.file_name)} - for stream in self.downloads if stream.stream_hash == stream_hash][0]) - d.addErrback(lambda _: 'UnknownNameError') return d - def _get_downloads(self): - downloads = [] - for stream in self.downloads: - try: - downloads.append({'stream_hash': stream.stream_hash, - 'path': os.path.join(stream.downloader.download_directory, stream.downloader.file_name)}) - except: - pass - return downloads - def _resolve_name(self, name): d = defer.Deferred() d.addCallback(lambda _: self.session.wallet.get_stream_info_for_name(name)) - d.addErrback(lambda _: 'UnknownNameError') - d.callback(None) + d.addErrback(lambda _: defer.fail(UnknownNameError)) + return d - def _check_history(self, name, metadata): - if metadata == 'UnknownNameError': - return 'UnknownNameError' - r = self.cur.execute("select * from history where stream_hash='" + metadata['stream_hash'] + "'") - files = r.fetchall() + def _resolve_name_wc(self, name): + d = defer.Deferred() + d.addCallback(lambda _: self.session.wallet.get_stream_info_for_name(name)) + d.addErrback(lambda _: defer.fail(UnknownNameError)) + d.callback(None) - if files: - if not os.path.isfile(files[0][2]): - print "[" + str(datetime.now()) + "] Couldn't find", files[0][2], ", trying to redownload it" - self.cur.execute("delete from history where stream_hash='" + files[0][0] + "'") - self.db.commit() - return [] + return d + + def _check_history(self, name): + def _get_lbry_file(path): + f = open(path, 'r') + l = json.loads(f.read()) + f.close() + file_name = l['stream_name'].decode('hex') + lbry_file = [file for file in self.lbry_file_manager.lbry_files if file.stream_name == file_name][0] + return lbry_file + + def _check(info): + stream_hash = info['stream_hash'] + path = os.path.join(self.blobfile_dir, stream_hash) + if os.path.isfile(path): + print "[" + str(datetime.now()) + "] Search for lbry_file, returning: " + stream_hash + return defer.succeed(_get_lbry_file(path)) else: - return files + print "[" + str(datetime.now()) + "] Search for lbry_file didn't return anything" + return defer.succeed(False) + + d = self._resolve_name(name) + d.addCallbacks(_check, lambda _: False) + d.callback(None) + + return d + + def _delete_lbry_file(self, lbry_file): + d = self.lbry_file_manager.delete_lbry_file(lbry_file) + + def finish_deletion(lbry_file): + d = lbry_file.delete_data() + d.addCallback(lambda _: _delete_stream_data(lbry_file)) + return d + + def _delete_stream_data(lbry_file): + s_h = lbry_file.stream_hash + d = self.lbry_file_manager.get_count_for_stream_hash(s_h) + # 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) + return d + + d.addCallback(lambda _: finish_deletion(lbry_file)) + return d + + def _path_from_name(self, name): + d = self._check_history(name) + d.addCallback(lambda lbry_file: {'stream_hash': lbry_file.stream_hash, + 'path': os.path.join(self.download_directory, lbry_file.file_name)} + if lbry_file else defer.fail(UnknownNameError)) + return d + + def _path_from_lbry_file(self, lbry_file): + if lbry_file: + r = {'stream_hash': lbry_file.stream_hash, + 'path': os.path.join(self.download_directory, lbry_file.file_name)} + return defer.succeed(r) else: - return files - - def _add_to_history(self, name, path): - if path == 'UnknownNameError': - return 'UnknownNameError' - - r = self.cur.execute("select * from history where stream_hash='" + path['stream_hash'] + "'") - files = r.fetchall() - if not files: - vals = path['stream_hash'], name, path['path'] - self.cur.execute("insert into history values (?, ?, ?)", vals) - self.db.commit() - else: - print '[' + str(datetime.now()) + '] Already downloaded', path['stream_hash'], '-->', path['path'] - - return path + return defer.fail(UnknownNameError) def xmlrpc_get_settings(self): """ @@ -483,7 +461,6 @@ class LBRYDaemon(xmlrpc.XMLRPC): print 'Shutting down lbrynet daemon' d = self._shutdown() - d.addCallback(lambda _: self.db.close()) d.addCallback(lambda _: _disp_shutdown()) d.addCallback(lambda _: reactor.stop()) d.callback(None) @@ -523,95 +500,53 @@ class LBRYDaemon(xmlrpc.XMLRPC): """ def _disp(info): - log.debug('[' + str(datetime.now()) + ']' + ' Resolved info: ' + str(info)) - print '[' + str(datetime.now()) + ']' + ' Resolved info: ' + str(info) + log.debug('[' + str(datetime.now()) + ']' + ' Resolved info: ' + str(info['stream_hash'])) + print '[' + str(datetime.now()) + ']' + ' Resolved info: ' + str(info['stream_hash']) return info - d = defer.Deferred() - d.addCallback(lambda _: self.session.wallet.get_stream_info_for_name(name)) + d = self._resolve_name(name) d.addCallbacks(_disp, lambda _: str('UnknownNameError')) d.callback(None) return d - def xmlrpc_get_downloads(self): - """ - Get files downloaded in this session - - @return: [{stream_hash, path}] - """ - - downloads = [] - - for stream in self.downloads: - try: - downloads.append({'stream_hash': stream.stream_hash, - 'path': os.path.join(stream.downloader.download_directory, stream.downloader.file_name)}) - except: - pass - - print '[' + str(datetime.now()) + '] Get downloads' - return downloads - - def xmlrpc_download_name(self, name): + def xmlrpc_get(self, name): """ Download stream from a LBRY uri @param: name """ - def _disp(stream): - print '[' + str(datetime.now()) + ']' + ' Downloading: ' + str(stream.stream_hash) - log.debug('[' + str(datetime.now()) + ']' + ' Downloading: ' + str(stream.stream_hash)) - return defer.succeed(None) + d = self._download_name(name) - 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) - - self.downloads.append(stream) - - d = self.session.wallet.get_stream_info_for_name(name) - d.addCallback(lambda stream_info: stream.start(stream_info)) - d.addCallback(lambda _: _disp(stream)) - d.addCallback(lambda _: {'ts': datetime.now(),'name': name}) - d.addErrback(lambda err: str(err.getTraceback())) return d - def xmlrpc_path_from_name(self, name): - """ - Get file path for a downloaded name + def xmlrpc_stop_lbry_file(self, stream_hash): + try: + lbry_file = [f for f in self.lbry_file_manager.lbry_files if f.stream_hash == stream_hash][0] + except IndexError: + return defer.fail(UnknownNameError) - @param: name - @return: {stream_hash, path}: - """ + if not lbry_file.stopped: + d = self.lbry_file_manager.toggle_lbry_file_running(lbry_file) + d.addCallback(lambda _: 'Stream has been stopped') + d.addErrback(lambda err: str(err)) + return d + else: + return defer.succeed('Stream was already stopped') - d = self.session.wallet.get_stream_info_for_name(name) - d.addCallback(lambda stream_info: stream_info['stream_hash']) - d.addCallback(lambda stream_hash: [{'stream_hash': stream.stream_hash, - 'path': os.path.join(stream.downloader.download_directory, - stream.downloader.file_name)} - for stream in self.downloads if stream.stream_hash == stream_hash][0]) - d.addErrback(lambda _: 'UnknownNameError') - return d + def xmlrpc_start_lbry_file(self, stream_hash): + try: + lbry_file = [f for f in self.lbry_file_manager.lbry_files if f.stream_hash == stream_hash][0] + except IndexError: + return defer.fail(UnknownNameError) - def xmlrpc_get(self, name): - """ - Download a name and return the path of the resulting file + if lbry_file.stopped: + d = self.lbry_file_manager.toggle_lbry_file_running(lbry_file) + d.callback(None) + return defer.succeed('Stream started') + else: + return defer.succeed('Stream was already running') - @param: name: - @return: {stream_hash, path}: - """ - - d = self._resolve_name(name) - d.addCallback(lambda metadata: self._check_history(name, metadata)) - d.addCallback(lambda hist: self._download_name(hist, name)) - d.addCallback(lambda _: self._path_from_name(name)) - d.addCallback(lambda path: self._add_to_history(name, path)) - return d - - def xmlrpc_toggle_lbry_file_status(self, stream_hash): - d = self.lbry_file_manager.toggle_lbry_file_running(stream_hash) - d.addErrback(lambda err: str(err)) - return d def xmlrpc_render_html(self, html): def _make_file(html, path): @@ -690,7 +625,7 @@ class LBRYDaemon(xmlrpc.XMLRPC): filtered_results = [n for n in self.rpc_conn.getnametrie() if n['name'].startswith(search)] filtered_results = [n for n in filtered_results if 'txid' in n.keys()] - resolved_results = [defer.DeferredList([_return_d(n), self._resolve_name(n['name'])]) for n in filtered_results] + resolved_results = [defer.DeferredList([_return_d(n), self._resolve_name_wc(n['name'])]) for n in filtered_results] d = defer.DeferredList(resolved_results) d.addCallback(_clean) @@ -699,6 +634,23 @@ class LBRYDaemon(xmlrpc.XMLRPC): return d + def xmlrpc_delete_lbry_file(self, file_name): + def _disp(file_name): + print '[' + str(datetime.now()) + '] Deleted: ' + file_name + return defer.succeed(str('Deleted: ' + file_name)) + + lbry_files = [self._delete_lbry_file(f) for f in self.lbry_file_manager.lbry_files if file_name == f.file_name] + d = defer.DeferredList(lbry_files) + d.addCallback(lambda _: _disp(file_name)) + return d + + def xmlrpc_check(self, name): + d = self._check_history(name) + d.addCallback(lambda lbry_file: self._path_from_lbry_file(lbry_file) if lbry_file else 'Not found') + d.addErrback(lambda err: str(err)) + + return d + def main(): daemon = LBRYDaemon() diff --git a/lbrynet/lbrynet_daemon/LBRYDownloader.py b/lbrynet/lbrynet_daemon/LBRYDownloader.py index 15c5eafde..6895ded68 100644 --- a/lbrynet/lbrynet_daemon/LBRYDownloader.py +++ b/lbrynet/lbrynet_daemon/LBRYDownloader.py @@ -13,31 +13,23 @@ log = logging.getLogger(__name__) class GetStream(object): def __init__(self, sd_identifier, session, wallet, lbry_file_manager, max_key_fee, pay_key=True, data_rate=0.5): - self.finished_deferred = defer.Deferred(None) self.wallet = wallet self.resolved_name = None self.description = None self.key_fee = None self.key_fee_address = None + self.data_rate = data_rate + self.pay_key = pay_key self.name = None self.session = session self.payment_rate_manager = PaymentRateManager(self.session.base_payment_rate_manager) - self.loading_metadata_deferred = defer.Deferred() self.lbry_file_manager = lbry_file_manager self.sd_identifier = sd_identifier - self.metadata = None - self.loading_failed = False - self.resolved_name = None - self.description = None - self.key_fee = None - self.key_fee_address = None self.stream_hash = None self.max_key_fee = max_key_fee self.stream_info = None self.stream_info_manager = None - self.downloader = None - self.data_rate = data_rate - self.pay_key = pay_key + def start(self, stream_info): self.stream_info = stream_info @@ -52,18 +44,9 @@ class GetStream(object): else: self.key_fee = None self.key_fee_address = None - self.stream_hash = self.stream_info['stream_hash'] - elif 'stream_hash' in json.loads(self.stream_info['value']): - self.resolved_name = self.stream_info.get('name', None) - self.description = json.loads(self.stream_info['value']).get('description', None) - try: - if 'key_fee' in json.loads(self.stream_info['value']): - self.key_fee = float(json.loads(self.stream_info['value'])['key_fee']) - except ValueError: - self.key_fee = None - self.key_fee_address = json.loads(self.stream_info['value']).get('key_fee_address', None) - self.stream_hash = json.loads(self.stream_info['value'])['stream_hash'] + self.stream_hash = self.stream_info['stream_hash'] + else: print 'InvalidStreamInfoError' raise InvalidStreamInfoError(self.stream_info) @@ -72,71 +55,42 @@ class GetStream(object): if self.pay_key: print "Key fee (" + str(self.key_fee) + ") above limit of " + str( self.max_key_fee) + ", didn't download lbry://" + str(self.resolved_name) - return self.finished_deferred.callback(None) + return defer.fail(None) else: pass - def _get_downloader_for_return(): - return defer.succeed(self.downloader) + d = defer.Deferred(None) + d.addCallback(lambda _: download_sd_blob(self.session, self.stream_hash, self.payment_rate_manager)) + d.addCallback(self.sd_identifier.get_metadata_for_sd_blob) + d.addCallback(lambda metadata: + metadata.factories[1].make_downloader(metadata, [self.data_rate, True], self.payment_rate_manager)) + d.addErrback(lambda err: err.trap(defer.CancelledError)) + d.addErrback(lambda err: log.error("An exception occurred attempting to load the stream descriptor: %s", err.getTraceback())) + d.addCallback(self._start_download) + d.callback(None) + + return d + + 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()) + print 'Key fee: ' + str(self.key_fee) + ' | ' + str(self.key_fee_address) + return self.wallet.send_points_to_address(reserved_points, self.key_fee) + return defer.succeed(None) - self.loading_metadata_deferred = defer.Deferred(None) - self.loading_metadata_deferred.addCallback( - lambda _: download_sd_blob(self.session, self.stream_hash, self.payment_rate_manager)) - self.loading_metadata_deferred.addCallback(self.sd_identifier.get_metadata_for_sd_blob) - self.loading_metadata_deferred.addCallback(self._handle_metadata) - self.loading_metadata_deferred.addErrback(self._handle_load_canceled) - self.loading_metadata_deferred.addErrback(self._handle_load_failed) if self.pay_key: - self.loading_metadata_deferred.addCallback(lambda _: self._pay_key_fee()) - self.loading_metadata_deferred.addCallback(lambda _: self._make_downloader()) - self.loading_metadata_deferred.addCallback(lambda _: self.downloader.start()) - self.loading_metadata_deferred.addErrback(self._handle_download_error) - self.loading_metadata_deferred.addCallback(lambda _: _get_downloader_for_return()) - self.loading_metadata_deferred.callback(None) - - return defer.succeed(None) - - def _pay_key_fee(self): - 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()) - print 'Key fee: ' + str(self.key_fee) + ' | ' + str(self.key_fee_address) - return self.wallet.send_points_to_address(reserved_points, self.key_fee) - return defer.succeed(None) - - def _handle_load_canceled(self, err): - err.trap(defer.CancelledError) - self.finished_deferred.callback(None) - - def _handle_load_failed(self, err): - self.loading_failed = True - log.error("An exception occurred attempting to load the stream descriptor: %s", err.getTraceback()) - print 'Load Failed: ', err.getTraceback() - self.finished_deferred.callback(None) - - def _handle_metadata(self, metadata): - self.metadata = metadata - self.factory = self.metadata.factories[1] - return defer.succeed(None) - - def _handle_download_error(self, err): - if err.check(InsufficientFundsError): - print "Download stopped due to insufficient funds." + d = _pay_key_fee() else: - print "Autoaddstream: An unexpected error has caused the download to stop: ", err.getTraceback() + d = defer.Deferred() - def _make_downloader(self): + downloader.start() - def _set_downloader(downloader): - self.downloader = downloader - print "Downloading", self.stream_hash, "-->", os.path.join(self.downloader.download_directory, - self.downloader.file_name) - return self.downloader + print "Downloading", self.stream_hash, "-->", os.path.join(downloader.download_directory, downloader.file_name) - downloader = self.factory.make_downloader(self.metadata, [self.data_rate, True], self.payment_rate_manager) - downloader.addCallback(_set_downloader) - return downloader + return d class FetcherDaemon(object): From 6f4b2d79677c6287571c391c388bcca28cb38460 Mon Sep 17 00:00:00 2001 From: jackrobison Date: Tue, 19 Jan 2016 21:13:29 -0500 Subject: [PATCH 10/10] Delete x.sh --- x.sh | 3 --- 1 file changed, 3 deletions(-) delete mode 100755 x.sh diff --git a/x.sh b/x.sh deleted file mode 100755 index 1174512d8..000000000 --- a/x.sh +++ /dev/null @@ -1,3 +0,0 @@ -python setup.py install -clear -lbrynet-daemon