jierizhufuhuakaifugui.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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 JRZFHKFGRecommend(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/jie_ri_zhu_fu_hua_kai_fu_gui/recommend"
  42. next_cursor = None
  43. while True:
  44. payload = json.dumps({
  45. "cursor": next_cursor
  46. })
  47. response = requests.request("POST", url, headers=headers, data=payload)
  48. response = response.json()
  49. if response['code'] != 0:
  50. self.aliyun_log.logging(
  51. code="3000",
  52. message="抓取单条视频失败,请求失败"
  53. ),
  54. return
  55. next_cursor = response['data']['next_cursor']
  56. data = response['data']['data']
  57. if len(data) == 0:
  58. return
  59. for index, video_obj in enumerate(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(1, 5))
  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. item.add_video_info("video_id", video_obj["id"])
  85. item.add_video_info("video_title", video_obj["title"])
  86. item.add_video_info("play_cnt", 0)
  87. item.add_video_info("publish_time_stamp", int(time.time()))
  88. item.add_video_info("out_user_id", video_obj["id"])
  89. item.add_video_info("cover_url", video_obj["video_cover"])
  90. item.add_video_info("like_cnt", 0)
  91. item.add_video_info("share_cnt", 0)
  92. item.add_video_info("comment_cnt", 0)
  93. item.add_video_info("video_url", video_obj["video_url"])
  94. item.add_video_info("out_video_id", video_obj["id"])
  95. item.add_video_info("platform", self.platform)
  96. item.add_video_info("strategy", self.mode)
  97. item.add_video_info("session", "{}-{}".format(self.platform, int(time.time())))
  98. item.add_video_info("user_id", our_user["uid"])
  99. item.add_video_info("user_name", our_user["nick_name"])
  100. mq_obj = item.produce_item()
  101. pipeline = PiaoQuanPipeline(
  102. platform=self.platform,
  103. mode=self.mode,
  104. rule_dict=self.rule_dict,
  105. env=self.env,
  106. item=mq_obj,
  107. trace_id=trace_id,
  108. )
  109. if pipeline.process_item():
  110. self.download_cnt += 1
  111. self.mq.send_msg(mq_obj)
  112. self.aliyun_log.logging(code="1002", message="成功发送至 ETL", data=mq_obj)
  113. if self.download_cnt >= int(
  114. self.rule_dict.get("videos_cnt", {}).get("min", 200)
  115. ):
  116. self.limit_flag = True
  117. def run(self):
  118. self.get_recommend_list()
  119. if __name__ == '__main__':
  120. J = JRZFHKFGRecommend(
  121. platform="jierizhufuhuakaifugui",
  122. mode="recommend",
  123. rule_dict={},
  124. user_list=[{'uid': "123456", 'nick_name': "xiaoxiao"}],
  125. )
  126. J.get_recommend_list()
  127. # J.logic()