#!/usr/bin/python3

import os
import sys
import struct
import json
import logging


# log what happen in background for debug your app
log = logging.getLogger("default")
handler = logging.FileHandler("/tmp/luanch_local_app.log")
formatter = logging.Formatter("%(asctime)s %(levelname)s %(message)s")
handler.setFormatter(formatter)
log.addHandler(handler)
log.setLevel(logging.DEBUG)

# get length of msg
length_bytes = sys.stdin.buffer.read(4)
if len(length_bytes) == 0:
    log.fatal("Err: fail to get msg length")
    quit(0)

# get msg
length = struct.unpack("i", length_bytes)[0]
text_json = sys.stdin.buffer.read(length).decode("utf-8")
log.debug("msg: %s" % text_json)
url = json.loads(text_json).get("text")

# start the target app
run_cmd = "you-get -p deepin-movie --debug %s" % url
log.debug("run: %s" % run_cmd)

# luanch local app
os.system(run_cmd)
