lbry-sdk/lbrynet/lbrynet_daemon/Apps/LBRYURIHandler.py

95 lines
2.6 KiB
Python
Raw Normal View History

2016-03-03 06:16:51 +01:00
import os
import json
import webbrowser
import subprocess
2016-03-25 01:46:24 +01:00
import sys
2016-03-03 06:16:51 +01:00
2016-03-25 01:46:24 +01:00
from time import sleep
from jsonrpc.proxy import JSONRPCProxy
API_CONNECTION_STRING = "http://localhost:5279/lbryapi"
UI_ADDRESS = "http://localhost:5279"
2016-03-03 06:16:51 +01:00
2016-03-25 01:46:24 +01:00
class Timeout(Exception):
def __init__(self, value):
self.parameter = value
def __str__(self):
return repr(self.parameter)
class LBRYURIHandler(object):
def __init__(self):
self.started_daemon = False
self.start_timeout = 0
self.daemon = JSONRPCProxy.from_url(API_CONNECTION_STRING)
2016-03-03 06:16:51 +01:00
def check_status(self):
status = None
try:
status = self.daemon.is_running()
if self.start_timeout < 30 and not status:
sleep(1)
self.start_timeout += 1
self.check_status()
elif status:
return True
else:
2016-03-25 01:46:24 +01:00
raise Timeout("LBRY daemon is running, but connection timed out")
except:
if self.start_timeout < 30:
sleep(1)
self.start_timeout += 1
self.check_status()
else:
2016-03-25 01:46:24 +01:00
raise Timeout("Timed out trying to start LBRY daemon")
2016-03-03 06:16:51 +01:00
def handle_osx(self, lbry_name):
lbry_process = [d for d in subprocess.Popen(['ps','aux'], stdout=subprocess.PIPE).stdout.readlines()
2016-03-25 01:46:24 +01:00
if 'LBRY.app' in d and 'LBRYURIHandler' not in d]
try:
status = self.daemon.is_running()
except:
status = None
if lbry_process or status:
self.check_status()
started = False
else:
os.system("open /Applications/LBRY.app")
self.check_status()
started = True
2016-03-03 06:16:51 +01:00
if lbry_name == "lbry" or lbry_name == "" and not started:
webbrowser.open(UI_ADDRESS)
2016-03-03 06:16:51 +01:00
else:
webbrowser.open(UI_ADDRESS + "/view?name=" + lbry_name)
def handle_linux(self, lbry_name):
try:
is_running = self.daemon.is_running()
if not is_running:
sys.exit(0)
except:
sys.exit(0)
if lbry_name == "lbry":
webbrowser.open(UI_ADDRESS)
else:
webbrowser.open(UI_ADDRESS + "/view?name=" + lbry_name)
def main(args):
if len(args) != 1:
args = ['lbry://lbry']
name = args[0][7:]
if sys.platform == "darwin":
LBRYURIHandler().handle_osx(lbry_name=name)
else:
LBRYURIHandler().handle_linux(lbry_name=name)
2016-03-03 06:16:51 +01:00
if __name__ == "__main__":
main(sys.argv[1:])