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

def submit_conversion_task(file_url):
    app_id = 'SX20240723LAKILA'
    app_key = 'mIwDAgJZIZEUsOZatRrCvhtMkaxGdWbq'
    current_time = email.utils.formatdate(usegmt=True)
    payload = json.dumps({"url": file_url})
    md5_hasher = hashlib.md5()
    md5_hasher.update(payload.encode('utf-8'))
    content_md5 = md5_hasher.hexdigest()
    content_type = "application/json"
    signing_string = app_key + content_md5 + content_type + current_time
    hasher = hashlib.sha1()
    hasher.update(signing_string.encode('utf-8'))
    signature = hasher.hexdigest()
    authorization_header = f"WPS-2:{app_id}:{signature}"
    conn = http.client.HTTPSConnection("solution.wps.cn")
    headers = {
        'Date': current_time,
        'Content-Md5': content_md5,
        'Content-Type': content_type,
        'Authorization': authorization_header
    }
    conn.request("POST", "/api/developer/v1/office/pdf/convert/to/docx", payload, headers)
    res = conn.getresponse()
    data = res.read()
    response_json = json.loads(data.decode("utf-8"))
    return response_json['data']['task_id']

if __name__ == "__main__":
    file_url="C:\\Users\\Administrator\\Desktop\\招标文件\\output1\\zbfile.pdf"
    submit_conversion_task(file_url)

