video_processor.py 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562
  1. import configparser
  2. import json
  3. import os
  4. import random
  5. import re
  6. import time
  7. from datetime import datetime
  8. from common.redis import get_data, get_first_value_with_prefix, increment_key
  9. from common.tts_help import TTS
  10. from common import Material, Feishu, Common, Oss, AliyunLogger
  11. from common.ffmpeg import FFmpeg
  12. from common.gpt4o_help import GPT4o
  13. from data_channel.douyin import DY
  14. from data_channel.dy_keyword import DyKeyword
  15. from data_channel.dy_ls import DYLS
  16. from data_channel.ks_ls import KSLS
  17. from data_channel.kuaishou import KS
  18. from data_channel.kuaishouchuangzuozhe import KsFeedVideo
  19. from data_channel.piaoquan import PQ
  20. from common.sql_help import sqlCollect
  21. from data_channel.shipinhao import SPH
  22. # 读取配置文件
  23. from data_channel.shipinhaodandian import SPHDD
  24. from data_channel.sph_ls import SPHLS
  25. config = configparser.ConfigParser()
  26. config.read('./config.ini')
  27. class VideoProcessor:
  28. """
  29. 视频处理类,包含创建文件夹、生成随机ID、删除文件和处理视频任务等方法。
  30. """
  31. @classmethod
  32. def create_folders(cls, mark):
  33. """
  34. 根据标示和任务标示创建目录
  35. """
  36. id = cls.random_id()
  37. video_path_url = config['PATHS']['VIDEO_PATH'] + mark + "/" + str(id) + "/"
  38. if not os.path.exists(video_path_url):
  39. os.makedirs(video_path_url)
  40. return video_path_url
  41. @classmethod
  42. def random_id(cls):
  43. """
  44. 随机生成ID
  45. """
  46. now = datetime.now()
  47. rand_num = random.randint(10000, 99999)
  48. return f"{now.strftime('%Y%m%d%H%M%S')}{rand_num}"
  49. @classmethod
  50. def remove_files(cls, video_path_url):
  51. """
  52. 删除指定目录下的所有文件和子目录
  53. """
  54. if os.path.exists(video_path_url) and os.path.isdir(video_path_url):
  55. for root, dirs, files in os.walk(video_path_url):
  56. for file in files:
  57. file_path = os.path.join(root, file)
  58. os.remove(file_path)
  59. for dir in dirs:
  60. dir_path = os.path.join(root, dir)
  61. os.rmdir(dir_path)
  62. @classmethod
  63. def process_task(cls, task, mark, name, feishu_id, cookie_sheet):
  64. """
  65. 处理单个任务
  66. """
  67. task_mark = task["task_mark"]
  68. channel_id = str(task["channel_id"])
  69. url = str(task["channel_url"])
  70. piaoquan_id = str(task["piaoquan_id"])
  71. number = task["number"]
  72. title = task["title"]
  73. video_share = task["video_share"]
  74. video_ending = task["video_ending"]
  75. crop_total = task["crop_total"]
  76. gg_duration_total = task["gg_duration_total"]
  77. voice = task['voice']
  78. if voice:
  79. if ',' in voice:
  80. voices = voice.split(',')
  81. else:
  82. voices = [voice]
  83. voice = random.choice(voices)
  84. else:
  85. voice = "zhifeng_emo"
  86. video_path_url = cls.create_folders(mark)
  87. zm = Material.get_pzsrt_data("summary", "500Oe0", video_share)
  88. Common.logger(mark).info(f"{name}的{task_mark}下{channel_id}的用户:{url}开始获取视频")
  89. data_list = cls.get_data_list(channel_id, task_mark, url, number, mark, feishu_id, cookie_sheet, name, task)
  90. if not data_list:
  91. AliyunLogger.logging(channel_id, name, url, "", "无改造视频", "4000")
  92. Common.logger(mark).info(f"{name}的{task_mark}下{channel_id}的视频ID{url} 已经改造过了")
  93. text = (
  94. f"**通知类型**: 没有改造的视频\n"
  95. f"**负责人**: {name}\n"
  96. f"**渠道**: {channel_id}\n"
  97. f"**视频主页ID**: {url}\n"
  98. )
  99. Feishu.finish_bot(text, "https://open.feishu.cn/open-apis/bot/v2/hook/e7697dc6-5254-4411-8b59-3cd0742bf703",
  100. "【 机器改造通知 】")
  101. cls.remove_files(video_path_url)
  102. return
  103. Common.logger(mark).info(f"{name}的{task_mark}下的ID{url} 获取视频完成,共{len(data_list)}条")
  104. for video in data_list:
  105. try:
  106. new_title = cls.generate_title(video, title)
  107. v_id = video["video_id"]
  108. cover = video["cover"]
  109. video_url = video["video_url"]
  110. old_title = video['old_title']
  111. rule = video['rule']
  112. # if channel_id == "单点视频":
  113. # redis_video = get_redis_video_data(v_id)
  114. # if redis_video:
  115. # continue
  116. if not old_title:
  117. old_title = '这个视频,分享给我的老友,祝愿您能幸福安康'
  118. text = (
  119. f"**通知类型**: 标题为空,使用兜底标题生成片尾\n"
  120. f"**负责人**: {name}\n"
  121. f"**渠道**: {channel_id}\n"
  122. f"**视频主页ID**: {url}\n"
  123. f"**视频Video_id**: {v_id}\n"
  124. )
  125. Feishu.finish_bot(text,
  126. "https://open.feishu.cn/open-apis/bot/v2/hook/e7697dc6-5254-4411-8b59-3cd0742bf703",
  127. "【 机器改造通知 】")
  128. Common.logger(mark).info(f"{name}的{task_mark}下的视频{url},标题为空,使用兜底标题生成片尾")
  129. time.sleep(1)
  130. pw_random_id = cls.random_id()
  131. Common.logger(mark).info(f"{name}的{task_mark}下的ID{url} 开始下载视频")
  132. new_video_path = cls.download_and_process_video(channel_id, video_url, video_path_url, v_id,
  133. crop_total, gg_duration_total, pw_random_id, new_title, mark, video)
  134. if not os.path.isfile(new_video_path) or new_video_path == None:
  135. AliyunLogger.logging(channel_id, name, url, v_id, "视频下载失败", "3002", f"video_url:{video_url}")
  136. text = (
  137. f"**通知类型**: 视频下载失败\n"
  138. f"**负责人**: {name}\n"
  139. f"**渠道**: {channel_id}\n"
  140. f"**视频主页ID**: {url}\n"
  141. f"**视频Video_id**: {v_id}\n"
  142. )
  143. Feishu.finish_bot(text,
  144. "https://open.feishu.cn/open-apis/bot/v2/hook/e7697dc6-5254-4411-8b59-3cd0742bf703",
  145. "【 机器改造通知 】")
  146. cls.remove_files(video_path_url)
  147. continue
  148. if new_video_path:
  149. if video_ending and video_ending != 'None':
  150. new_video_path = cls.handle_video_ending(new_video_path, video_ending, old_title, pw_random_id, video_path_url, mark, task_mark, url, name, video_share, zm, voice)
  151. if new_video_path == None:
  152. cls.remove_files(video_path_url)
  153. continue
  154. else:
  155. if video_share and video_share != 'None':
  156. new_video_path = FFmpeg.single_video(new_video_path, video_path_url, zm)
  157. # new_video_path = FFmpeg.single_video(new_video_path, video_path_url, zm)
  158. if not os.path.isfile(new_video_path):
  159. log_data = f"user:{url},,video_id:{v_id},,video_url:{video_url},,ai_title:{new_title}"
  160. AliyunLogger.logging(channel_id, name, url, v_id, "视频改造失败", "3001", log_data)
  161. text = (
  162. f"**通知类型**: 视频改造失败\n"
  163. f"**负责人**: {name}\n"
  164. f"**渠道**: {channel_id}\n"
  165. f"**视频主页ID**: {url}\n"
  166. f"**视频Video_id**: {v_id}\n"
  167. )
  168. Feishu.finish_bot(text,
  169. "https://open.feishu.cn/open-apis/bot/v2/hook/e7697dc6-5254-4411-8b59-3cd0742bf703",
  170. "【 机器改造通知 】")
  171. cls.remove_files(video_path_url)
  172. continue
  173. # 上传视频和封面,并更新数据库
  174. code = cls.upload_video_and_thumbnail(new_video_path, cover, v_id, new_title, task_mark, name, piaoquan_id,
  175. video_path_url, mark, channel_id, url, old_title, title, rule)
  176. # 更新已使用的视频号状态
  177. pq_url = f'https://admin.piaoquantv.com/cms/post-detail/{code}/detail' # 站内视频链接
  178. if name == "单点视频":
  179. sphdd_status = sqlCollect.update_shp_dd_vid(v_id)
  180. if sphdd_status == 1:
  181. Common.logger(mark).info(f"{name}的{task_mark}下的ID{url} 视频修改已使用,状态已修改")
  182. from_user_name = video['from_user_name'] # 来源用户
  183. from_group_name = video['from_group_name'] # 来源群组
  184. source = video['source'] # 渠道
  185. channel_id = source
  186. text = (
  187. f"**站内视频链接**: {pq_url}\n"
  188. f"**渠道**: {source}\n"
  189. f"**来源用户**: {from_user_name}\n"
  190. f"**来源群组**: {from_group_name}\n"
  191. f"**原视频链接**: {video['video_url']}\n"
  192. f"**原视频封面**: {video['cover']}\n"
  193. f"**原视频标题**: {video['old_title']}\n"
  194. )
  195. Feishu.finish_bot(text, "https://open.feishu.cn/open-apis/bot/v2/hook/493b3d4c-5fae-4a9d-980b-1dd86636524e", "【 有一条新的内容改造成功 】")
  196. text = (
  197. f"**通知类型**: 视频改造成功\n"
  198. f"**站内视频链接**: {pq_url}\n"
  199. f"**负责人**: {name}\n"
  200. f"**渠道**: {channel_id}\n"
  201. f"**视频主页ID**: {url}\n"
  202. f"**视频Video_id**: {v_id}\n"
  203. f"**使用音频音色**: {voice}\n"
  204. )
  205. Feishu.finish_bot(text,
  206. "https://open.feishu.cn/open-apis/bot/v2/hook/e7697dc6-5254-4411-8b59-3cd0742bf703",
  207. "【 机器改造通知 】")
  208. if channel_id == "快手历史" or channel_id == "抖音历史" or channel_id == "视频号历史":
  209. explain = "历史爆款"
  210. else:
  211. explain = "新供给"
  212. current_time = datetime.now()
  213. formatted_time = current_time.strftime("%Y-%m-%d %H:%M:%S")
  214. if name == "品类关键词搜索":
  215. first_category = task["first_category"]
  216. secondary_category = task["secondary_category"]
  217. keyword_principal = task["keyword_name"]
  218. log_data = f"user:{url},,video_id:{v_id},,video_url:{video_url},,ai_title:{new_title},,voice:{voice},,first_category:{first_category},,secondary_category:{secondary_category},,keyword_principal:{keyword_principal}"
  219. AliyunLogger.logging(channel_id, name, url, v_id, "视频改造成功", "1000", log_data, str(code))
  220. values = [
  221. [
  222. name,
  223. task_mark,
  224. channel_id,
  225. url,
  226. str(v_id),
  227. piaoquan_id,
  228. old_title,
  229. title if title in ["原标题", "AI标题"] else "",
  230. new_title,
  231. str(code),
  232. formatted_time,
  233. str(rule),
  234. explain,
  235. voice,
  236. first_category,
  237. secondary_category,
  238. keyword_principal,
  239. pq_url
  240. ]
  241. ]
  242. else:
  243. log_data = f"user:{url},,video_id:{v_id},,video_url:{video_url},,ai_title:{new_title},,voice:{voice}"
  244. AliyunLogger.logging(channel_id, name, url, v_id, "视频改造成功", "1000", log_data, str(code))
  245. values = [
  246. [
  247. name,
  248. task_mark,
  249. channel_id,
  250. url,
  251. str(v_id),
  252. piaoquan_id,
  253. old_title,
  254. title if title in ["原标题", "AI标题"] else "",
  255. new_title,
  256. str(code),
  257. formatted_time,
  258. str(rule),
  259. explain,
  260. voice
  261. ]
  262. ]
  263. if values:
  264. if name == "王雪珂":
  265. sheet = "vfhHwj"
  266. elif name == "抖音品类账号-1":
  267. sheet = "61kvW7"
  268. elif name == "鲁涛":
  269. sheet = "FhewlS"
  270. elif name == "范军":
  271. sheet = "B6dCfS"
  272. elif name == "余海涛":
  273. sheet = "mfBrNT"
  274. elif name == "罗情":
  275. sheet = "2J3PwN"
  276. elif name == "王玉婷":
  277. sheet = "bBHFwC"
  278. elif name == "刘诗雨":
  279. sheet = "fBdxIQ"
  280. elif name == "信欣":
  281. sheet = "lPe1eT"
  282. elif name == "快手创作者版品类推荐流":
  283. sheet = "k7l7nQ"
  284. elif name == "抖音品类账号":
  285. sheet = "Bsg5UR"
  286. elif name == "视频号品类账号":
  287. sheet = "b0uLWw"
  288. elif name == "单点视频":
  289. sheet = "ptgCXW"
  290. elif name == "快手品类账号":
  291. sheet = "ibjoMx"
  292. elif name == "品类关键词搜索":
  293. sheet = "Tgpikc"
  294. Feishu.insert_columns("ILb4sa0LahddRktnRipcu2vQnLb", sheet, "ROWS", 1, 2)
  295. time.sleep(0.5)
  296. Feishu.update_values("ILb4sa0LahddRktnRipcu2vQnLb", sheet, "A2:Z2", values)
  297. cls.remove_files(video_path_url)
  298. except Exception as e:
  299. Common.logger(mark).warning(f"{name}的{task_mark}任务处理失败:{e}")
  300. cls.remove_files(video_path_url)
  301. continue
  302. @classmethod
  303. def get_data_list(cls, channel_id, task_mark, url, number, mark, feishu_id, cookie_sheet, name, task):
  304. """
  305. 根据渠道ID获取数据列表
  306. """
  307. if channel_id == "抖音":
  308. return DY.get_dy_url(task_mark, url, number, mark, feishu_id, cookie_sheet, channel_id, name)
  309. elif channel_id == "票圈":
  310. return PQ.get_pq_url(task_mark, url, number, mark, channel_id, name)
  311. elif channel_id == "视频号":
  312. return SPH.get_sph_url(task_mark, url, number, mark, channel_id, name)
  313. elif channel_id == "快手":
  314. return KS.get_ks_url(task_mark, url, number, mark, feishu_id, cookie_sheet, channel_id, name)
  315. elif channel_id == "快手创作者版":
  316. return KsFeedVideo.get_data(channel_id, name)
  317. elif channel_id == "单点视频":
  318. return SPHDD.get_sphdd_data(url, channel_id, name)
  319. elif channel_id == "抖音历史":
  320. return DYLS.get_dy_zr_list(task_mark, url, number, mark, channel_id, name)
  321. elif channel_id == "快手历史":
  322. return KSLS.get_ksls_list(task_mark, url, number, mark, channel_id, name)
  323. elif channel_id == "视频号历史":
  324. return SPHLS.get_sphls_data(task_mark, url, number, mark, channel_id, name)
  325. elif channel_id == '抖音搜索':
  326. return DyKeyword.get_key_word(url, task_mark, mark, channel_id, name, task)
  327. @classmethod
  328. def generate_title(cls, video, title):
  329. """
  330. 生成新标题
  331. """
  332. if video['old_title']:
  333. new_title = video['old_title'].strip().replace("\n", "") \
  334. .replace("/", "").replace("\\", "").replace("\r", "") \
  335. .replace(":", "").replace("*", "").replace("?", "") \
  336. .replace("?", "").replace('"', "").replace("<", "") \
  337. .replace(">", "").replace("|", "").replace(" ", "") \
  338. .replace("&NBSP", "").replace(".", "。").replace(" ", "") \
  339. .replace("'", "").replace("#", "").replace("Merge", "")
  340. else:
  341. return '这个视频,分享给我的老友,祝愿您能幸福安康'
  342. if title == "原标题":
  343. if not new_title:
  344. new_title = '这个视频,分享给我的老友,祝愿您能幸福安康'
  345. elif title == "AI标题":
  346. if not new_title:
  347. new_title = '这个视频,分享给我的老友,祝愿您能幸福安康'
  348. else:
  349. new_title = GPT4o.get_ai_title(new_title)
  350. else:
  351. titles = title.split('/') if '/' in title else [title]
  352. new_title = random.choice(titles)
  353. return new_title
  354. @classmethod
  355. def download_and_process_video(cls, channel_id, video_url, video_path_url, v_id, crop_total, gg_duration_total,
  356. pw_random_id, new_title, mark, video):
  357. """
  358. 下载并处理视频
  359. """
  360. if channel_id in ["单点视频"]:
  361. new_video_path = PQ.sph_download_video(video_url, video_path_url, v_id, video, channel_id)
  362. if new_video_path == None:
  363. return None
  364. Common.logger(mark).info(f"{channel_id}视频下载成功: {new_video_path}")
  365. elif channel_id in ["票圈", "快手创作者版"]:
  366. new_video_path = PQ.download_video(video_url, video_path_url, v_id)
  367. if new_video_path == None:
  368. return None
  369. Common.logger(mark).info(f"{channel_id}视频下载成功: {new_video_path}")
  370. elif channel_id == "视频号历史":
  371. new_video_path = Oss.download_sph_ls(video_url, video_path_url, v_id)
  372. Common.logger(mark).info(f"{channel_id}视频下载成功: {new_video_path}")
  373. else:
  374. Common.logger(mark).info(f"视频准备下载")
  375. new_video_path = Oss.download_video_oss(video_url, video_path_url, v_id)
  376. Common.logger(mark).info(f"视频下载成功: {new_video_path}")
  377. if os.path.isfile(new_video_path):
  378. if crop_total and crop_total != 'None': # 判断是否需要裁剪
  379. new_video_path = FFmpeg.video_crop(new_video_path, video_path_url, pw_random_id)
  380. if gg_duration_total and gg_duration_total != 'None': # 判断是否需要指定视频时长
  381. new_video_path = FFmpeg.video_ggduration(new_video_path, video_path_url, pw_random_id,
  382. gg_duration_total)
  383. width, height = FFmpeg.get_w_h_size(new_video_path)
  384. if width < height: # 判断是否需要修改为竖屏
  385. new_video_path = FFmpeg.update_video_h_w(new_video_path, video_path_url, pw_random_id)
  386. new_title_re = re.sub(r'[^\w\s\u4e00-\u9fff,。!?]', '', new_title)
  387. if len(new_title_re) > 12:
  388. new_title_re = '\n'.join(
  389. [new_title_re[i:i + 12] for i in range(0, len(new_title_re), 12)])
  390. new_video_path = FFmpeg.add_video_zm(new_video_path, video_path_url, pw_random_id, new_title_re)
  391. return new_video_path
  392. else:
  393. return None
  394. @classmethod
  395. def handle_video_ending(cls, new_video_path, video_ending, old_title, pw_random_id, video_path_url, mark, task_mark, url, name, video_share, zm, voice):
  396. """
  397. 处理视频片尾
  398. """
  399. if video_ending == "AI片尾引导":
  400. pw_srt_text = GPT4o.get_ai_pw(old_title)
  401. if pw_srt_text:
  402. pw_url = TTS.get_pw_zm(pw_srt_text, voice)
  403. if pw_url:
  404. pw_mp3_path = TTS.download_mp3(pw_url, video_path_url, pw_random_id)
  405. # oss_mp3_key = Oss.mp3_upload_oss(pw_mp3_path, pw_random_id)
  406. # oss_mp3_key = oss_mp3_key.get("oss_object_key")
  407. # new_pw_path = f"http://art-crawler.oss-cn-hangzhou.aliyuncs.com/{oss_mp3_key}"
  408. # print(f"mp3地址:{new_pw_path}")
  409. # pw_url_sec = FFmpeg.get_video_duration(pw_mp3_path)
  410. pw_srt = TTS.getSrt(pw_url)
  411. Common.logger(mark).info(f"{name}的{task_mark}下的视频{url},获取AI片尾srt成功")
  412. else:
  413. # Feishu.bot('zhangyong', 'TTS获取失败提示', f'无法获取到片尾音频,及时更换token', "张勇")
  414. Common.logger(mark).info(f"{name}的{task_mark}下的视频{url},获取AI片尾失败")
  415. return None
  416. else:
  417. Common.logger(mark).info(f"{name}的{task_mark}下的视频{url},获取AI片尾失败")
  418. return None
  419. else:
  420. if ',' in video_ending:
  421. video_ending_list = video_ending.split(',')
  422. else:
  423. video_ending_list = [video_ending]
  424. ending = random.choice(video_ending_list)
  425. pw_list = Material.get_pwsrt_data("summary", "DgX7vC", ending) # 获取srt
  426. if pw_list:
  427. pw_id = pw_list["pw_id"]
  428. pw_srt = pw_list["pw_srt"]
  429. pw_url = PQ.get_pw_url(pw_id)
  430. pw_mp3_path = FFmpeg.get_video_mp3(pw_url, video_path_url, pw_random_id)
  431. else:
  432. Feishu.bot(mark, '机器自动改造消息通知', f'{task_mark}任务下片尾标示错误,请关注!!!!', name)
  433. for attempt in range(3):
  434. jpg_path = FFmpeg.video_png(new_video_path, video_path_url, pw_random_id) # 生成视频最后一帧jpg
  435. if os.path.isfile(jpg_path):
  436. Common.logger(mark).info(f"{name}的{task_mark}下的视频{url},生成视频最后一帧成功")
  437. break
  438. time.sleep(1)
  439. for attempt in range(3):
  440. Common.logger(mark).info(f"{name}的{task_mark}下的视频{url},获取mp3成功")
  441. pw_path = FFmpeg.pw_video(jpg_path, video_path_url, pw_mp3_path, pw_srt, pw_random_id,
  442. pw_mp3_path) # 生成片尾视频
  443. if os.path.isfile(pw_path):
  444. Common.logger(mark).info(f"{task_mark}下的视频{url},生成片尾视频成功")
  445. break
  446. time.sleep(1)
  447. pw_video_list = [new_video_path, pw_path]
  448. Common.logger(mark).info(f"{task_mark}下的视频{url},视频与片尾开始拼接")
  449. video_path = FFmpeg.concatenate_videos(pw_video_list, video_path_url) # 视频与片尾拼接到一起
  450. Common.logger(mark).info(f"{name}的{task_mark}下的视频{url},视频与片尾拼接成功")
  451. time.sleep(1)
  452. if video_share and video_share != 'None':
  453. new_video_path = FFmpeg.single_video(video_path, video_path_url, zm)
  454. else:
  455. new_video_path = video_path
  456. return new_video_path
  457. @classmethod
  458. def upload_video_and_thumbnail(cls, new_video_path, cover, v_id, new_title, task_mark, name, piaoquan_id,
  459. video_path_url, mark, channel_id, url, old_title, title, rule):
  460. """
  461. 上传视频和封面到OSS,并更新数据库
  462. """
  463. try:
  464. oss_id = cls.random_id()
  465. Common.logger(mark).info(f"{name}的{task_mark},开始发送oss")
  466. oss_object_key = Oss.stitching_sync_upload_oss(new_video_path, oss_id) # 视频发送OSS
  467. Common.logger(mark).info(f"{name}的{task_mark},发送oss成功{oss_object_key}")
  468. status = oss_object_key.get("status")
  469. if status == 200:
  470. oss_object_key = oss_object_key.get("oss_object_key")
  471. time.sleep(1)
  472. if channel_id == "快手历史":
  473. jpg = None
  474. else:
  475. if channel_id == "视频号历史":
  476. jpg_path = Oss.download_sph_ls(cover, video_path_url, v_id)
  477. else:
  478. jpg_path = PQ.download_video_jpg(cover, video_path_url, v_id) # 下载视频封面
  479. if os.path.isfile(jpg_path):
  480. oss_jpg_key = Oss.stitching_fm_upload_oss(jpg_path, oss_id) # 封面发送OSS
  481. status = oss_jpg_key.get("status")
  482. if status == 200:
  483. jpg = oss_jpg_key.get("oss_object_key")
  484. else:
  485. jpg = None
  486. else:
  487. jpg = None
  488. code = PQ.insert_piaoquantv(oss_object_key, new_title, jpg, piaoquan_id)
  489. Common.logger(mark).info(f"{name}的{task_mark}下的视频ID{v_id}发送成功")
  490. sqlCollect.insert_task(task_mark, v_id, mark, channel_id) # 插入数据库
  491. current_time = datetime.now()
  492. formatted_time = current_time.strftime("%Y-%m-%d %H:%M:%S")
  493. if name == "单点视频":
  494. url = str(rule)
  495. sqlCollect.insert_machine_making_data(name, task_mark, channel_id, url, v_id, piaoquan_id, new_title, code,
  496. formatted_time, old_title, oss_object_key)
  497. return code
  498. except Exception as e:
  499. cls.remove_files(video_path_url)
  500. Common.logger("error").warning(f"{name}的{task_mark}上传视频和封面到OSS,并更新数据库失败:{e}\n")
  501. return
  502. @classmethod
  503. def main(cls, data):
  504. """
  505. 主函数,初始化任务并使用线程池处理任务。
  506. """
  507. mark = data["mark"]
  508. name = data["name"]
  509. feishu_id = data["feishu_id"]
  510. feishu_sheet = data["feishu_sheet"]
  511. cookie_sheet = data["cookie_sheet"]
  512. if mark == 'pl-gjc':
  513. task_data = Material.get_keyword_data(feishu_id, feishu_sheet)
  514. else:
  515. task_data = Material.get_task_data(feishu_id, feishu_sheet)
  516. try:
  517. data = get_data(mark, task_data)
  518. if not data:
  519. Common.logger("redis").error(f"{mark}任务开始新的一轮\n")
  520. return
  521. task = json.loads(data)
  522. if mark == 'pl-gjc' and task['channel_id'] == '抖音搜索':
  523. count = get_first_value_with_prefix()
  524. increment_key()
  525. if int(count) == 300:
  526. Common.logger(mark).log(f"抖音搜索接口今日已经上限")
  527. return "抖音搜索上限"
  528. VideoProcessor.process_task(task, mark, name, feishu_id, cookie_sheet)
  529. return mark
  530. except Exception as e:
  531. Common.logger(mark).error(f"任务处理失败: {e}")
  532. return mark
  533. # if __name__ == "__main__":
  534. # main()