tiantianjufuqi.py 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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 requests
  9. from application.common import Feishu
  10. from application.common.feishu import FsData
  11. from application.common.feishu.feishu_utils import FeishuUtils
  12. from application.common.gpt import GPT4oMini
  13. sys.path.append(os.getcwd())
  14. from application.items import VideoItem
  15. from application.pipeline import PiaoQuanPipeline
  16. from application.common.messageQueue import MQ
  17. from application.common.proxies import tunnel_proxies
  18. from application.common.log import AliyunLogger
  19. from application.common.mysql import MysqlHelper
  20. class TTJFFQRecommend(object):
  21. """
  22. 福气矩阵-天天聚福气
  23. """
  24. def __init__(self, platform, mode, rule_dict, user_list, env="prod"):
  25. self.limit_flag = False
  26. self.platform = platform
  27. self.mode = mode
  28. self.rule_dict = rule_dict
  29. self.user_list = user_list
  30. self.env = env
  31. self.download_cnt = 0
  32. self.mq = MQ(topic_name="topic_crawler_etl_" + self.env)
  33. self.expire_flag = False
  34. self.aliyun_log = AliyunLogger(mode=self.mode, platform=self.platform)
  35. self.mysql = MysqlHelper(mode=self.mode, platform=self)
  36. def get_recommend_list(self):
  37. if self.expire_flag:
  38. self.aliyun_log.logging(
  39. code="2000",
  40. message="本轮已经抓取到足够的数据,自动退出\t{}".format(self.download_cnt),
  41. )
  42. return
  43. """
  44. 获取推荐页视频
  45. """
  46. headers = {
  47. 'Host': 'api.xinghetime.com',
  48. 'xweb_xhr': '1',
  49. '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.6(0x13080610) XWEB/1156',
  50. 'content-type': 'application/json',
  51. 'accept': '*/*',
  52. 'sec-fetch-site': 'cross-site',
  53. 'sec-fetch-mode': 'cors',
  54. 'sec-fetch-dest': 'empty',
  55. 'referer': 'https://servicewechat.com/wxa12a841184757478/7/page-frame.html',
  56. 'accept-language': 'zh-CN,zh;q=0.9'
  57. }
  58. data_rule = FsData()
  59. title_rule = data_rule.get_title_rule()
  60. while True:
  61. time.sleep(random.randint(1, 10))
  62. url = "https://api.xinghetime.com/luckvideo/video/getRecommendVideos"
  63. payload = json.dumps({
  64. "baseParam": {
  65. "mid": "openid_oeBeO4lCgcJCey9JEJm9ZHPnOln8",
  66. "pageSource": "video-home",
  67. "appType": 2
  68. },
  69. "bizParam": {
  70. "pageSize": 10
  71. }
  72. })
  73. response = requests.request("POST", url, headers=headers, data=payload)
  74. for index, video_obj in enumerate(response.json()['data'], 1):
  75. try:
  76. self.aliyun_log.logging(
  77. code="1001", message="扫描到一条视频", data=video_obj
  78. )
  79. self.process_video_obj(video_obj,title_rule)
  80. except Exception as e:
  81. self.aliyun_log.logging(
  82. code="3000",
  83. message="抓取单条视频失败,第{}条报错原因是{}".format(
  84. index, e
  85. ),
  86. )
  87. if self.limit_flag:
  88. return
  89. time.sleep(random.randint(5, 10))
  90. def process_video_obj(self, video_obj, title_rule):
  91. """
  92. 处理视频
  93. :param video_obj:
  94. """
  95. time.sleep(random.randint(3, 8))
  96. trace_id = self.platform + str(uuid.uuid1())
  97. our_user = random.choice(self.user_list)
  98. play_str = video_obj["playCountFormat"]
  99. play_cnt = int(play_str.replace("+", "").replace("次播放", ""))
  100. item = VideoItem()
  101. item.add_video_info("video_id", video_obj["videoId"])
  102. item.add_video_info("video_title", video_obj["title"])
  103. item.add_video_info("play_cnt", play_cnt)
  104. item.add_video_info("publish_time_stamp", int(time.time()))
  105. item.add_video_info("out_user_id", video_obj["videoId"])
  106. item.add_video_info("cover_url", video_obj["coverImagePath"])
  107. item.add_video_info("like_cnt", 0)
  108. item.add_video_info("video_url", video_obj["videoPath"])
  109. item.add_video_info("out_video_id", video_obj["videoId"])
  110. item.add_video_info("platform", self.platform)
  111. item.add_video_info("strategy", self.mode)
  112. item.add_video_info("session", "{}-{}".format(self.platform, int(time.time())))
  113. item.add_video_info("user_id", our_user["uid"])
  114. item.add_video_info("user_name", our_user["nick_name"])
  115. # 获取当前时间
  116. current_time = datetime.now()
  117. formatted_time = current_time.strftime("%Y-%m-%d %H:%M:%S")
  118. values = [[
  119. video_obj["videoId"],
  120. formatted_time,
  121. video_obj["title"],
  122. video_obj["coverImagePath"],
  123. video_obj["videoPath"],
  124. play_cnt
  125. ]]
  126. Feishu.insert_columns(self.platform, 'fuxiaoshun', "MZrtFR", "ROWS", 1, 2)
  127. time.sleep(0.5)
  128. Feishu.update_values(self.platform, 'fuxiaoshun', "MZrtFR", "A2:Z2", values)
  129. mq_obj = item.produce_item()
  130. pipeline = PiaoQuanPipeline(
  131. platform=self.platform,
  132. mode=self.mode,
  133. rule_dict=self.rule_dict,
  134. env=self.env,
  135. item=mq_obj,
  136. trace_id=trace_id,
  137. )
  138. if pipeline.process_item():
  139. title_list = title_rule.split(",")
  140. title = video_obj["title"]
  141. contains_keyword = any(keyword in title for keyword in title_list)
  142. if contains_keyword:
  143. new_title = GPT4oMini.get_ai_mini_title(title)
  144. if new_title:
  145. item.add_video_info("video_title", new_title)
  146. current_time = datetime.now()
  147. formatted_time = current_time.strftime("%Y-%m-%d %H:%M:%S")
  148. values = [
  149. [
  150. video_obj["videoPath"],
  151. video_obj["coverImagePath"],
  152. title,
  153. new_title,
  154. formatted_time,
  155. ]
  156. ]
  157. FeishuUtils.insert_columns("U5dXsSlPOhiNNCtEfgqcm1iYnpf", "ftQdRy", "ROWS", 1, 2)
  158. time.sleep(0.5)
  159. FeishuUtils.update_values("U5dXsSlPOhiNNCtEfgqcm1iYnpf", "ftQdRy", "A2:Z2", values)
  160. self.download_cnt += 1
  161. self.mq.send_msg(mq_obj)
  162. self.aliyun_log.logging(code="1002", message="成功发送至 ETL", data=mq_obj)
  163. if self.download_cnt >= int(
  164. self.rule_dict.get("videos_cnt", {}).get("min", 200)
  165. ):
  166. self.limit_flag = True
  167. def run(self):
  168. self.get_recommend_list()
  169. if __name__ == '__main__':
  170. J = TTJFFQRecommend(
  171. platform="tiantianjufuqi",
  172. mode="recommend",
  173. rule_dict={},
  174. user_list=[{'uid': "123456", 'nick_name': "xiaoxiao"}],
  175. )
  176. J.get_recommend_list()