""" @author: luojunhui """ import json import os import oss2 import requests from uuid import uuid4 def process_obj(obj): """ title, text, img_list, group_id :return: """ group_id = str(uuid4()).replace("-", "") title = obj['title'] text = obj['text'] img_list = obj['img_list'] cover_obj = obj['cover'] temp = [ { "inputValue": [ cover_obj ], "fieldName": "cover", "fieldType": 1, "groupId": group_id }, { "fieldName": "title", "fieldType": 0, "groupId": group_id, "inputValue": title }, { "fieldName": "content", "fieldType": 0, "groupId": group_id, "inputValue": text }, { "fieldName": "image", "fieldType": 1, "groupId": group_id, "inputValue": img_list }, { "fieldName": "video", "fieldType": 2, "groupId": group_id }, { "fieldName": "audio", "fieldType": 3, "groupId": group_id }, { "fieldName": "tag", "fieldType": 0, "groupId": group_id } ] return temp def auto_upload_aigc(task_name, obj_list): """ auto publish :return: """ url = "http://aigc-api.cybertogether.net/aigc/crawler/plan/save" payload = json.dumps({ "params": { "contentFilters": [], "accountFilters": [], "filterAccountMatchMode": 1, "filterContentMatchMode": 1, "selectModeValues": [], "imageSearchModeValues": [], "contentModal": 3, "analyze": {}, "crawlerComment": 0, "inputGroup": [ process_obj(obj) for obj in obj_list ], "inputSourceGroups": [], "modePublishTime": [], "name": task_name, "frequencyType": 3, "planType": 2 }, "baseInfo": { "token": "af54cdc404c3464d896745df389b2dce", "appType": 9, "platform": "pc", "appVersionCode": 1000, "clientTimestamp": 1, "fid": 1, "loginUid": 1, "pageSource": 1, "requestId": 1, "rid": 1, "uid": 1 } }) headers = { 'Accept': 'application/json', 'Accept-Language': 'en,zh;q=0.9,zh-CN;q=0.8', 'Content-Type': 'application/json', 'Origin': 'http://aigc-admin.cybertogether.net', 'Proxy-Connection': 'keep-alive', 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/125.0.0.0 Safari/537.36' } response = requests.request("POST", url, headers=headers, data=payload) return response.json() def upload_to_oss(local_path): """ 上传到oss :return: """ oss_video_key = "upload/" + str(uuid4()) + ".png" access_key_id = "LTAI5tCHXiNx4sUVPTSrk3zD" access_key_secret = "BoTrog6kA2dsmp70VrReKC7ZQHjQ0F" endpoint = "oss-ap-southeast-1.aliyuncs.com" bucket_name = "aigc-admin" bucket = oss2.Bucket( oss2.Auth(access_key_id, access_key_secret), endpoint, bucket_name ) bucket.put_object_from_file(key=oss_video_key, filename=local_path) return oss_video_key def download_image(img_path, image_url): """ 下载视频 :param img_path: :param 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