sourceIdRoute.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. """
  2. @author: luojunhui
  3. 生成sourceId
  4. """
  5. import datetime
  6. import hashlib
  7. import random
  8. import time
  9. import urllib.parse
  10. from config import source_id_db
  11. def createPath(video_id, shared_uid, gh_id, business_type, index):
  12. """
  13. :param index: 视频位置信息
  14. :param business_type: 业务类型
  15. :param gh_id: 公众号账号的gh_id
  16. :param video_id: 视频 id
  17. :param shared_uid: 分享 id
  18. """
  19. def generate_source_id():
  20. """
  21. generate_source_id
  22. :return:
  23. """
  24. timestamp = str(int(time.time() * 1000))
  25. random_str = str(random.randint(1000, 9999))
  26. hash_input = f"{timestamp}-{random_str}"
  27. return hashlib.md5(hash_input.encode()).hexdigest()
  28. match business_type:
  29. case 1:
  30. # 公众号投流
  31. source_id = "GzhTouLiu_Articles_{}_".format(gh_id) + generate_source_id()
  32. url = f"pages/user-videos?id={video_id}&su={shared_uid}&fromGzh=1&index={index}&rootSourceId={source_id}"
  33. return source_id, f"pages/category?jumpPage={urllib.parse.quote(url, safe='')}"
  34. case 2:
  35. # 小程序投流
  36. source_id = "GzhTouLiu_Minigram_{}_".format(gh_id) + generate_source_id()
  37. url = f"pages/user-videos?id={video_id}&su={shared_uid}&fromGzh=1&index={index}&rootSourceId={source_id}"
  38. return source_id, f"pages/category?jumpPage={urllib.parse.quote(url, safe='')}"
  39. case 3:
  40. # 小程序投流
  41. source_id = "GzhTouLiu_Wecom_{}_".format(gh_id) + generate_source_id()
  42. url = f"pages/user-videos?id={video_id}&su={shared_uid}&fromGzh=1&index={index}&rootSourceId={source_id}"
  43. return source_id, f"pages/category?jumpPage={urllib.parse.quote(url, safe='')}"
  44. return None, None
  45. async def saveSourceToDB(db_client, params, request_id):
  46. """
  47. 将sourceId信息存储到数据库
  48. :param request_id: 请求id
  49. :param db_client:
  50. :param params:
  51. :return:
  52. """
  53. # input 信息
  54. try:
  55. # 视频信息
  56. video_id = params['videoId']
  57. video_cover = params['videoCover']
  58. video_title = params['videoTitle']
  59. uid = params['uid']
  60. # 公众号信息
  61. account_name = params['accountName']
  62. gh_id = params['ghId']
  63. # 文章信息
  64. article_title = params['articleTitle']
  65. # 业务信息
  66. business_type = params['businessType']
  67. position = params['position']
  68. cooperation = params['cooperation']
  69. except Exception as e:
  70. response = {
  71. "error": str(e),
  72. "message": "params error",
  73. "requestId": request_id
  74. }
  75. return response
  76. # source_id, gzh_path
  77. source_id, share_path = createPath(video_id, uid, gh_id, business_type, position)
  78. # minigram_info
  79. minigram_name = "祝福岁岁平安"
  80. minigram_id = "wxe54dd94c5246127a"
  81. sql = f"""
  82. INSERT INTO {source_id_db}
  83. (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)
  84. values
  85. (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s);
  86. """
  87. try:
  88. await db_client.asyncInsert(
  89. sql=sql,
  90. params=(
  91. video_id,
  92. video_cover,
  93. video_title,
  94. uid,
  95. gh_id,
  96. account_name,
  97. article_title,
  98. business_type,
  99. cooperation,
  100. position,
  101. source_id,
  102. share_path,
  103. datetime.datetime.now().__str__(),
  104. minigram_name,
  105. minigram_id
  106. )
  107. )
  108. response = {
  109. "requestId": request_id,
  110. "message": "success"
  111. }
  112. except Exception as e:
  113. response = {
  114. "requestId": request_id,
  115. "error": str(e),
  116. "info": "db error"
  117. }
  118. return response