video_processor.py 28 KB

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