|
@@ -0,0 +1,131 @@
|
|
|
+"""
|
|
|
+@author: luojunhui
|
|
|
+生成sourceId
|
|
|
+"""
|
|
|
+import datetime
|
|
|
+import hashlib
|
|
|
+import random
|
|
|
+import time
|
|
|
+
|
|
|
+import urllib.parse
|
|
|
+
|
|
|
+from config import source_id_db
|
|
|
+
|
|
|
+
|
|
|
+def createPath(video_id, shared_uid, gh_id, business_type, index):
|
|
|
+ """
|
|
|
+ :param index: 视频位置信息
|
|
|
+ :param business_type: 业务类型
|
|
|
+ :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()
|
|
|
+
|
|
|
+ match business_type:
|
|
|
+ case 1:
|
|
|
+ # 公众号投流
|
|
|
+ source_id = "GzhTouLiu_Articles_{}_".format(gh_id) + generate_source_id()
|
|
|
+ url = f"pages/user-videos?id={video_id}&su={shared_uid}&fromGzh=1&index={index}&rootSourceId={source_id}"
|
|
|
+ return source_id, f"pages/category?jumpPage={urllib.parse.quote(url, safe='')}"
|
|
|
+ case 2:
|
|
|
+ # 小程序投流
|
|
|
+ source_id = "GzhTouLiu_Minigram_{}_".format(gh_id) + generate_source_id()
|
|
|
+ url = f"pages/user-videos?id={video_id}&su={shared_uid}&fromGzh=1&index={index}&rootSourceId={source_id}"
|
|
|
+ return source_id, f"pages/category?jumpPage={urllib.parse.quote(url, safe='')}"
|
|
|
+ case 3:
|
|
|
+ # 小程序投流
|
|
|
+ source_id = "GzhTouLiu_Wecom_{}_".format(gh_id) + generate_source_id()
|
|
|
+ url = f"pages/user-videos?id={video_id}&su={shared_uid}&fromGzh=1&index={index}&rootSourceId={source_id}"
|
|
|
+ return source_id, f"pages/category?jumpPage={urllib.parse.quote(url, safe='')}"
|
|
|
+
|
|
|
+ return None, None
|
|
|
+
|
|
|
+
|
|
|
+async def saveSourceToDB(db_client, params, request_id):
|
|
|
+ """
|
|
|
+ 将sourceId信息存储到数据库
|
|
|
+ :param request_id: 请求id
|
|
|
+ :param db_client:
|
|
|
+ :param params:
|
|
|
+ :return:
|
|
|
+ """
|
|
|
+ # input 信息
|
|
|
+ try:
|
|
|
+ # 视频信息
|
|
|
+ video_id = params['videoId']
|
|
|
+ video_cover = params['videoCover']
|
|
|
+ video_title = params['videoTitle']
|
|
|
+ uid = params['uid']
|
|
|
+ # 公众号信息
|
|
|
+ account_name = params['accountName']
|
|
|
+ gh_id = params['ghId']
|
|
|
+ # 文章信息
|
|
|
+ article_title = params['articleTitle']
|
|
|
+ # 业务信息
|
|
|
+ business_type = params['businessType']
|
|
|
+ position = params['position']
|
|
|
+ cooperation = params['cooperation']
|
|
|
+
|
|
|
+ except Exception as e:
|
|
|
+ response = {
|
|
|
+ "error": str(e),
|
|
|
+ "message": "params error",
|
|
|
+ "requestId": request_id
|
|
|
+ }
|
|
|
+ return response
|
|
|
+
|
|
|
+ # source_id, gzh_path
|
|
|
+ source_id, share_path = createPath(video_id, uid, gh_id, business_type, position)
|
|
|
+
|
|
|
+ # minigram_info
|
|
|
+ minigram_name = "祝福岁岁平安"
|
|
|
+ minigram_id = "wxe54dd94c5246127a"
|
|
|
+
|
|
|
+ sql = f"""
|
|
|
+ INSERT INTO {source_id_db}
|
|
|
+ (video_id, video_cover, video_title, uid, gh_id, account_name, article_title, business_type, cooperation, position, source_id, share_path, create_time, minigram_name, minigram_id)
|
|
|
+ values
|
|
|
+ (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s);
|
|
|
+ """
|
|
|
+ try:
|
|
|
+ await db_client.asyncInsert(
|
|
|
+ sql=sql,
|
|
|
+ params=(
|
|
|
+ video_id,
|
|
|
+ video_cover,
|
|
|
+ video_title,
|
|
|
+ uid,
|
|
|
+ gh_id,
|
|
|
+ account_name,
|
|
|
+ article_title,
|
|
|
+ business_type,
|
|
|
+ cooperation,
|
|
|
+ position,
|
|
|
+ source_id,
|
|
|
+ share_path,
|
|
|
+ datetime.datetime.now().__str__(),
|
|
|
+ minigram_name,
|
|
|
+ minigram_id
|
|
|
+ )
|
|
|
+ )
|
|
|
+ response = {
|
|
|
+ "requestId": request_id,
|
|
|
+ "message": "success"
|
|
|
+ }
|
|
|
+ except Exception as e:
|
|
|
+ response = {
|
|
|
+ "requestId": request_id,
|
|
|
+ "error": str(e),
|
|
|
+ "info": "db error"
|
|
|
+ }
|
|
|
+ return response
|