upload.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. """
  2. @author: luojunhui
  3. """
  4. import json
  5. import os
  6. import oss2
  7. import requests
  8. from uuid import uuid4
  9. def process_obj(obj):
  10. """
  11. title, text, img_list, group_id
  12. :return:
  13. """
  14. group_id = str(uuid4()).replace("-", "")
  15. title = obj['title']
  16. text = obj['text']
  17. img_list = obj['img_list']
  18. cover_obj = obj['cover']
  19. temp = [
  20. {
  21. "inputValue": [
  22. cover_obj
  23. ],
  24. "fieldName": "cover",
  25. "fieldType": 1,
  26. "groupId": group_id
  27. },
  28. {
  29. "fieldName": "title",
  30. "fieldType": 0,
  31. "groupId": group_id,
  32. "inputValue": title
  33. },
  34. {
  35. "fieldName": "content",
  36. "fieldType": 0,
  37. "groupId": group_id,
  38. "inputValue": text
  39. },
  40. {
  41. "fieldName": "image",
  42. "fieldType": 1,
  43. "groupId": group_id,
  44. "inputValue": img_list
  45. },
  46. {
  47. "fieldName": "video",
  48. "fieldType": 2,
  49. "groupId": group_id
  50. },
  51. {
  52. "fieldName": "audio",
  53. "fieldType": 3,
  54. "groupId": group_id
  55. },
  56. {
  57. "fieldName": "tag",
  58. "fieldType": 0,
  59. "groupId": group_id
  60. }
  61. ]
  62. return temp
  63. def auto_upload_aigc(task_name, obj_list):
  64. """
  65. auto publish
  66. :return:
  67. """
  68. url = "http://aigc-api.cybertogether.net/aigc/crawler/plan/save"
  69. payload = json.dumps({
  70. "params": {
  71. "contentFilters": [],
  72. "accountFilters": [],
  73. "filterAccountMatchMode": 1,
  74. "filterContentMatchMode": 1,
  75. "selectModeValues": [],
  76. "imageSearchModeValues": [],
  77. "contentModal": 3,
  78. "analyze": {},
  79. "crawlerComment": 0,
  80. "inputGroup": [
  81. process_obj(obj) for obj in obj_list
  82. ],
  83. "inputSourceGroups": [],
  84. "modePublishTime": [],
  85. "name": task_name,
  86. "frequencyType": 3,
  87. "planType": 2
  88. },
  89. "baseInfo": {
  90. "token": "af54cdc404c3464d896745df389b2dce",
  91. "appType": 9,
  92. "platform": "pc",
  93. "appVersionCode": 1000,
  94. "clientTimestamp": 1,
  95. "fid": 1,
  96. "loginUid": 1,
  97. "pageSource": 1,
  98. "requestId": 1,
  99. "rid": 1,
  100. "uid": 1
  101. }
  102. })
  103. headers = {
  104. 'Accept': 'application/json',
  105. 'Accept-Language': 'en,zh;q=0.9,zh-CN;q=0.8',
  106. 'Content-Type': 'application/json',
  107. 'Origin': 'http://aigc-admin.cybertogether.net',
  108. 'Proxy-Connection': 'keep-alive',
  109. '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'
  110. }
  111. response = requests.request("POST", url, headers=headers, data=payload)
  112. return response.json()
  113. def upload_to_oss(local_path):
  114. """
  115. 上传到oss
  116. :return:
  117. """
  118. oss_video_key = "upload/" + str(uuid4()) + ".png"
  119. access_key_id = "LTAI5tCHXiNx4sUVPTSrk3zD"
  120. access_key_secret = "BoTrog6kA2dsmp70VrReKC7ZQHjQ0F"
  121. endpoint = "oss-ap-southeast-1.aliyuncs.com"
  122. bucket_name = "aigc-admin"
  123. bucket = oss2.Bucket(
  124. oss2.Auth(access_key_id, access_key_secret), endpoint, bucket_name
  125. )
  126. bucket.put_object_from_file(key=oss_video_key, filename=local_path)
  127. return oss_video_key
  128. def download_image(img_path, image_url):
  129. """
  130. 下载视频
  131. :param img_path:
  132. :param image_url:
  133. :return:
  134. """
  135. res = requests.get(image_url)
  136. os.makedirs(os.path.dirname(img_path), exist_ok=True)
  137. with open(img_path, "wb") as f:
  138. f.write(res.content)
  139. return img_path
  140. def download_and_upload(save_path, url):
  141. """
  142. Download images and upload to oss
  143. :return:
  144. """
  145. local_path = download_image(save_path, url)
  146. oss_key = upload_to_oss(local_path)
  147. img_obj = {
  148. "fileName": local_path.split("/")[-1],
  149. "ossKey": oss_key,
  150. "type": "image/png",
  151. "size": 1000
  152. }
  153. return img_obj