lbry-sdk/packaging/uri_handler/LBRYURIHandler.py

91 lines
2.8 KiB
Python
Raw Normal View History

2016-08-27 07:32:28 +02:00
import os
import webbrowser
import subprocess
import sys
from time import sleep
2016-09-22 03:55:10 +02:00
from lbrynet.lbrynet_daemon.auth.client import LBRYAPIClient
from lbrynet import conf
2016-08-27 07:32:28 +02:00
class LBRYURIHandler(object):
def __init__(self):
self.started_daemon = False
2017-01-03 20:13:01 +01:00
self.daemon = LBRYAPIClient.get_client()
2016-08-27 07:32:28 +02:00
def handle_osx(self, lbry_name):
self.check_daemon()
if not self.started_daemon:
os.system("open /Applications/LBRY.app")
sleep(3)
lbry_name = self.parse_name(lbry_name)
self.open_address(lbry_name)
def handle_linux(self, lbry_name):
self.check_daemon()
if not self.started_daemon:
cmd = r'DIR = "$( cd "$(dirname "${BASH_SOURCE[0]}" )" && pwd )"' \
r'if [-z "$(pgrep lbrynet-daemon)"]; then' \
2017-01-04 23:10:36 +01:00
r'echo "running lbrynet-daemon"' \
2016-08-27 07:32:28 +02:00
r'$DIR / lbrynet - daemon &' \
r'sleep 3 # let the daemon load before connecting' \
r'fi'
subprocess.Popen(cmd, shell=True)
lbry_name = self.parse_name(lbry_name)
self.open_address(lbry_name)
def handle_win32(self, lbry_name):
# Opening LBRY.exe with lbry_name as arg prevents the need to
# make a separate call to open_address()
self.check_daemon()
lbry_name = self.parse_name(lbry_name)
if self.started_daemon:
self.open_address(lbry_name)
else:
lbry_path = os.path.join(os.environ["PROGRAMFILES"], "LBRY", "LBRY.exe ")
subprocess.call(lbry_path + lbry_name)
def check_daemon(self):
try:
2017-01-09 20:03:25 +01:00
status = self.daemon.call('status')
self.started_daemon = status['is_running']
2016-08-27 07:32:28 +02:00
except:
self.started_daemon = False
@staticmethod
def parse_name(lbry_name):
if lbry_name[:7].lower() == "lbry://":
if lbry_name[-1] == "/":
return lbry_name[7:-1]
else:
return lbry_name[7:]
else:
if lbry_name[-1] == "/":
return lbry_name[:-1]
else:
return lbry_name[:]
@staticmethod
def open_address(lbry_name):
if lbry_name == "lbry" or lbry_name == "" or lbry_name is None:
2017-01-17 04:23:20 +01:00
webbrowser.open(conf.settings.get_ui_address())
2016-08-27 07:32:28 +02:00
else:
2017-01-17 04:23:20 +01:00
webbrowser.open(conf.settings.get_ui_address() + "/?show=" + lbry_name)
2016-08-27 07:32:28 +02:00
def main(args):
if len(args) != 1:
args = ["lbry://lbry"]
name = args[0][7:]
if sys.platform == "darwin":
LBRYURIHandler().handle_osx(lbry_name=name)
elif os.name == "nt":
LBRYURIHandler().handle_win32(lbry_name=name)
else:
LBRYURIHandler().handle_linux(lbry_name=name)
if __name__ == "__main__":
main(sys.argv[1:])