|
@@ -1,6 +1,9 @@
|
|
|
from curl_cffi import requests
|
|
|
from api.error import unauthorized_error
|
|
|
from api.error import system_error
|
|
|
+import oss2
|
|
|
+import random
|
|
|
+import string
|
|
|
|
|
|
|
|
|
# 查询历史所有任务列表
|
|
@@ -46,6 +49,35 @@ def submit_job(cookie, prompt, user_id, mode):
|
|
|
response = requests.post(url=url, headers=nec_headers(cookie), impersonate="chrome101", json=request_data)
|
|
|
return response_catch(response)
|
|
|
|
|
|
+def generate_random_string(length=15):
|
|
|
+ characters = string.ascii_letters + string.digits
|
|
|
+ return ''.join(random.choice(characters) for _ in range(length))
|
|
|
+
|
|
|
+def upload_image_to_oss(job_id, image_num=4):
|
|
|
+ OSS_ACCESS_KEY_ID = 'LTAI5tEYvefc4U3fyU5du225'
|
|
|
+ OSS_ACCESS_KEY_SECRET = 'Z1gZtAGe8NwRtXgPzVgRMkRez4Ex4K'
|
|
|
+ OSS_ENDPOINT = 'oss-ap-southeast-1-internal.aliyuncs.com'
|
|
|
+ OSS_BUCKET_NAME = 'aigc-admin'
|
|
|
+
|
|
|
+ results = []
|
|
|
+ for i in range(image_num):
|
|
|
+ object_name = 'saas/mjImage/' + generate_random_string(32) + ".png"
|
|
|
+ # 下载网络图片
|
|
|
+ response = requests.get(f"https://cdn.midjourney.com/{job_id}/0_{i}.png",
|
|
|
+ headers=nec_headers(""),
|
|
|
+ impersonate="chrome101")
|
|
|
+ if response.ok:
|
|
|
+ # 连接OSS
|
|
|
+ auth = oss2.Auth(OSS_ACCESS_KEY_ID, OSS_ACCESS_KEY_SECRET)
|
|
|
+ bucket = oss2.Bucket(auth, OSS_ENDPOINT, OSS_BUCKET_NAME)
|
|
|
+ # 上传图片到OSS
|
|
|
+ bucket.put_object(object_name, response.content)
|
|
|
+ # 构造OSS文件URL
|
|
|
+ oss_url = f"https://res.aiddit.com/{object_name}"
|
|
|
+ results.append(oss_url)
|
|
|
+
|
|
|
+ return results
|
|
|
+
|
|
|
|
|
|
def response_catch(response):
|
|
|
if response.ok:
|