process.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. class ProcessParams(object):
  14. """
  15. Params Analysis
  16. """
  17. @classmethod
  18. def get_params(cls, data):
  19. """
  20. "accountName": "公众号名称",
  21. "content": "文章正文",
  22. "title": "文章标题",
  23. "cover": "封面链接"
  24. :param data:
  25. :return: title
  26. """
  27. return data['title']
  28. @classmethod
  29. def ask_kimi_and_save_to_local(cls, title):
  30. """
  31. save file to local
  32. :param title:
  33. :return:
  34. """
  35. save_path = os.path.join(os.getcwd(), 'applications', 'static', "titles", "{}.json".format(title))
  36. if os.path.exists(save_path):
  37. return
  38. else:
  39. os.makedirs(os.path.dirname(save_path), exist_ok=True)
  40. if not title:
  41. result = "{}"
  42. else:
  43. result = ask_kimi(title)
  44. with open(save_path, "w", encoding="utf-8") as f:
  45. f.write(json.dumps(result, ensure_ascii=False))
  46. @classmethod
  47. def create_gzh_path(cls, video_id, shared_uid):
  48. """
  49. :param video_id: 视频 id
  50. :param shared_uid: 分享 id
  51. """
  52. root_share_id = str(uuid.uuid4())
  53. url = f"pages/user-videos?id={video_id}&su={shared_uid}&fromGzh=1&rootShareId={root_share_id}&shareId={root_share_id}"
  54. return root_share_id, f"pages/category?jumpPage={urllib.parse.quote(url)}"
  55. @classmethod
  56. def request_for_info(cls, video_id):
  57. """
  58. 请求数据
  59. :param video_id:
  60. :return:
  61. """
  62. url = "https://longvideoapi.piaoquantv.com/longvideoapi/openapi/video/batchSelectVideoInfo"
  63. data = {
  64. "videoIdList": [video_id]
  65. }
  66. header = {
  67. "Content-Type": "application/json",
  68. }
  69. response = requests.post(url, headers=header, data=json.dumps(data))
  70. return response.json()
  71. @classmethod
  72. def process(cls, data):
  73. """执行代码"""
  74. today = datetime.today()
  75. yesterday = today - timedelta(days=1)
  76. yesterday_str = yesterday.strftime("%Y%m%d")
  77. title = cls.get_params(data)
  78. title_p = os.path.join(os.getcwd(), 'applications', 'static', "titles", "{}.json".format(title))
  79. if os.path.exists(title_p):
  80. result = title_mix(title_p=title_p, dt=yesterday_str)
  81. else:
  82. cls.ask_kimi_and_save_to_local(title)
  83. result = title_mix(title_p=title_p, dt=yesterday_str)
  84. uid, video_id = result['s1_uid'], result['s1_vid']
  85. root_share_id, productionPath = cls.create_gzh_path(uid, video_id)
  86. response = cls.request_for_info(video_id)
  87. productionCover = response['data'][0]['shareImgPath']
  88. productionName = response["data"][0]['title']
  89. programAvatar = "http://0.0.0.0:8111/static/logo.png"
  90. programId = "wx89e7eb06478361d7"
  91. programName = "票圈vlog"
  92. source = "Web"
  93. result = {
  94. "productionCover": productionCover,
  95. "productionName": productionName,
  96. "programAvatar": programAvatar,
  97. "programId": programId,
  98. "programName": programName,
  99. "source": source,
  100. "rootShareId": root_share_id,
  101. "productionPath": productionPath
  102. }
  103. return result