publish_video_to_pq_for_audit.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. """
  2. @author: luojunhui
  3. 将抓取的视频发送至pq获取视频的审核结果
  4. """
  5. import time
  6. import traceback
  7. from typing import List, Dict
  8. from tqdm import tqdm
  9. from pymysql.cursors import DictCursor
  10. from applications import log
  11. from applications import PQAPI
  12. from applications import longArticlesMySQL
  13. from applications.const import WeixinVideoCrawlerConst
  14. from applications.api import generate_mini_program_title
  15. const = WeixinVideoCrawlerConst()
  16. pq_functions = PQAPI()
  17. class PublishVideosForAudit(object):
  18. """
  19. 发布视频到pq,获取video_id,并且轮询获取视频id状态
  20. """
  21. def __init__(self):
  22. self.db = longArticlesMySQL()
  23. def get_publish_video_list(self) -> List[Dict]:
  24. """
  25. 获取视频的信息
  26. :return:
  27. """
  28. already_published_count = self.get_published_articles_today()
  29. rest_count = const.MAX_VIDEO_NUM - already_published_count
  30. sql = f"""
  31. SELECT id, article_title, video_oss_path
  32. FROM publish_single_video_source
  33. WHERE audit_status = {const.VIDEO_AUDIT_INIT_STATUS} and bad_status = {const.TITLE_DEFAULT_STATUS}
  34. LIMIT {rest_count};
  35. """
  36. response = self.db.select(sql, cursor_type=DictCursor)
  37. return response
  38. def update_audit_status(self, video_id: int, ori_audit_status: int, new_audit_status: int) -> int:
  39. """
  40. 更新视频的审核状态
  41. :param new_audit_status:
  42. :param ori_audit_status:
  43. :param video_id:
  44. :param
  45. :return:
  46. """
  47. update_sql = f"""
  48. UPDATE publish_single_video_source
  49. SET audit_status = %s
  50. WHERE audit_video_id = %s and audit_status = %s;
  51. """
  52. affected_rows = self.db.update(
  53. sql=update_sql,
  54. params=(new_audit_status, video_id, ori_audit_status)
  55. )
  56. return affected_rows
  57. def get_published_articles_today(self):
  58. """
  59. 获取今天发布的视频数量总量
  60. :return:
  61. """
  62. select_sql = f"""
  63. SELECT COUNT(1) as total_count
  64. FROM publish_single_video_source
  65. WHERE audit_status != {const.VIDEO_AUDIT_INIT_STATUS}
  66. AND DATE(FROM_UNIXTIME(audit_timestamp)) = CURDATE();
  67. """
  68. response = self.db.select(select_sql, cursor_type=DictCursor)
  69. return response[0]['total_count']
  70. def publish_each_video(self, video_obj: Dict) -> Dict:
  71. """
  72. 发布视频到pq
  73. :param video_obj:
  74. :return:
  75. """
  76. response = pq_functions.publish_to_pq(
  77. oss_path=video_obj.get("video_oss_path"),
  78. uid=const.DEFAULT_ACCOUNT_UID,
  79. title=video_obj.get("article_title")
  80. )
  81. response_json = response.json()
  82. if response_json.get("code") == const.REQUEST_SUCCESS:
  83. video_id = response_json['data']['id']
  84. update_sql = f"""
  85. UPDATE publish_single_video_source
  86. SET audit_status = %s, audit_video_id = %s, audit_timestamp = %s
  87. WHERE id = %s;
  88. """
  89. affected_rows = self.db.update(
  90. sql=update_sql,
  91. params=(const.VIDEO_AUDIT_PROCESSING_STATUS, video_id, int(time.time()), video_obj['id'])
  92. )
  93. if affected_rows:
  94. result = {
  95. "status": "success",
  96. "video_id": video_id
  97. }
  98. return result
  99. else:
  100. result = {
  101. "status": "fail",
  102. "video_id": video_id,
  103. "error_msg": "抢占锁失败,update执行操作修改0行"
  104. }
  105. return result
  106. else:
  107. if response_json.get("code") == const.PUBLISHED_ILLEGAL_TITLE_CODE:
  108. # 发布了标题违规的视频,发布失败, 修改审核状态从0-->2
  109. update_sql = f"""
  110. UPDATE publish_single_video_source
  111. SET audit_status = %s
  112. WHERE id = %s and audit_status = %s;
  113. """
  114. self.db.update(update_sql, params=(const.VIDEO_AUDIT_FAIL_STATUS, video_obj['id'], const.VIDEO_AUDIT_INIT_STATUS))
  115. result = {
  116. "status": "fail",
  117. "error_msg": "发布到pq失败",
  118. "title": video_obj.get("article_title"),
  119. "oss_path": video_obj.get("video_oss_path"),
  120. "response": response_json
  121. }
  122. return result
  123. def get_check_article_list(self) -> List[Dict]:
  124. """
  125. 获取需要检查的视频列表
  126. :return:
  127. """
  128. sql = f"""SELECT audit_video_id FROM publish_single_video_source WHERE audit_status = {const.VIDEO_AUDIT_PROCESSING_STATUS};"""
  129. response = self.db.select(sql, cursor_type=DictCursor)
  130. return response
  131. def update_mini_program_title(self, video_id: int) -> bool:
  132. """
  133. :param video_id:
  134. """
  135. select_sql = f"""
  136. SELECT article_title FROM publish_single_video_source WHERE audit_video_id = {video_id};
  137. """
  138. title = self.db.select(select_sql, cursor_type=DictCursor)[0]['article_title']
  139. try:
  140. mini_program_title = generate_mini_program_title(title)
  141. update_sql = f"""
  142. UPDATE publish_single_video_source SET mini_program_title = %s WHERE audit_video_id = %s;
  143. """
  144. self.db.update(update_sql, params=(mini_program_title, video_id))
  145. log(
  146. task="publish_video_for_audit",
  147. function="update_mini_program_title",
  148. message="修改小程序标题成功",
  149. data={
  150. "video_id": video_id,
  151. "title": title,
  152. "mini_program_title": mini_program_title
  153. }
  154. )
  155. return True
  156. except Exception as e:
  157. log(
  158. task="publish_video_for_audit",
  159. function="update_mini_program_title",
  160. status="fail",
  161. data={
  162. "video_id": video_id,
  163. "title": title,
  164. "error": str(e),
  165. "error_stack": traceback.format_exc()
  166. }
  167. )
  168. return False
  169. def check_video_status(self, video_id: int) -> Dict:
  170. """
  171. 检查视频的状态,若视频审核通过or不通过,修改记录状态
  172. :param video_id:
  173. :return:
  174. """
  175. response = pq_functions.getPQVideoListDetail([video_id])
  176. audit_status = response.get("data")[0].get("auditStatus")
  177. # 请求成功
  178. if audit_status == const.PQ_AUDIT_SUCCESS_STATUS:
  179. # 更新小程序标题字段
  180. mini_program_title_flag = self.update_mini_program_title(video_id)
  181. if mini_program_title_flag:
  182. # 处理成功,修改审核状态为1
  183. affected_rows = self.update_audit_status(
  184. video_id=video_id,
  185. ori_audit_status=const.VIDEO_AUDIT_PROCESSING_STATUS,
  186. new_audit_status=const.VIDEO_AUDIT_SUCCESS_STATUS
  187. )
  188. else:
  189. # 修改小程序标题失败,修改审核状态为4
  190. affected_rows = self.update_audit_status(
  191. video_id=video_id,
  192. ori_audit_status=const.VIDEO_AUDIT_PROCESSING_STATUS,
  193. new_audit_status=const.VIDEO_TITLE_GENERATE_FAIL_STATUS
  194. )
  195. elif audit_status in {const.PQ_AUDIT_SELF_VISIBLE_STATUS, const.PQ_AUDIT_FAIL_STATUS}:
  196. # 视频审核失败,修改审核状态为2
  197. affected_rows = self.update_audit_status(
  198. video_id=video_id,
  199. ori_audit_status=const.VIDEO_AUDIT_PROCESSING_STATUS,
  200. new_audit_status=const.VIDEO_AUDIT_FAIL_STATUS
  201. )
  202. elif audit_status == const.PQ_AUDIT_PROCESSING_STATUS:
  203. # 视频正在审核中,不做处理
  204. affected_rows = 0
  205. else:
  206. # 其他情况,暂时不做处理
  207. affected_rows = 0
  208. result = {
  209. "affected_rows": affected_rows,
  210. "video_id": video_id,
  211. "audit_status": audit_status
  212. }
  213. return result
  214. def publish_job(self):
  215. """
  216. 发布视频到pq
  217. :return:
  218. """
  219. video_list = self.get_publish_video_list()
  220. for video_obj in tqdm(video_list, desc="视频发布"):
  221. try:
  222. response = self.publish_each_video(video_obj)
  223. if response.get("status") == "success":
  224. log(
  225. task="publish_video_for_audit",
  226. message="发送至PQ成功",
  227. function="publish_each_video",
  228. data={
  229. "video_id": response.get("video_id")
  230. }
  231. )
  232. else:
  233. log(
  234. task="publish_video_for_audit",
  235. message=response.get('error_msg'),
  236. function="publish_each_video",
  237. status="fail",
  238. data={
  239. "response": response,
  240. "video_obj": video_obj
  241. }
  242. )
  243. except Exception as e:
  244. error_msg = traceback.format_exc()
  245. log(
  246. task="publish_video_for_audit",
  247. message="发送至PQ代码执行失败",
  248. function="publish_each_video",
  249. status="fail",
  250. data={
  251. "error_msg": error_msg,
  252. "video_obj": video_obj,
  253. "error": str(e)
  254. }
  255. )
  256. def check_job(self):
  257. """
  258. 检查视频的状态
  259. :return:
  260. """
  261. video_list = self.get_check_article_list()
  262. for video_obj in tqdm(video_list, desc="视频检查"):
  263. video_id = video_obj.get("audit_video_id")
  264. try:
  265. response = self.check_video_status(video_id)
  266. if response.get("affected_rows"):
  267. continue
  268. else:
  269. log(
  270. task="publish_video_for_audit",
  271. function="check_each_video",
  272. message="修改行数为0",
  273. data={
  274. "video_id": video_id,
  275. "audit_status": response['audit_status']
  276. }
  277. )
  278. except Exception as e:
  279. error_msg = traceback.format_exc()
  280. log(
  281. task="publish_video_for_audit",
  282. message="查询状态执行失败",
  283. function="check_each_video",
  284. status="fail",
  285. data={
  286. "error_msg": error_msg,
  287. "video_obj": video_obj,
  288. "error": str(e)
  289. }
  290. )