youlegaoxiaoxiaoshipin_scheduling.py 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. # -*- coding: utf-8 -*-
  2. # @Author: luojunhui
  3. # @Time: 2023/10/23
  4. import json
  5. import os
  6. import random
  7. import sys
  8. import time
  9. import requests
  10. sys.path.append(os.getcwd())
  11. from common.mq import MQ
  12. from common.aliyun_log import AliyunLogger
  13. from common.pipeline import PiaoQuanPipeline
  14. def clean_title(strings):
  15. return (
  16. strings.strip()
  17. .replace("\n", "")
  18. .replace("/", "")
  19. .replace("\r", "")
  20. .replace("#", "")
  21. .replace(".", "。")
  22. .replace("\\", "")
  23. .replace("&NBSP", "")
  24. .replace(":", "")
  25. .replace("*", "")
  26. .replace("?", "")
  27. .replace("?", "")
  28. .replace('"', "")
  29. .replace("<", "")
  30. .replace(">", "")
  31. .replace("|", "")
  32. .replace(" ", "")
  33. .replace('"', "")
  34. .replace("'", "")
  35. )
  36. class YLGXXSPScheduling:
  37. def __init__(self, log_type, crawler, rule_dict, env, our_uid):
  38. self.platform = "youlegaoxiaoxiaoshipin"
  39. self.log_type = log_type
  40. self.crawler = crawler
  41. self.rule_dict = rule_dict
  42. self.env = env
  43. self.our_uid = our_uid
  44. self.mq = MQ(topic_name="topic_crawler_etl_" + self.env)
  45. self.download_count = 0
  46. # 获取视频id_list
  47. def get_videoList(self, page_id):
  48. # time.sleep(random.randint(5, 10))
  49. headers = {
  50. "Host": "cpu.baidu.com",
  51. "xweb_xhr": "1",
  52. "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36 MicroMessenger/6.8.0(0x16080000) NetType/WIFI MiniProgramEnv/Mac MacWechat/WMPF MacWechat/3.8.4(0x13080410)XWEB/31009",
  53. "Accept": "*/*",
  54. "Sec-Fetch-Site": "cross-site",
  55. "Sec-Fetch-Mode": "cors",
  56. "Sec-Fetch-Dest": "empty",
  57. "Referer": "https://servicewechat.com/wx38382a240eab7214/4/page-frame.html",
  58. "Accept-Language": "en-US,en;q=0.9",
  59. }
  60. data = {
  61. "channelId": "1033",
  62. "needHybrid": "1",
  63. "pageNo": str(page_id),
  64. "pageSize": "10",
  65. }
  66. response = requests.post(
  67. "https://cpu.baidu.com/1033/a16a67fe",
  68. headers=headers,
  69. data=data
  70. )
  71. result = response.json()
  72. if "data" not in result or response.status_code != 200:
  73. # Common.logger(self.log_type, self.crawler).info(
  74. # f"get_videoList:{response.text}\n"
  75. # )
  76. # Common.logging(
  77. # self.log_type,
  78. # self.crawler,
  79. # self.env,
  80. # f"get_videoList:{response.text}\n",
  81. # )
  82. return
  83. elif len(result["data"]['result']) == 0:
  84. # Common.logger(self.log_type, self.crawler).info(f"没有更多数据啦~\n")
  85. # Common.logging(self.log_type, self.crawler, self.env, f"没有更多数据啦~\n")
  86. return
  87. else:
  88. data_list = result['data']["result"]
  89. for video_obj in data_list:
  90. print(1)
  91. AliyunLogger.logging(
  92. code="1001",
  93. platform=self.crawler,
  94. mode=self.log_type,
  95. env=self.env,
  96. data={},
  97. message="成功扫描到一条视频"
  98. )
  99. self.process_video_obj(video_obj)
  100. # try:
  101. # AliyunLogger.logging(
  102. # code="1001",
  103. # platform=self.crawler,
  104. # mode=self.log_type,
  105. # env=self.env,
  106. # data="",
  107. # message="成功扫描到一条视频"
  108. # )
  109. # self.process_video_obj(video_obj)
  110. # except Exception as e:
  111. # Common.logger(self.log_type, self.crawler).error(f"抓取单条视频异常:{e}\n")
  112. # Common.logging(
  113. # self.log_type, self.crawler, self.env, f"抓取单条视频异常:{e}\n"
  114. # )
  115. def process_video_obj(self, video_obj):
  116. video_id = video_obj.get("data", {}).get("id", 0)
  117. video_title = clean_title(video_obj.get("data", {}).get("title", "no title"))
  118. video_time = video_obj['data']['duration']
  119. publish_time_stamp = int(video_obj['data']['clusterTime'])
  120. publish_time_str = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(publish_time_stamp))
  121. user_name = video_obj['data']['source']
  122. video_dict = {
  123. "video_title": video_title,
  124. "video_id": video_id,
  125. "duration": video_time,
  126. "play_cnt": int(video_obj['data'].get("playbackCount", 0)),
  127. "like_cnt": int(video_obj.get("likeCount", 0)),
  128. "comment_cnt": int(video_obj.get("commentCounts", 0)),
  129. "share_cnt": 0,
  130. "user_name": user_name,
  131. "publish_time_stamp": publish_time_stamp,
  132. "publish_time_str": publish_time_str,
  133. "video_width": 0,
  134. "video_height": 0,
  135. "profile_id": 0,
  136. "profile_mid": 0,
  137. "session": f"youlegaoxiaoxiaoshipin-{int(time.time())}",
  138. }
  139. flag = PiaoQuanPipeline(
  140. platform=self.crawler,
  141. mode=self.log_type,
  142. rule_dict=self.rule_dict,
  143. env=self.env,
  144. item=video_dict
  145. )
  146. if flag:
  147. video_dict["out_user_id"] = video_obj['data'].get("ownerId", 0)
  148. video_dict["platform"] = self.crawler
  149. video_dict["strategy"] = self.log_type
  150. video_dict["out_video_id"] = str(video_dict["video_id"])
  151. video_dict["width"] = video_dict["video_width"]
  152. video_dict["height"] = video_dict["video_height"]
  153. video_dict["crawler_rule"] = json.dumps(self.rule_dict)
  154. video_dict["user_id"] = self.our_uid
  155. video_dict["publish_time"] = video_dict["publish_time_str"]
  156. video_dict["video_url"] = "http:" + video_obj['data']['url']
  157. video_dict["avatar_url"] = "http:" + video_obj['data']['avatar']
  158. video_dict["cover_url"] = "http:" + video_obj['data']['thumbUrl']
  159. print(json.dumps(video_dict, ensure_ascii=False, indent=4))
  160. self.download_count += 1
  161. # self.mq.send_msg(video_dict)
  162. if __name__ == "__main__":
  163. ZL = YLGXXSPScheduling(
  164. log_type="recommend",
  165. crawler="ylgxxsp",
  166. rule_dict={},
  167. our_uid="luojunhuihaoshuai",
  168. env="prod"
  169. )
  170. for i in range(5):
  171. ZL.get_videoList(page_id=i + 1)
  172. print(ZL.download_count)