#!/usr/bin/env python
#-*- coding: utf-8 -*-

import urllib.request
import json
import sys

def get_app_version():
    return sys.argv[1]
    pass

def get_app_name():
    return sys.argv[2]
    pass

def get_app_env():
    return sys.argv[3]
    pass

def get_dd_icon():
    return sys.argv[4]
    pass

def get_dd_url():
    return sys.argv[5]
    pass
    
def get_dd_host():
    return sys.argv[6]
    pass

def get_pgy_pwd():
    return sys.argv[7]
    pass

def get_request_params_str():
    
    app_env = get_app_env()

    app_pwd = get_pgy_pwd()

    message = app_env + " 已更新" + " 密码:" + app_pwd

    app_version = get_app_version()

    app_name = get_app_name()

    title = "Android " + app_name + " " + app_env + " " + app_version

    dd_icon = "https://appicon.pgyer.com/image/view/app_icons/" + get_dd_icon()

    dd_url = "https://www.pgyer.com/" + get_dd_url()

    params = {
        "msgtype": "link",
            "link": {
            "title": title,
            "text": message,
            "picUrl": dd_icon,
            "messageUrl": dd_url
        }
    }
    return json.dumps(params)  #python obj to json(str)
    pass

def send_ding_talk_request():
    host_url = "https://oapi.dingtalk.com/robot/send?access_token=" + get_dd_host()
    req = urllib.request.Request(host_url)
    req.add_header('Content-Type', 'application/json')
    
    params_str = get_request_params_str()
    # print(params_str)

    print(host_url)
    
    response = urllib.request.urlopen(req, params_str.encode('utf-8'))
    print(response.read().decode('utf-8'))
    pass

send_ding_talk_request()
