haoyunzhufuduo.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. import os
  2. import random
  3. import sys
  4. import time
  5. import uuid
  6. import json
  7. import cv2
  8. import requests
  9. from application.common.mysql.sql import Sql
  10. from application.common.redis.xng_redis import xng_in_video_data
  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 HYZFDfRecommend(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_recommend_list(self):
  34. print("好运祝福多开始")
  35. """
  36. 获取推荐页视频
  37. """
  38. headers = {
  39. 'Content-Type': 'application/json'
  40. }
  41. url = "http://8.217.192.46:8889/crawler/hao_yun_zhu_fu_duo/recommend"
  42. while True:
  43. payload = json.dumps({
  44. "cursor": ""
  45. })
  46. response = requests.request("POST", url, headers=headers, data=payload)
  47. response = response.json()
  48. if response['code'] != 0:
  49. self.aliyun_log.logging(
  50. code="3000",
  51. message="抓取单条视频失败,请求失败"
  52. ),
  53. return
  54. data = response['data']['data']
  55. if len(data) == 0:
  56. return
  57. for index, video_obj in enumerate(data, 1):
  58. try:
  59. self.aliyun_log.logging(
  60. code="1001", message="扫描到一条视频", data=video_obj
  61. )
  62. self.process_video_obj(video_obj)
  63. except Exception as e:
  64. self.aliyun_log.logging(
  65. code="3000",
  66. message="抓取单条视频失败, 该视频位于第{}页第{}条报错原因是{}".format(
  67. 1, index, e
  68. ),
  69. )
  70. if self.limit_flag:
  71. return
  72. time.sleep(random.randint(1, 5))
  73. def process_video_obj(self, video_obj):
  74. """
  75. 处理视频
  76. :param video_obj:
  77. """
  78. time.sleep(random.randint(3, 8))
  79. trace_id = self.platform + str(uuid.uuid1())
  80. our_user = random.choice(self.user_list)
  81. item = VideoItem()
  82. item.add_video_info("video_id", video_obj["videoId"])
  83. item.add_video_info("video_title", video_obj["title"])
  84. item.add_video_info("play_cnt", 0)
  85. item.add_video_info("publish_time_stamp", int(time.time()))
  86. item.add_video_info("out_user_id", video_obj["videoId"])
  87. item.add_video_info("cover_url", video_obj["coverImagePath"])
  88. item.add_video_info("like_cnt", 0)
  89. item.add_video_info("share_cnt", 0)
  90. item.add_video_info("comment_cnt", 0)
  91. item.add_video_info("video_url", video_obj["videoPath"])
  92. item.add_video_info("out_video_id", video_obj["videoId"])
  93. item.add_video_info("platform", self.platform)
  94. item.add_video_info("strategy", self.mode)
  95. item.add_video_info("session", "{}-{}".format(self.platform, int(time.time())))
  96. item.add_video_info("user_id", our_user["uid"])
  97. item.add_video_info("user_name", our_user["nick_name"])
  98. mq_obj = item.produce_item()
  99. pipeline = PiaoQuanPipeline(
  100. platform=self.platform,
  101. mode=self.mode,
  102. rule_dict=self.rule_dict,
  103. env=self.env,
  104. item=mq_obj,
  105. trace_id=trace_id,
  106. )
  107. if pipeline.process_item():
  108. self.download_cnt += 1
  109. self.mq.send_msg(mq_obj)
  110. self.aliyun_log.logging(code="1002", message="成功发送至 ETL", data=mq_obj)
  111. if self.download_cnt >= int(
  112. self.rule_dict.get("videos_cnt", {}).get("min", 200)
  113. ):
  114. self.limit_flag = True
  115. def run(self):
  116. self.get_recommend_list()
  117. if __name__ == '__main__':
  118. J = HYZFDfRecommend(
  119. platform="haoyunzhufuduo",
  120. mode="recommend",
  121. rule_dict={},
  122. user_list=[{'uid': "123456", 'nick_name': "xiaoxiao"}],
  123. )
  124. J.get_recommend_list()
  125. # J.logic()