youlegaoxiaoxiaoshipin_scheduling.py 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. # -*- coding: utf-8 -*-
  2. # @Author: luojunhui
  3. # @Time: 2023/10/23
  4. import json
  5. import os
  6. import sys
  7. import time
  8. import uuid
  9. import random
  10. import requests
  11. sys.path.append(os.getcwd())
  12. from common.mq import MQ
  13. from common.aliyun_log import AliyunLogger
  14. from common.pipeline import PiaoQuanPipeline
  15. from common.public import clean_title
  16. class YLGXXSPScheduling:
  17. def __init__(self, platform, mode, rule_dict, env, our_uid_list):
  18. self.platform = platform
  19. self.mode = mode
  20. self.rule_dict = rule_dict
  21. self.env = env
  22. self.our_uid_list = our_uid_list
  23. self.mq = MQ(topic_name="topic_crawler_etl_" + self.env)
  24. self.download_count = 0
  25. # 获取视频id_list
  26. def get_videoList(self, page_id):
  27. time.sleep(random.randint(5, 10))
  28. AliyunLogger.logging(
  29. code="1000",
  30. platform=self.platform,
  31. mode=self.mode,
  32. env=self.env,
  33. data={},
  34. message="开始抓取第{}页".format(page_id),
  35. )
  36. headers = {
  37. "Host": "cpu.baidu.com",
  38. "xweb_xhr": "1",
  39. "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",
  40. "Accept": "*/*",
  41. "Sec-Fetch-Site": "cross-site",
  42. "Sec-Fetch-Mode": "cors",
  43. "Sec-Fetch-Dest": "empty",
  44. "Referer": "https://servicewechat.com/wx38382a240eab7214/4/page-frame.html",
  45. "Accept-Language": "en-US,en;q=0.9",
  46. }
  47. channel_id_dict = {
  48. '1059': "搞笑",
  49. "1058": "音乐",
  50. "1061": "娱乐",
  51. "1063": "社会",
  52. "1066": "生活",
  53. "1064": "猎奇"
  54. }
  55. for channel_id in channel_id_dict:
  56. data = {
  57. "channelId": channel_id,
  58. "needHybrid": "1",
  59. "pageNo": str(page_id),
  60. "pageSize": "10",
  61. }
  62. response = requests.post(
  63. "https://cpu.baidu.com/1033/a16a67fe", headers=headers, data=data
  64. )
  65. result = response.json()
  66. channel_name = channel_id_dict[channel_id]
  67. if "data" not in result or response.status_code != 200:
  68. AliyunLogger.logging(
  69. code="2000",
  70. platform=self.platform,
  71. mode=self.mode,
  72. env=self.env,
  73. data={},
  74. message="{}抓取第{}页失败,无数据".format(channel_name, page_id),
  75. )
  76. return
  77. elif len(result["data"]["result"]) == 0:
  78. AliyunLogger.logging(
  79. code="2001",
  80. platform=self.platform,
  81. mode=self.mode,
  82. env=self.env,
  83. data={},
  84. message="{}抓取d到第{}页, 没有更多数据了".format(channel_name, page_id),
  85. )
  86. return
  87. else:
  88. data_list = result["data"]["result"]
  89. for index, video_obj in enumerate(data_list):
  90. # AliyunLogger.logging(
  91. # code="1001",
  92. # platform=self.platform,
  93. # mode=self.mode,
  94. # env=self.env,
  95. # data={},
  96. # message="{}成功扫描到一条视频, 该视频位于第{}页{}条".format(channel_name, page_id, index + 1),
  97. # )
  98. # self.process_video_obj(video_obj)
  99. try:
  100. AliyunLogger.logging(
  101. code="1001",
  102. platform=self.platform,
  103. mode=self.mode,
  104. env=self.env,
  105. data={},
  106. message="{}成功扫描到一条视频, 该视频位于第{}页{}条".format(channel_name, page_id, index + 1),
  107. )
  108. self.process_video_obj(video_obj)
  109. except Exception as e:
  110. AliyunLogger.logging(
  111. code="3000",
  112. platform=self.platform,
  113. mode=self.mode,
  114. env=self.env,
  115. data=video_obj,
  116. message="{}抓取单条视频异常, 报错原因是: {}, 该视频位于第{}页{}条".format(
  117. channel_name, e, page_id, index + 1
  118. ),
  119. )
  120. AliyunLogger.logging(
  121. code="1000",
  122. platform=self.platform,
  123. mode=self.mode,
  124. env=self.env,
  125. data={},
  126. message="{}完成抓取第{}页".format(channel_name, page_id),
  127. )
  128. def process_video_obj(self, video_obj):
  129. trace_id = self.platform + str(uuid.uuid1())
  130. video_id = video_obj.get("data", {}).get("id", 0)
  131. video_title = clean_title(video_obj.get("data", {}).get("title", "no title"))
  132. video_time = video_obj["data"]["duration"]
  133. publish_time_stamp = int(video_obj["data"]["clusterTime"])
  134. publish_time_str = time.strftime(
  135. "%Y-%m-%d %H:%M:%S", time.localtime(publish_time_stamp)
  136. )
  137. user_name = video_obj["data"]["source"]
  138. video_dict = {
  139. "video_title": video_title,
  140. "video_id": video_id,
  141. "duration": video_time,
  142. "play_cnt": int(video_obj["data"].get("playbackCount", 0)),
  143. "like_cnt": int(video_obj.get("likeCount", 0)),
  144. "comment_cnt": int(video_obj.get("commentCounts", 0)),
  145. "share_cnt": 0,
  146. "user_name": user_name,
  147. "publish_time_stamp": publish_time_stamp,
  148. "publish_time_str": publish_time_str,
  149. "update_time_stamp": int(time.time()),
  150. "video_width": 0,
  151. "video_height": 0,
  152. "profile_id": 0,
  153. "profile_mid": 0,
  154. "out_video_id": video_id,
  155. "session": f"youlegaoxiaoxiaoshipin-{int(time.time())}",
  156. }
  157. rule_pipeline = PiaoQuanPipeline(
  158. platform=self.platform,
  159. mode=self.mode,
  160. rule_dict=self.rule_dict,
  161. env=self.env,
  162. item=video_dict,
  163. trace_id=trace_id
  164. )
  165. flag = rule_pipeline.process_item()
  166. if flag:
  167. video_dict["out_user_id"] = video_obj["data"].get("ownerId", 0)
  168. video_dict["platform"] = self.platform
  169. video_dict["strategy"] = self.mode
  170. video_dict["width"] = video_dict["video_width"]
  171. video_dict["height"] = video_dict["video_height"]
  172. video_dict["crawler_rule"] = json.dumps(self.rule_dict)
  173. video_dict["user_id"] = random.choice(self.our_uid_list)
  174. video_dict["publish_time"] = video_dict["publish_time_str"]
  175. video_dict["video_url"] = video_obj["data"]["url"]
  176. video_dict["avatar_url"] = "http:" + video_obj["data"]["avatar"]
  177. video_dict["cover_url"] = "http:" + video_obj["data"]["thumbUrl"]
  178. self.download_count += 1
  179. self.mq.send_msg(video_dict)
  180. # print(video_dict)
  181. AliyunLogger.logging(
  182. code="1002",
  183. platform=self.platform,
  184. mode=self.mode,
  185. env=self.env,
  186. data=video_dict,
  187. trace_id=trace_id,
  188. message="成功发送 MQ 至 ETL",
  189. )
  190. if __name__ == "__main__":
  191. ZL = YLGXXSPScheduling(
  192. platform="ylgxxsp",
  193. mode="recommend",
  194. rule_dict={},
  195. our_uid_list=["luojunhuihaoshuai"],
  196. env="prod",
  197. )
  198. for i in range(5):
  199. ZL.get_videoList(page_id=i + 1)
  200. print(ZL.download_count)