xinshiquan.py 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. import os
  2. import random
  3. import sys
  4. import time
  5. import uuid
  6. import json
  7. from datetime import datetime
  8. import requests
  9. from application.common.feishu import FsData
  10. from application.common.feishu.feishu_utils import FeishuUtils
  11. from application.common.gpt import GPT4oMini
  12. sys.path.append(os.getcwd())
  13. from application.items import VideoItem
  14. from application.pipeline import PiaoQuanPipeline
  15. from application.common.messageQueue import MQ
  16. from application.common.log import AliyunLogger
  17. from application.common.mysql import MysqlHelper
  18. class XSQRecommend(object):
  19. """
  20. 新视圈
  21. """
  22. def __init__(self, platform, mode, rule_dict, user_list, env="prod"):
  23. self.limit_flag = False
  24. self.platform = platform
  25. self.mode = mode
  26. self.rule_dict = rule_dict
  27. self.user_list = user_list
  28. self.env = env
  29. self.download_cnt = 0
  30. self.mq = MQ(topic_name="topic_crawler_etl_" + self.env)
  31. self.expire_flag = False
  32. self.aliyun_log = AliyunLogger(mode=self.mode, platform=self.platform)
  33. self.mysql = MysqlHelper(mode=self.mode, platform=self)
  34. def get_recommend_list(self):
  35. print("新视圈")
  36. """
  37. 获取推荐页视频
  38. """
  39. headers = {
  40. 'Content-Type': 'application/json'
  41. }
  42. url = "http://8.217.192.46:8889/crawler/xin_shi_quan/recommend"
  43. data_rule = FsData()
  44. title_rule = data_rule.get_title_rule()
  45. while True:
  46. payload = json.dumps({
  47. "cursor": ""
  48. })
  49. response = requests.request("POST", url, headers=headers, data=payload)
  50. response = response.json()
  51. if response['code'] != 0:
  52. self.aliyun_log.logging(
  53. code="3000",
  54. message="抓取单条视频失败,请求失败"
  55. ),
  56. return
  57. data = response['data']['data']
  58. if len(data) == 0:
  59. return
  60. for index, video_obj in enumerate(data, 1):
  61. try:
  62. self.aliyun_log.logging(
  63. code="1001", message="扫描到一条视频", data=video_obj
  64. )
  65. self.process_video_obj(video_obj,title_rule)
  66. except Exception as e:
  67. self.aliyun_log.logging(
  68. code="3000",
  69. message="抓取单条视频失败, 该视频位于第{}页第{}条报错原因是{}".format(
  70. 1, index, e
  71. ),
  72. )
  73. if self.limit_flag:
  74. return
  75. time.sleep(random.randint(1, 5))
  76. def process_video_obj(self, video_obj,title_rule):
  77. """
  78. 处理视频
  79. :param video_obj:
  80. """
  81. video_obj = video_obj['video_info']
  82. time.sleep(random.randint(3, 8))
  83. trace_id = self.platform + str(uuid.uuid1())
  84. our_user = random.choice(self.user_list)
  85. item = VideoItem()
  86. item.add_video_info("video_id", video_obj["id"])
  87. item.add_video_info("video_title", video_obj["title"])
  88. item.add_video_info("play_cnt", 0)
  89. item.add_video_info("publish_time_stamp", int(time.time()))
  90. item.add_video_info("out_user_id", video_obj["id"])
  91. item.add_video_info("cover_url", video_obj["cover_image"])
  92. item.add_video_info("like_cnt", 0)
  93. item.add_video_info("share_cnt", 0)
  94. item.add_video_info("comment_cnt", 0)
  95. item.add_video_info("video_url", video_obj['video_path'])
  96. item.add_video_info("out_video_id", video_obj["id"])
  97. item.add_video_info("platform", self.platform)
  98. item.add_video_info("strategy", self.mode)
  99. item.add_video_info("session", "{}-{}".format(self.platform, int(time.time())))
  100. item.add_video_info("user_id", our_user["uid"])
  101. item.add_video_info("user_name", our_user["nick_name"])
  102. mq_obj = item.produce_item()
  103. pipeline = PiaoQuanPipeline(
  104. platform=self.platform,
  105. mode=self.mode,
  106. rule_dict=self.rule_dict,
  107. env=self.env,
  108. item=mq_obj,
  109. trace_id=trace_id,
  110. )
  111. if pipeline.process_item():
  112. title_list = title_rule.split(",")
  113. title = video_obj["title"]
  114. contains_keyword = any(keyword in title for keyword in title_list)
  115. if contains_keyword:
  116. new_title = GPT4oMini.get_ai_mini_title(title)
  117. if new_title:
  118. item.add_video_info("video_title", new_title)
  119. current_time = datetime.now()
  120. formatted_time = current_time.strftime("%Y-%m-%d %H:%M:%S")
  121. values = [
  122. [
  123. video_obj['video_path'],
  124. video_obj["cover_image"],
  125. title,
  126. new_title,
  127. formatted_time,
  128. ]
  129. ]
  130. FeishuUtils.insert_columns("U5dXsSlPOhiNNCtEfgqcm1iYnpf", "8c7191", "ROWS", 1, 2)
  131. time.sleep(0.5)
  132. FeishuUtils.update_values("U5dXsSlPOhiNNCtEfgqcm1iYnpf", "8c7191", "A2:Z2", values)
  133. self.download_cnt += 1
  134. self.mq.send_msg(mq_obj)
  135. self.aliyun_log.logging(code="1002", message="成功发送至 ETL", data=mq_obj)
  136. if self.download_cnt >= int(
  137. self.rule_dict.get("videos_cnt", {}).get("min", 200)
  138. ):
  139. self.limit_flag = True
  140. def run(self):
  141. self.get_recommend_list()
  142. if __name__ == '__main__':
  143. J = XSQRecommend(
  144. platform="xinshiquan",
  145. mode="recommend",
  146. rule_dict={},
  147. user_list=[{'uid': "123456", 'nick_name': "xiaoxiao"}],
  148. )
  149. J.get_recommend_list()
  150. # J.logic()