2016-03-03 06:16:51 +01:00
|
|
|
import os
|
|
|
|
import json
|
|
|
|
import webbrowser
|
2016-03-22 16:51:27 +01:00
|
|
|
import sys
|
|
|
|
from time import sleep
|
2016-03-24 00:49:14 +01:00
|
|
|
import subprocess
|
2016-03-03 06:16:51 +01:00
|
|
|
|
2016-03-22 16:51:27 +01:00
|
|
|
from jsonrpc.proxy import JSONRPCProxy
|
2016-03-24 00:49:14 +01:00
|
|
|
|
|
|
|
API_CONNECTION_STRING = "http://localhost:5279/lbryapi"
|
|
|
|
UI_ADDRESS = "http://localhost:5279"
|
2016-03-03 06:16:51 +01:00
|
|
|
|
|
|
|
|
2016-03-22 16:51:27 +01:00
|
|
|
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
|
|
|
|
2016-03-22 16:51:27 +01:00
|
|
|
def check_status(self):
|
2016-03-24 00:49:14 +01:00
|
|
|
status = None
|
2016-03-22 16:51:27 +01:00
|
|
|
try:
|
2016-03-24 00:49:14 +01:00
|
|
|
status = json.loads(self.daemon.is_running())['result']
|
|
|
|
if self.start_timeout < 30 and not status:
|
|
|
|
sleep(1)
|
|
|
|
self.start_timeout += 1
|
|
|
|
self.check_status()
|
|
|
|
elif status:
|
|
|
|
return True
|
2016-03-22 16:51:27 +01:00
|
|
|
else:
|
2016-03-24 00:49:14 +01:00
|
|
|
exit(1)
|
|
|
|
except:
|
|
|
|
if self.start_timeout < 30:
|
|
|
|
sleep(1)
|
2016-03-22 16:51:27 +01:00
|
|
|
self.start_timeout += 1
|
|
|
|
self.check_status()
|
2016-03-24 00:49:14 +01:00
|
|
|
else:
|
|
|
|
exit(1)
|
2016-03-03 06:16:51 +01:00
|
|
|
|
2016-03-22 16:51:27 +01:00
|
|
|
def handle(self, lbry_name):
|
2016-03-24 00:49:14 +01:00
|
|
|
lbry_process = [d for d in subprocess.Popen(['ps','aux'], stdout=subprocess.PIPE).stdout.readlines()
|
|
|
|
if 'LBRY.app' in d]
|
|
|
|
try:
|
|
|
|
status = json.loads(self.daemon.is_running())['result']
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
|
|
|
|
if lbry_process:
|
|
|
|
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
|
|
|
|
2016-03-24 00:49:14 +01:00
|
|
|
if lbry_name == "lbry" or lbry_name == "" and not started:
|
2016-03-22 16:51:27 +01:00
|
|
|
webbrowser.get('safari').open(UI_ADDRESS)
|
2016-03-03 06:16:51 +01:00
|
|
|
else:
|
2016-03-22 16:51:27 +01:00
|
|
|
r = json.loads(self.daemon.get({'name': lbry_name}))
|
|
|
|
if r['code'] == 200:
|
|
|
|
path = r['result']['path'].encode('utf-8')
|
|
|
|
extension = os.path.splitext(path)[1]
|
|
|
|
if extension in ['mp4', 'flv', 'mov', 'ogv']:
|
|
|
|
webbrowser.get('safari').open(UI_ADDRESS + "/view?name=" + lbry_name)
|
2016-03-08 18:15:49 +01:00
|
|
|
else:
|
2016-03-22 16:51:27 +01:00
|
|
|
webbrowser.get('safari').open('file://' + path)
|
2016-03-03 06:16:51 +01:00
|
|
|
else:
|
2016-03-08 18:15:49 +01:00
|
|
|
webbrowser.get('safari').open('http://lbry.io/get')
|
2016-03-03 06:16:51 +01:00
|
|
|
|
2016-03-22 16:51:27 +01:00
|
|
|
|
|
|
|
def main(args):
|
|
|
|
if len(args) != 1:
|
|
|
|
args = ['lbry://lbry']
|
|
|
|
|
|
|
|
name = args[0][7:]
|
|
|
|
LBRYURIHandler().handle(lbry_name=name)
|
2016-03-03 06:16:51 +01:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main(sys.argv[1:])
|