ソースを参照

开放根据job_id下载并转存图片接口

TanJingyu 4 ヶ月 前
コミット
234e813ab7
3 ファイル変更56 行追加2 行削除
  1. 32 0
      api/mj_api.py
  2. 22 1
      app.py
  3. 2 1
      requirements.txt

+ 32 - 0
api/mj_api.py

@@ -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:

+ 22 - 1
app.py

@@ -3,6 +3,7 @@ from api import mj_api
 from api.error import unauthorized_error
 from api.error import system_error
 
+
 app = Flask(__name__)
 
 
@@ -75,6 +76,16 @@ def submit_job():
                                           user_id=request_data.get('user_id'), mode=request_data.get('mode'))
     return response_json_str
 
+@app.route('/get_image_urls', methods=['POST'])
+def get_image_urls():
+    request_data = request.get_json()
+
+    if request_data.get('job_id') is None:
+        return jsonify({"error": "job_id is required!"}), 400
+
+    urls = mj_api.upload_image_to_oss(job_id=request_data.get('job_id'), image_num=request_data.get('image_num'))
+    return urls
+
 
 @app.errorhandler(unauthorized_error)
 def handle_unauthorized_error(error):
@@ -89,6 +100,16 @@ def handle_system_error(error):
     response.status_code = 500
     return response
 
+@app.errorhandler(system_error)
+def handle_system_error(error):
+    response = jsonify({"error": str(error)})
+    response.status_code = 500
+    return response
+
+
+
+
+
 
 if __name__ == '__main__':
-    app.run()
+    app.run()

+ 2 - 1
requirements.txt

@@ -1,3 +1,4 @@
 curl_cffi~=0.7.3
 Flask~=3.0.3
-gunicorn~=20.1.0
+gunicorn~=20.1.0
+oss2~=2.19.1