# -*-coding:utf-8-*-
from api import Api
import sys
import requests
import io
import os
import json

crop_id = sys.argv[1]
app_name = sys.argv[2]
privacy_url = sys.argv[3]
update_local = sys.argv[4]

error_url = privacy_url.replace("privacy.html","privacy_error.html")
oss_path=requests.get(error_url).text\
    .split("<Key>")[1]\
    .split("</Key>")[0]\
    .split("/")[1]

print("crop_id: %s, app_name: %s, osspath: %s"%(crop_id,app_name,oss_path))

api = Api()
success = api.new_privacy(str(crop_id), app_name, oss_path)


#是否更新本地文件
path = "../../../app/src/main/assets/web/".replace("/",os.path.sep)
if not os.path.exists(path):
    os.makedirs(path)
if success and update_local == "true":
    content = requests.get(privacy_url).text
    with io.open(path+"privacy.html", "w+", encoding="ISO-8859-1") as fout:
        fout.write(content)
        print("本地协议已更新！")
