benshanzhufu.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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 cv2
  9. import requests
  10. from application.common import Feishu
  11. sys.path.append(os.getcwd())
  12. from application.items import VideoItem
  13. from application.pipeline import PiaoQuanPipeline
  14. from application.common.messageQueue import MQ
  15. from application.common.log import AliyunLogger
  16. from application.common.mysql import MysqlHelper
  17. class BSZHRecommend(object):
  18. """
  19. 本山祝福推荐流
  20. """
  21. def __init__(self, platform, mode, rule_dict, user_list, env="prod"):
  22. self.limit_flag = False
  23. self.platform = platform
  24. self.mode = mode
  25. self.rule_dict = rule_dict
  26. self.user_list = user_list
  27. self.env = env
  28. self.download_cnt = 0
  29. self.mq = MQ(topic_name="topic_crawler_etl_" + self.env)
  30. self.expire_flag = False
  31. self.aliyun_log = AliyunLogger(mode=self.mode, platform=self.platform)
  32. self.mysql = MysqlHelper(mode=self.mode, platform=self)
  33. def get_video_duration(self, video_link: str) -> int:
  34. cap = cv2.VideoCapture(video_link)
  35. if cap.isOpened():
  36. rate = cap.get(5)
  37. frame_num = cap.get(7)
  38. duration = int(frame_num / rate)
  39. return duration
  40. return 0
  41. def get_recommend_list(self):
  42. print("本山祝福开始")
  43. """
  44. 获取推荐页视频
  45. """
  46. url = "http://8.217.192.46:8889/crawler/ben_shan_zhu_fu/recommend"
  47. next_cursor = 1
  48. for i in range(1, 200):
  49. payload = json.dumps({
  50. "cursor": f"{next_cursor}"
  51. })
  52. headers = {
  53. 'Content-Type': 'application/json'
  54. }
  55. for j in range(3):
  56. response = requests.request("POST", url, headers=headers, data=payload)
  57. response = response.json()
  58. if response['code'] != 0:
  59. time.sleep(2)
  60. continue
  61. else:
  62. break
  63. if response['code'] != 0:
  64. self.aliyun_log.logging(
  65. code="3000",
  66. message="抓取单条视频失败,请求失败"
  67. ),
  68. return
  69. for index, video_obj in enumerate(response['data']['data'], i):
  70. try:
  71. self.aliyun_log.logging(
  72. code="1001", message="扫描到一条视频", data=video_obj
  73. )
  74. next_cursor = response['data']['next_cursor']
  75. self.process_video_obj(video_obj)
  76. except Exception as e:
  77. self.aliyun_log.logging(
  78. code="3000",
  79. message="抓取单条视频失败, 该视频位于第{}页第{}条报错原因是{}".format(
  80. i, index, e
  81. ),
  82. )
  83. if self.limit_flag:
  84. return
  85. time.sleep(random.randint(5, 10))
  86. def process_video_obj(self, video_obj):
  87. """
  88. 处理视频
  89. :param video_obj:
  90. """
  91. time.sleep(random.randint(3, 8))
  92. trace_id = self.platform + str(uuid.uuid1())
  93. our_user = random.choice(self.user_list)
  94. item = VideoItem()
  95. # id = uuid.uuid4()
  96. item.add_video_info("video_id", video_obj["nid"])
  97. item.add_video_info("video_title", video_obj["title"])
  98. item.add_video_info("play_cnt", 0)
  99. item.add_video_info("publish_time_stamp", int(video_obj["update_time"]))
  100. item.add_video_info("out_user_id", video_obj["nid"])
  101. item.add_video_info("cover_url", video_obj["video_cover"])
  102. item.add_video_info("like_cnt", 0)
  103. item.add_video_info("video_url", video_obj["video_url"])
  104. item.add_video_info("out_video_id", video_obj["nid"])
  105. item.add_video_info("platform", self.platform)
  106. item.add_video_info("strategy", self.mode)
  107. item.add_video_info("session", "{}-{}".format(self.platform, int(time.time())))
  108. item.add_video_info("user_id", our_user["uid"])
  109. item.add_video_info("user_name", our_user["nick_name"])
  110. mq_obj = item.produce_item()
  111. pipeline = PiaoQuanPipeline(
  112. platform=self.platform,
  113. mode=self.mode,
  114. rule_dict=self.rule_dict,
  115. env=self.env,
  116. item=mq_obj,
  117. trace_id=trace_id,
  118. )
  119. if pipeline.process_item():
  120. self.download_cnt += 1
  121. self.mq.send_msg(mq_obj)
  122. self.aliyun_log.logging(code="1002", message="成功发送至 ETL", data=mq_obj)
  123. if self.download_cnt >= int(
  124. self.rule_dict.get("videos_cnt", {}).get("min", 200)
  125. ):
  126. self.limit_flag = True
  127. def run(self):
  128. self.get_recommend_list()
  129. if __name__ == '__main__':
  130. J = BSZHRecommend(
  131. platform="benshanzhufu",
  132. mode="recommend",
  133. rule_dict={},
  134. user_list=[{
  135. "createTime": 1684311893899,
  136. "id": 8,
  137. "interval": 7200,
  138. "machine": "aliyun",
  139. "mode": "recommend",
  140. "operator": "王雪珂",
  141. "rule": "[{\"like_cnt\":{\"min\":0,\"max\":0}}]",
  142. "source": "benshanzhufu",
  143. "spiderName": "run_bszf_recommend",
  144. "startTime": 1730452800000,
  145. "status": 0,
  146. "taskName": "本山祝福",
  147. "updateTime": 1730452617817
  148. }],
  149. )
  150. J.get_recommend_list()
  151. # J.logic()