Update daemon and uri handler

This commit is contained in:
Jack 2016-01-16 23:06:24 -05:00
parent 2085012874
commit 885a0882f2
2 changed files with 109 additions and 15 deletions

View file

@ -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()

View file

@ -3,10 +3,12 @@ import json
import webbrowser
import xmlrpclib, sys
def render_video(path):
r = r'<center><video src="' + path + r'" controls width="960" height="720"></center>'
r = r'<center><video src="' + path + r'" controls autoplay width="960" height="720"></center>'
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 = "<body>" + json.dumps(r) + "</body>"
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("<body>" + json.dumps(r) + "</body>")
f.close()
webbrowser.open('file://' + os.path.join(os.getcwd(), 'lbry.html'))
webbrowser.open('file://' + str(path))
if __name__ == "__main__":
main(sys.argv[1:])