zhufuquanzituijianliu.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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 ZFQZTJLRecommend(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. headers = {
  47. 'Content-Type': 'application/json'
  48. }
  49. url = "http://47.236.68.175:8889/crawler/zhu_fu_quan_zi/recommend"
  50. payload = json.dumps({})
  51. response = requests.request("POST", url, headers=headers, data=payload)
  52. response = response.json()
  53. if response['code'] != 0:
  54. self.aliyun_log.logging(
  55. code="3000",
  56. message="抓取单条视频失败,请求失败"
  57. ),
  58. return
  59. for index, video_obj in enumerate(response['data']['data'], 1):
  60. try:
  61. self.aliyun_log.logging(
  62. code="1001", message="扫描到一条视频", data=video_obj
  63. )
  64. self.process_video_obj(video_obj)
  65. except Exception as e:
  66. self.aliyun_log.logging(
  67. code="3000",
  68. message="抓取单条视频失败, 该视频位于第{}页第{}条报错原因是{}".format(
  69. 1, index, e
  70. ),
  71. )
  72. if self.limit_flag:
  73. return
  74. time.sleep(random.randint(5, 10))
  75. def process_video_obj(self, video_obj):
  76. """
  77. 处理视频
  78. :param video_obj:
  79. """
  80. time.sleep(random.randint(3, 8))
  81. trace_id = self.platform + str(uuid.uuid1())
  82. our_user = random.choice(self.user_list)
  83. item = VideoItem()
  84. url = video_obj["v_url"]
  85. duration = self.get_video_duration(url)
  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", int(video_obj["play_pv"]))
  89. item.add_video_info("publish_time_stamp", int(int(video_obj["t"])/1000))
  90. item.add_video_info("out_user_id", video_obj["id"])
  91. item.add_video_info("cover_url", video_obj["url"])
  92. item.add_video_info("like_cnt", 0)
  93. item.add_video_info("share_cnt", int(video_obj["share"]))
  94. item.add_video_info("comment_cnt", int(video_obj["comment_count"]))
  95. item.add_video_info("video_url", video_obj["v_url"])
  96. item.add_video_info("out_video_id", video_obj["id"])
  97. item.add_video_info("duration", int(duration))
  98. item.add_video_info("platform", self.platform)
  99. item.add_video_info("strategy", self.mode)
  100. item.add_video_info("session", "{}-{}".format(self.platform, int(time.time())))
  101. item.add_video_info("user_id", our_user["uid"])
  102. item.add_video_info("user_name", our_user["nick_name"])
  103. mq_obj = item.produce_item()
  104. pipeline = PiaoQuanPipeline(
  105. platform=self.platform,
  106. mode=self.mode,
  107. rule_dict=self.rule_dict,
  108. env=self.env,
  109. item=mq_obj,
  110. trace_id=trace_id,
  111. )
  112. if pipeline.process_item():
  113. self.download_cnt += 1
  114. self.mq.send_msg(mq_obj)
  115. self.aliyun_log.logging(code="1002", message="成功发送至 ETL", data=mq_obj)
  116. if self.download_cnt >= int(
  117. self.rule_dict.get("videos_cnt", {}).get("min", 200)
  118. ):
  119. self.limit_flag = True
  120. def run(self):
  121. self.get_recommend_list()
  122. if __name__ == '__main__':
  123. J = ZFQZTJLRecommend(
  124. platform="zhufuquanzituijianliu",
  125. mode="recommend",
  126. rule_dict={},
  127. user_list=[{"uid": 75590470, "link": "zfqz推荐流_接口1", "nick_name": "做你的尾巴"}, {"uid": 75590471, "link": "zfqz推荐流_接口2", "nick_name": "能够相遇"}, {"uid": 75590472, "link": "zfqz推荐流_接口3", "nick_name": "一别两宽各生欢喜"}, {"uid": 75590473, "link": "zfqz推荐流_接口4", "nick_name": "惹火"}, {"uid": 75590475, "link": "zfqz推荐流_接口5", "nick_name": "顾九"}, {"uid": 75590476, "link": "zfqz推荐流_接口6", "nick_name": "宠一身脾气惯一身毛病"}],
  128. )
  129. J.get_recommend_list()
  130. # J.logic()