Explorar o código

v0.4 新服务上线

罗俊辉 hai 10 meses
pai
achega
fdcfeaf363
Modificáronse 5 ficheiros con 74 adicións e 42 borrados
  1. 21 6
      applications/upload.py
  2. 36 23
      deal/publish_deal.py
  3. 10 8
      requirements.txt
  4. 4 2
      test/upload_dev.py
  5. 3 3
      test/videos_dev.py

+ 21 - 6
applications/upload.py

@@ -2,6 +2,8 @@
 @author: luojunhui
 """
 import json
+import os
+
 import oss2
 
 import requests
@@ -17,15 +19,11 @@ def process_obj(obj):
     title = obj['title']
     text = obj['text']
     img_list = obj['img_list']
+    cover_obj = obj['cover']
     temp = [
         {
             "inputValue": [
-                {
-                    "fileName": "pqzf.png",
-                    "ossKey": "upload/03bf695277827c2387133a1ac9290fd2.png",
-                    "type": "image/png",
-                    "size": 2978
-                }
+                cover_obj
             ],
             "fieldName": "cover",
             "fieldType": 1,
@@ -146,6 +144,23 @@ def download_image(img_path, image_url):
     :return:
     """
     res = requests.get(image_url)
+    os.makedirs(os.path.dirname(img_path), exist_ok=True)
     with open(img_path, "wb") as f:
         f.write(res.content)
     return img_path
+
+
+def download_and_upload(save_path, url):
+    """
+    Download images and upload to oss
+    :return:
+    """
+    local_path = download_image(save_path, url)
+    oss_key = upload_to_oss(local_path)
+    img_obj = {
+        "fileName": local_path.split("/")[-1],
+        "ossKey": oss_key,
+        "type": "image/png",
+        "size": 1000
+    }
+    return img_obj

+ 36 - 23
deal/publish_deal.py

@@ -1,7 +1,7 @@
 """
 @author: luojunhui
 """
-from applications.upload import *
+from applications.upload import download_and_upload, auto_upload_aigc
 from applications.functions import hash_title
 
 
@@ -9,11 +9,10 @@ class PublishDeal(object):
     """
     自动发布接口处理
     """
+
     def __init__(self, params):
-        self.video_id = None
-        self.img_list = None
-        self.text = None
-        self.title = None
+        self.process_list = []
+        self.taskName = None
         self.params = params
 
     def check_params(self):
@@ -21,10 +20,17 @@ class PublishDeal(object):
         check params
         """
         try:
-            self.title = self.params['title']
-            self.text = self.params['text']
-            self.img_list = self.params['img_list']
-            self.video_id = self.params['video_id']
+            self.taskName = self.params['task_list']
+            obj_list = self.params['article_list']
+            self.process_list = [
+                {
+                    "title": obj['title'],
+                    "text": obj['text'],
+                    "img_list": obj['img_list'],
+                    "video_id": obj['video_id'],
+                    "cover": obj['cover']
+                } for obj in obj_list
+            ]
             return None
         except Exception as e:
             res = {
@@ -41,19 +47,26 @@ class PublishDeal(object):
         if params_error:
             return params_error
         else:
-            h_title = hash_title(self.title)
-            img_list = []
-            for index, url in enumerate(self.img_list, 1):
-                save_path = "temp/{}-{}.png".format(h_title, index)
-                local_p = download_image(save_path, url)
-                oss_key = upload_to_oss(local_p)
-                img_obj = {
-                    "fileName": save_path.replace("temo/", ""),
-                    "ossKey": oss_key,
-                    "type": "image/png",
-                    "size": 1234
+            L = []
+            for article_obj in self.process_list:
+                # hash title
+                h_title = hash_title(article_obj['title'])
+                # process imgList
+                img_list = []
+                for index, url in enumerate(article_obj['img_list'], 1):
+                    save_path = "temp/{}-{}.png".format(h_title, index)
+                    img_obj = download_and_upload(save_path, url)
+                    img_list.append(img_obj)
+
+                # process Cover
+                cover_path = "temp/cover-{}.png".format(h_title)
+                cover_obj = download_and_upload(cover_path, article_obj['cover'])
+                result_obj = {
+                    "title": article_obj['title'] + "video_id={}".format(article_obj['video_id']),
+                    "text": article_obj['text'],
+                    "img_list": img_list,
+                    "cover": cover_obj
                 }
-                img_list.append(img_obj)
-            res = auto_upload_aigc(title="{}video_id={}".format(self.title, self.video_id), text=self.text, img_list=self.img_list)
+                L.append(result_obj)
+            res = auto_upload_aigc(task_name=self.taskName, obj_list=L)
             return res
-

+ 10 - 8
requirements.txt

@@ -1,10 +1,12 @@
-quart
+quart~=0.19.5
 aiohttp
-aiomysql
-requests
-pymysql
-oss2
-lxml
+aiomysql~=0.2.0
+requests~=2.31.0
+pymysql~=1.1.0
+oss2~=2.18.4
+lxml~=5.2.1
 fake_useragent
-tqdm
-hypercorn
+tqdm~=4.66.2
+hypercorn
+openai~=1.21.2
+odps~=3.5.1

+ 4 - 2
test/upload_dev.py

@@ -3,6 +3,7 @@
 @author: luojunhui
 """
 import hashlib
+import json
 import os
 
 from applications.upload import download_image, upload_to_oss, auto_upload_aigc
@@ -77,7 +78,8 @@ def auto_upload(obj):
         "text": text,
         "img_list": img_list
     }
-    auto_upload_aigc("俊辉-测试自动发布5篇内容", [obj, obj, obj, obj, obj])
+    return auto_upload_aigc("俊辉-测试自动发布5篇内容-0603", [obj, obj, obj, obj, obj])
 
 
-auto_upload(test_obj)
+w = auto_upload(test_obj)
+print(json.dumps(w, ensure_ascii=False, indent=4))

+ 3 - 3
test/videos_dev.py

@@ -15,7 +15,7 @@ body = {
     "cate": "video_return",
     "start_date": "2024-05-28",
     "end_date": "2024-05-29",
-    "topN": 20
+    "topN": 5
 }
 a = time.time()
 header = {
@@ -27,5 +27,5 @@ b = time.time()
 print(b - a)
 print(json.dumps(response.json(), ensure_ascii=False, indent=4))
 
-with open("test_return.json", "w", encoding="utf-8") as f:
-    f.write(json.dumps(response.json(), ensure_ascii=False, indent=4))
+# with open("test_return.json", "w", encoding="utf-8") as f:
+#     f.write(json.dumps(response.json(), ensure_ascii=False, indent=4))