123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224 |
- """
- @author: luojunhui
- """
- import json
- import uuid
- import time
- import random
- import hashlib
- import urllib.parse
- from applications.functions.log import logging
- from applications.static.config import db_article
- from applications.functions.common import request_for_info
- class RecallDeal(object):
- """
- 召回逻辑
- """
- def __init__(self, trace_id, mysql_client, mini_program_type):
- self.trace_id = trace_id
- self.mysql_client = mysql_client
- self.mini_program_type = mini_program_type
- async def get_result(self):
- """
- 获取结果
- :return:
- """
- select_sql = f"""
- SELECT gh_id, recall_video_id1, recall_video_id2, recall_video_id3, kimi_title, content_status, process_times
- FROM {db_article}
- WHERE trace_id = '{self.trace_id}';
- """
- info_tuple = await self.mysql_client.async_select(select_sql)
- gh_id, vid1, vid2, vid3, kimi_title, content_status, process_times = info_tuple[0]
- response = {
- "gh_id": gh_id,
- "vid_list": [vid1, vid2, vid3],
- "kimi_title": kimi_title,
- "content_status": content_status,
- "process_times": process_times
- }
- return response
- def create_gzh_path(self, video_id, shared_uid, gh_id):
- """
- :param gh_id: 公众号账号的gh_id
- :param video_id: 视频 id
- :param shared_uid: 分享 id
- """
- def generate_source_id():
- """
- generate_source_id
- :return:
- """
- timestamp = str(int(time.time() * 1000))
- random_str = str(random.randint(1000, 9999))
- hash_input = f"{timestamp}-{random_str}"
- return hashlib.md5(hash_input.encode()).hexdigest()
- root_share_id = str(uuid.uuid4())
- if self.mini_program_type == 2:
- source_id = "touliu_tencentGzhArticle_{}_".format(gh_id) + generate_source_id()
- elif self.mini_program_type == 1:
- source_id = "longArticles_" + generate_source_id()
- elif self.mini_program_type == 3:
- source_id = "WeCom_" + generate_source_id()
- else:
- source_id = "Error mini_program_type {}".format(self.mini_program_type)
- url = f"pages/user-videos?id={video_id}&su={shared_uid}&fromGzh=1&rootShareId={root_share_id}&shareId={root_share_id}&rootSourceId={source_id}"
- # 自动把 root_share_id 加入到白名单
- # auto_white(root_share_id)
- return root_share_id, source_id, f"pages/category?jumpPage={urllib.parse.quote(url, safe='')}"
- def choose_mini_program(self):
- """
- 获取小程序分享卡片
- :return:
- """
- if self.mini_program_type == 1:
- # 正常长文业务
- programAvatar = "https://rescdn.yishihui.com/0temp/ssyqsh.png"
- programId = "wx59d9e2c05f00f880"
- programName = "刷刷有趣生活"
- elif self.mini_program_type == 2:
- # 投流业务
- programAvatar = "https://rescdn.yishihui.com/0temp/zfyfyc.jpeg"
- programId = "wxcddf231abd0dabdc"
- programName = "祝福有福有财"
- elif self.mini_program_type == 3:
- # 企业微信
- programAvatar = "https://rescdn.yishihui.com/0temp/xymhfqdd.png"
- programId = "wx7187c217efef24a7"
- programName = "幸运美好福气多多"
- else:
- programAvatar = "https://rescdn.yishihui.com/0temp/ssyqsh.png"
- programId = "wx59d9e2c05f00f880"
- programName = "刷刷有趣生活"
- return programAvatar, programId, programName
- async def generate_card(self, video_id, kimi_title, index, gh_id):
- """
- 生成分享卡片
- :return:
- """
- response = request_for_info(video_id)
- productionCover = response['data'][0]['shareImgPath']
- productionName = kimi_title
- videoUrl = response['data'][0]['videoPath']
- user_id = response['data'][0]['user']['uid']
- programAvatar, programId, programName = self.choose_mini_program()
- root_share_id, source_id, productionPath = self.create_gzh_path(video_id, user_id, gh_id)
- source = "Web"
- logging(
- code="1002",
- info="root_share_id --{}, productionPath -- {}".format(root_share_id, productionPath),
- function="process",
- trace_id=self.trace_id
- )
- result = {
- "productionCover": productionCover,
- "productionName": productionName,
- "programAvatar": programAvatar,
- "programId": programId,
- "programName": programName,
- "source": source,
- "rootShareId": root_share_id,
- "productionPath": productionPath,
- "videoUrl": videoUrl,
- "paragraphPosition": index * 0.25
- }
- update_result_sql = f"""
- UPDATE {db_article}
- SET
- result{index} = %s,
- success = %s
- WHERE
- trace_id = %s;
- """
- await self.mysql_client.async_insert(
- sql=update_result_sql,
- params=(json.dumps(result, ensure_ascii=False), 1, self.trace_id)
- )
- logging(
- code="2000",
- info="统计 root_share_id && video_id",
- function="process",
- trace_id=self.trace_id,
- data={
- "rootShareId": root_share_id,
- "videoId": video_id,
- "sourceId": source_id
- }
- )
- return result
- async def deal(self):
- """
- Recall Deal
- :return:
- """
- response = await self.get_result()
- status_code = response.get("content_status")
- process_times = response.get("process_times")
- if status_code == 0:
- if process_times > 5:
- result = {
- "traceId": self.trace_id,
- "code": 0,
- "error": "匹配失败,处理超过五次,文章敏感"
- }
- else:
- result = {
- "traceId": self.trace_id,
- "code": 0,
- "Message": "该请求还没处理"
- }
- elif status_code == 1:
- result = {
- "traceId": self.trace_id,
- "code": 1,
- "Message": "该请求正在处理中"
- }
- elif status_code == 2:
- L = []
- unEmptyList = [i for i in response['vid_list'] if i]
- for index, best_video_id in enumerate(unEmptyList, 1):
- card = await self.generate_card(best_video_id, response.get("kimi_title"), index, response['gh_id'])
- L.append(card)
- if L:
- result = {
- "traceId": self.trace_id,
- "miniprogramList": L
- }
- else:
- result = {
- "traceId": self.trace_id,
- "code": 0,
- "error": "匹配失败,视频下载失败返回vid为0"
- }
- elif status_code == 3:
- result = {
- "traceId": self.trace_id,
- "code": 0,
- "error": "匹配失败,处理超过五次,文章敏感"
- }
- else:
- result = {
- "traceId": self.trace_id,
- "Message": "UnKnow Error"
- }
- logging(
- code="1002",
- info="返回结果",
- function="process",
- data=result,
- trace_id=self.trace_id
- )
- return result
|