# filename: check_status.py
import hashlib
import email.utils
import http.client
import json

def get_download_url(task_id):
    app_id = 'SX20240723LAKILA'
    app_key = 'mIwDAgJZIZEUsOZatRrCvhtMkaxGdWbq'
    get_uri = f"/api/developer/v1/tasks/convert/to/docx/{task_id}"
    current_time = email.utils.formatdate(usegmt=True)
    content_md5 = hashlib.md5(get_uri.encode('utf-8')).hexdigest()
    data = app_key + content_md5 + "application/json" + current_time
    sha1_hex = hashlib.sha1(data.encode('utf-8')).hexdigest()
    authorization_header = f"WPS-2:{app_id}:{sha1_hex}"
    conn = http.client.HTTPSConnection("solution.wps.cn")
    headers = {
        'Date': current_time,
        'Content-Md5': content_md5,
        'Content-Type': "application/json",
        'Authorization': authorization_header
    }
    conn.request("GET", get_uri, headers=headers)
    res = conn.getresponse()
    data = res.read()
    response_json = json.loads(data.decode("utf-8"))
    return response_json['data']['download_url']



