|
@@ -8,15 +8,49 @@
|
|
|
import json
|
|
|
import os
|
|
|
import time
|
|
|
+from urllib.parse import unquote
|
|
|
|
|
|
import lark_oapi as lark
|
|
|
+import tos
|
|
|
from lark_oapi.api.drive.v1 import CreateExportTaskRequest, ExportTask, CreateExportTaskResponse, GetExportTaskRequest, \
|
|
|
GetExportTaskResponse, DownloadExportTaskRequest, DownloadExportTaskResponse
|
|
|
+from tos import HttpMethodType
|
|
|
|
|
|
from config import BaseConfig
|
|
|
|
|
|
|
|
|
-def f_doc_export(token:str) -> str:
|
|
|
+def f_upload_file(save_path) -> str:
|
|
|
+ ak = BaseConfig.cos_access_key_id
|
|
|
+ sk = BaseConfig.cos_secret_access_key
|
|
|
+ endpoint = BaseConfig.endpoint
|
|
|
+ region = BaseConfig.region
|
|
|
+ bucket_name = BaseConfig.bucket_name
|
|
|
+
|
|
|
+ try:
|
|
|
+ # 创建 TosClientV2 对象,对桶和对象的操作都通过 TosClientV2 实现
|
|
|
+ client = tos.TosClientV2(ak, sk, endpoint, region)
|
|
|
+ object_key = os.path.basename(save_path)
|
|
|
+ client.put_object_from_file(bucket_name, object_key, save_path)
|
|
|
+ pre_signed_url_output = client.pre_signed_url(HttpMethodType.Http_Method_Get, bucket_name, object_key)
|
|
|
+ return pre_signed_url_output.signed_url
|
|
|
+
|
|
|
+ except tos.exceptions.TosClientError as e:
|
|
|
+ # 操作失败,捕获客户端异常,一般情况为非法请求参数或网络异常
|
|
|
+ print('fail with client error, message:{}, cause: {}'.format(e.message, e.cause))
|
|
|
+ except tos.exceptions.TosServerError as e:
|
|
|
+ # 操作失败,捕获服务端异常,可从返回信息中获取详细错误信息
|
|
|
+ print('fail with server error, code: {}'.format(e.code))
|
|
|
+ # request id 可定位具体问题,强烈建议日志中保存
|
|
|
+ print('error with request id: {}'.format(e.request_id))
|
|
|
+ print('error with message: {}'.format(e.message))
|
|
|
+ print('error with http code: {}'.format(e.status_code))
|
|
|
+ print('error with ec: {}'.format(e.ec))
|
|
|
+ print('error with request url: {}'.format(e.request_url))
|
|
|
+ except Exception as e:
|
|
|
+ print('fail with unknown error: {}'.format(e))
|
|
|
+
|
|
|
+
|
|
|
+def f_doc_export(token: str) -> str:
|
|
|
# 飞书在线文档转word
|
|
|
app_id = BaseConfig.app_id
|
|
|
app_secret = BaseConfig.app_secret
|
|
@@ -85,12 +119,17 @@ def f_doc_export(token:str) -> str:
|
|
|
return
|
|
|
|
|
|
# 处理业务结果
|
|
|
- save_path = os.path.join(word_save_dir, response3.file_name)
|
|
|
+
|
|
|
+ file_name = unquote(response3.file_name)
|
|
|
+ save_path = os.path.join(word_save_dir, file_name)
|
|
|
with open(save_path, "wb") as f:
|
|
|
f.write(response3.file.read())
|
|
|
|
|
|
- return save_path
|
|
|
+ word_download_url = f_upload_file(save_path)
|
|
|
+
|
|
|
+ return word_download_url
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
f_doc_export('YKNBdbs10oA3pCxTdnAczcvOnxc')
|
|
|
+ # f_upload_file("/root/project/coze_znjd/大模型企业调查报告/1.docx")
|