process.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. """
  2. @author: luojunhui
  3. 对请求进行操作
  4. """
  5. import os
  6. import json
  7. import uuid
  8. import requests
  9. import urllib.parse
  10. from datetime import datetime, timedelta
  11. from applications.functions.ask_kimi import ask_kimi
  12. from applications.functions.calculate import title_mix
  13. from applications.functions.auto_white import auto_white
  14. class ProcessParams(object):
  15. """
  16. Params Analysis
  17. """
  18. @classmethod
  19. def get_params(cls, data):
  20. """
  21. "accountName": "公众号名称",
  22. "content": "文章正文",
  23. "title": "文章标题",
  24. "cover": "封面链接"
  25. :param data:
  26. :return: title
  27. """
  28. return data['title']
  29. @classmethod
  30. def ask_kimi_and_save_to_local(cls, title):
  31. """
  32. save file to local
  33. :param title:
  34. :return:
  35. """
  36. save_path = os.path.join(os.getcwd(), 'applications', 'static', "titles", "{}.json".format(title))
  37. if os.path.exists(save_path):
  38. return
  39. else:
  40. os.makedirs(os.path.dirname(save_path), exist_ok=True)
  41. if not title:
  42. result = "{}"
  43. else:
  44. result = ask_kimi(title)
  45. with open(save_path, "w", encoding="utf-8") as f:
  46. f.write(json.dumps(result, ensure_ascii=False))
  47. @classmethod
  48. def create_gzh_path(cls, video_id, shared_uid):
  49. """
  50. :param video_id: 视频 id
  51. :param shared_uid: 分享 id
  52. """
  53. root_share_id = str(uuid.uuid4())
  54. url = f"pages/user-videos?id={video_id}&su={shared_uid}&fromGzh=1&rootShareId={root_share_id}&shareId={root_share_id}"
  55. # 自动把 root_share_id 加入到白名单
  56. auto_white(root_share_id)
  57. return root_share_id, f"pages/category?jumpPage={urllib.parse.quote(url)}"
  58. @classmethod
  59. def request_for_info(cls, video_id):
  60. """
  61. 请求数据
  62. :param video_id:
  63. :return:
  64. """
  65. url = "https://longvideoapi.piaoquantv.com/longvideoapi/openapi/video/batchSelectVideoInfo"
  66. data = {
  67. "videoIdList": [video_id]
  68. }
  69. header = {
  70. "Content-Type": "application/json",
  71. }
  72. response = requests.post(url, headers=header, data=json.dumps(data))
  73. return response.json()
  74. @classmethod
  75. def choose_video(cls, result):
  76. """
  77. :param result: 计算出来的结果
  78. :return: uid, video_id
  79. """
  80. # 判断 score
  81. score1, score2 = result['s1_score'], result['s2_score']
  82. if score1 == 0 and score2 == 0:
  83. return None, None
  84. elif score1 == 0 and score2 > 0:
  85. return result['s2_uid'], result['s2_vid']
  86. elif score1 > 0 and score2 == 0:
  87. return result['s1_uid'], result['s1_vid']
  88. elif score1 > 0 and score2 > 0:
  89. return result['s1_uid'], result['s1_vid']
  90. @classmethod
  91. def process(cls, data):
  92. """执行代码"""
  93. today = datetime.today()
  94. yesterday = today - timedelta(days=1)
  95. yesterday_str = yesterday.strftime("%Y%m%d")
  96. title = cls.get_params(data)
  97. title_p = os.path.join(os.getcwd(), 'applications', 'static', "titles", "{}.json".format(title))
  98. if os.path.exists(title_p):
  99. result = title_mix(title_p=title_p, dt=yesterday_str)
  100. else:
  101. cls.ask_kimi_and_save_to_local(title)
  102. result = title_mix(title_p=title_p, dt=yesterday_str)
  103. uid, video_id = cls.choose_video(result)
  104. if video_id and uid:
  105. root_share_id, productionPath = cls.create_gzh_path(uid, video_id)
  106. response = cls.request_for_info(video_id)
  107. productionCover = response['data'][0]['shareImgPath']
  108. productionName = response["data"][0]['title']
  109. programAvatar = "/static/logo.png"
  110. programId = "wx89e7eb06478361d7"
  111. programName = "票圈vlog"
  112. source = "Web"
  113. result = {
  114. "productionCover": productionCover,
  115. "productionName": productionName,
  116. "programAvatar": programAvatar,
  117. "programId": programId,
  118. "programName": programName,
  119. "source": source,
  120. "rootShareId": root_share_id,
  121. "productionPath": productionPath
  122. }
  123. else:
  124. result = {
  125. "productionCover": None,
  126. "productionName": None,
  127. "programAvatar": None,
  128. "programId": None,
  129. "programName": None,
  130. "source": None,
  131. "rootShareId": None,
  132. "productionPath": None
  133. }
  134. return result