|
@@ -0,0 +1,338 @@
|
|
|
+"""
|
|
|
+@author: luojunhui
|
|
|
+"""
|
|
|
+import json
|
|
|
+import time
|
|
|
+import random
|
|
|
+import hashlib
|
|
|
+
|
|
|
+import uuid
|
|
|
+import urllib.parse
|
|
|
+
|
|
|
+from applications.functions.common import async_post
|
|
|
+from applications.functions.log import logging
|
|
|
+from static.config import db_article
|
|
|
+
|
|
|
+
|
|
|
+async def publishToPQ(video_obj):
|
|
|
+ """
|
|
|
+ publish video to pq
|
|
|
+ :return:
|
|
|
+ """
|
|
|
+ oss_path = video_obj['videoPath']
|
|
|
+ uid = video_obj['uid']
|
|
|
+ title = video_obj['title']
|
|
|
+ cover = video_obj['coverPath']
|
|
|
+ url = "https://vlogapi.piaoquantv.com/longvideoapi/crawler/video/send"
|
|
|
+ headers = {
|
|
|
+ "User-Agent": "PQSpeed/486 CFNetwork/1410.1 Darwin/22.6.0",
|
|
|
+ "cookie": "JSESSIONID=4DEA2B5173BB9A9E82DB772C0ACDBC9F; JSESSIONID=D02C334150025222A0B824A98B539B78",
|
|
|
+ "referer": "http://appspeed.piaoquantv.com",
|
|
|
+ "token": "524a8bc871dbb0f4d4717895083172ab37c02d2f",
|
|
|
+ "accept-language": "zh-CN,zh-Hans;q=0.9",
|
|
|
+ "Content-Type": "application/x-www-form-urlencoded",
|
|
|
+ }
|
|
|
+ payload = {
|
|
|
+ "coverImgPath": cover,
|
|
|
+ "deviceToken": "9ef064f2f7869b3fd67d6141f8a899175dddc91240971172f1f2a662ef891408",
|
|
|
+ "fileExtensions": "MP4",
|
|
|
+ "loginUid": uid,
|
|
|
+ "networkType": "Wi-Fi",
|
|
|
+ "platform": "iOS",
|
|
|
+ "requestId": "fb972cbd4f390afcfd3da1869cd7d001",
|
|
|
+ "sessionId": "362290597725ce1fa870d7be4f46dcc2",
|
|
|
+ "subSessionId": "362290597725ce1fa870d7be4f46dcc2",
|
|
|
+ "title": title,
|
|
|
+ "token": "524a8bc871dbb0f4d4717895083172ab37c02d2f",
|
|
|
+ "uid": uid,
|
|
|
+ "versionCode": "486",
|
|
|
+ "versionName": "3.4.12",
|
|
|
+ "videoFromScene": "1",
|
|
|
+ "videoPath": oss_path,
|
|
|
+ "viewStatus": "1",
|
|
|
+ }
|
|
|
+ response = await async_post(url, headers, payload)
|
|
|
+ print(response)
|
|
|
+ return response
|
|
|
+
|
|
|
+
|
|
|
+async def getPQVideoDetail(video_id):
|
|
|
+ """
|
|
|
+ 获取票圈视频详情信息
|
|
|
+ :return:
|
|
|
+ """
|
|
|
+ url = "https://longvideoapi.piaoquantv.com/longvideoapi/openapi/video/batchSelectVideoInfo"
|
|
|
+ data = {
|
|
|
+ "videoIdList": [video_id]
|
|
|
+ }
|
|
|
+ header = {
|
|
|
+ "Content-Type": "application/json",
|
|
|
+ }
|
|
|
+ response = await async_post(url, header, json.dumps(data))
|
|
|
+ print(response)
|
|
|
+ return response
|
|
|
+
|
|
|
+
|
|
|
+class FinalResponse(object):
|
|
|
+ """
|
|
|
+ 视频发布至pq并且获取video_id
|
|
|
+ """
|
|
|
+
|
|
|
+ def __init__(self, params, mysql_client):
|
|
|
+ """
|
|
|
+ :param params:
|
|
|
+ :param mysql_client:
|
|
|
+ :return:
|
|
|
+ """
|
|
|
+ self.kimi_title = None
|
|
|
+ self.video_obj_list = None
|
|
|
+ self.mini_type = None
|
|
|
+ self.gh_id = None
|
|
|
+ self.trace_id = None
|
|
|
+ self.params = params
|
|
|
+ self.mysql_client = mysql_client
|
|
|
+ self.mini_map = {
|
|
|
+ 25: {
|
|
|
+ "avatar": "https://rescdn.yishihui.com/0temp/ttmhzfsh.png",
|
|
|
+ "id": "wx0b7d95eb293b783b",
|
|
|
+ "name": "天天美好祝福生活",
|
|
|
+ "index": 25,
|
|
|
+ },
|
|
|
+ 29: {
|
|
|
+ "avatar": "https://rescdn.yishihui.com/0temp/cyfyld.png",
|
|
|
+ "id": "wx65c76bb4c67934db",
|
|
|
+ "name": "财运福运来到",
|
|
|
+ "index": 29,
|
|
|
+ },
|
|
|
+ 31: {
|
|
|
+ "avatar": "https://rescdn.yishihui.com/0temp/mhzfshxf2.png",
|
|
|
+ "id": "wx2e4478b1641b3b15",
|
|
|
+ "name": "美好祝福生活幸福",
|
|
|
+ "index": 31,
|
|
|
+ },
|
|
|
+ 36: {
|
|
|
+ "avatar": "https://rescdn.yishihui.com/0temp/zfyfyc.jpeg",
|
|
|
+ "id": "wxcddf231abd0dabdc",
|
|
|
+ "name": "祝福有福有财",
|
|
|
+ "index": 36,
|
|
|
+ },
|
|
|
+ 27: {
|
|
|
+ "avatar": "https://rescdn.yishihui.com/0temp/xymhfqdd.png",
|
|
|
+ "id": "wx7187c217efef24a7",
|
|
|
+ "name": "幸运美好福气多多",
|
|
|
+ "index": 27,
|
|
|
+ },
|
|
|
+ 33: {
|
|
|
+ "avatar": "https://rescdn.yishihui.com/0temp/ssnnfqd.jpeg",
|
|
|
+ "id": "wx5e67713277549b6f",
|
|
|
+ "name": "年年岁岁福气多",
|
|
|
+ "index": 33,
|
|
|
+ },
|
|
|
+ }
|
|
|
+
|
|
|
+ def checkParams(self):
|
|
|
+ """
|
|
|
+ 校验参数
|
|
|
+ :return:
|
|
|
+ """
|
|
|
+ try:
|
|
|
+ self.trace_id = self.params['traceId']
|
|
|
+ self.kimi_title = self.params['kimiTitle']
|
|
|
+ self.gh_id = self.params['ghId']
|
|
|
+ self.mini_type = self.params['miniType']
|
|
|
+ self.video_obj_list = self.params['videoObjList']
|
|
|
+ return None
|
|
|
+ except Exception as e:
|
|
|
+ response = {
|
|
|
+ "traceId": self.trace_id,
|
|
|
+ "error": str(e),
|
|
|
+ "info": "参数校验失败"
|
|
|
+ }
|
|
|
+ return response
|
|
|
+
|
|
|
+ def createGzhPath(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_type == 2:
|
|
|
+ source_id = (
|
|
|
+ "touliu_tencentGzhArticle_{}_".format(gh_id) + generate_source_id()
|
|
|
+ )
|
|
|
+ elif self.mini_type == 1:
|
|
|
+ source_id = "longArticles_" + generate_source_id()
|
|
|
+ elif self.mini_type == 3:
|
|
|
+ source_id = "WeCom_" + generate_source_id()
|
|
|
+ else:
|
|
|
+ source_id = "Error mini_program_type {}".format(self.mini_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 chooseMiniProgram(self, mini_id):
|
|
|
+ """
|
|
|
+ 获取小程序分享卡片
|
|
|
+ :return:
|
|
|
+ """
|
|
|
+ mini_info = self.mini_map[mini_id]
|
|
|
+ return (
|
|
|
+ mini_info["avatar"],
|
|
|
+ mini_info["id"],
|
|
|
+ mini_info["name"],
|
|
|
+ mini_info["index"],
|
|
|
+ )
|
|
|
+
|
|
|
+ async def generateSingleCard(self, video_id, title, index, gh_id, mini_id):
|
|
|
+ """
|
|
|
+ 生成单个分享卡片
|
|
|
+ :return:
|
|
|
+ """
|
|
|
+ response = await getPQVideoDetail(video_id)
|
|
|
+ print(response)
|
|
|
+ productionCover = response["data"][0]["shareImgPath"]
|
|
|
+ videoUrl = response["data"][0]["videoPath"]
|
|
|
+ user_id = response["data"][0]["user"]["uid"]
|
|
|
+ programAvatar, programId, programName, pqMiniId = self.chooseMiniProgram(mini_id)
|
|
|
+ root_share_id, source_id, productionPath = self.createGzhPath(video_id, user_id, gh_id)
|
|
|
+ logging(
|
|
|
+ code="1002",
|
|
|
+ info="root_share_id --{}, productionPath -- {}".format(
|
|
|
+ root_share_id, productionPath
|
|
|
+ ),
|
|
|
+ function="process",
|
|
|
+ trace_id=self.trace_id,
|
|
|
+ )
|
|
|
+ result = {
|
|
|
+ "productionCover": productionCover,
|
|
|
+ "productionName": title,
|
|
|
+ "programAvatar": programAvatar,
|
|
|
+ "programId": programId,
|
|
|
+ "programName": programName,
|
|
|
+ "source": "web",
|
|
|
+ "rootShareId": root_share_id,
|
|
|
+ "productionPath": productionPath,
|
|
|
+ "videoUrl": videoUrl,
|
|
|
+ "mini_id": mini_id,
|
|
|
+ "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 generateCards(self, video_list, kimi_title, gh_id):
|
|
|
+ """
|
|
|
+ 生成一组卡片
|
|
|
+ :param video_list:
|
|
|
+ :param kimi_title:
|
|
|
+ :param gh_id:
|
|
|
+ :return:
|
|
|
+ """
|
|
|
+ L = []
|
|
|
+ if self.mini_type == 1:
|
|
|
+ mini_id_list = [25, 29, 31]
|
|
|
+ video_count = len(video_list)
|
|
|
+ mini_choice_index = random.sample(range(3), video_count)
|
|
|
+ mini_choice_item = [mini_id_list[i] for i in mini_choice_index]
|
|
|
+ for index, video_id in enumerate(video_list, 1):
|
|
|
+ result = await self.generateSingleCard(
|
|
|
+ video_id=video_id,
|
|
|
+ title=kimi_title,
|
|
|
+ index=index,
|
|
|
+ gh_id=gh_id,
|
|
|
+ mini_id=mini_choice_item[index - 1],
|
|
|
+ )
|
|
|
+ L.append(result)
|
|
|
+ elif self.mini_type == 2:
|
|
|
+ for index, video_id in enumerate(video_list, 1):
|
|
|
+ result = await self.generateSingleCard(
|
|
|
+ video_id=video_id,
|
|
|
+ title=kimi_title,
|
|
|
+ index=index,
|
|
|
+ gh_id=gh_id,
|
|
|
+ mini_id=33,
|
|
|
+ )
|
|
|
+ L.append(result)
|
|
|
+ elif self.mini_type == 3:
|
|
|
+ for index, video_id in enumerate(video_list, 1):
|
|
|
+ result = await self.generateSingleCard(
|
|
|
+ video_id=video_id,
|
|
|
+ title=kimi_title,
|
|
|
+ index=index,
|
|
|
+ gh_id=gh_id,
|
|
|
+ mini_id=27,
|
|
|
+ )
|
|
|
+ L.append(result)
|
|
|
+ return L
|
|
|
+
|
|
|
+ async def getVideoIdList(self):
|
|
|
+ """
|
|
|
+ 获取匹配文章的id_list
|
|
|
+ :return:
|
|
|
+ """
|
|
|
+ vid_list = []
|
|
|
+ for video_obj in self.video_obj_list:
|
|
|
+ video_publish_info = await publishToPQ(video_obj=video_obj)
|
|
|
+ video_id = video_publish_info['data']['id']
|
|
|
+ vid_list.append(video_id)
|
|
|
+ return vid_list
|
|
|
+
|
|
|
+ async def deal(self):
|
|
|
+ """
|
|
|
+ deal function
|
|
|
+ :return:
|
|
|
+ """
|
|
|
+ params_error = self.checkParams()
|
|
|
+ if params_error:
|
|
|
+ return params_error
|
|
|
+ else:
|
|
|
+ video_list = await self.getVideoIdList()
|
|
|
+ card_list = await self.generateCards(
|
|
|
+ video_list, self.kimi_title, self.gh_id
|
|
|
+ )
|
|
|
+ response = {
|
|
|
+ "trace_id": self.trace_id,
|
|
|
+ "miniprogramList": card_list
|
|
|
+ }
|
|
|
+ return response
|
|
|
+
|
|
|
+
|
|
|
+
|