upload.py 3.9 KB

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