|
@@ -58,57 +58,57 @@ class ZhongQingKanDianRelatedRecommend:
|
|
|
self.LocalLog = Local.logger(self.platform, self.mode)
|
|
|
self.session = requests.session()
|
|
|
|
|
|
- def send_request(self, path, data):
|
|
|
- """
|
|
|
- 同步发送 POST 请求到指定路径,带有重试机制。
|
|
|
- :param path: 请求的 API 路径
|
|
|
- :param data: 请求的数据
|
|
|
- :return: 响应的 JSON 数据,如果请求失败则返回 None
|
|
|
- """
|
|
|
- full_url = f"{self.API_BASE_URL}{path}"
|
|
|
+ def __del__(self):
|
|
|
+ if self.session:
|
|
|
+ self.LocalLog.info("session 被正确关闭")
|
|
|
+ self.session.close()
|
|
|
|
|
|
- for retry in range(self.MAX_RETRIES):
|
|
|
+ def send_request(self, path, data):
|
|
|
+ """发送带重试机制的API请求"""
|
|
|
+ for attempt in range(self.MAX_RETRIES):
|
|
|
try:
|
|
|
- response = self.session.post(full_url, data=data, timeout=self.TIMEOUT, headers=self.COMMON_HEADERS)
|
|
|
- response.raise_for_status()
|
|
|
- self.LocalLog.info(f"{path}响应数据:{response.json()}")
|
|
|
- return response.json()
|
|
|
+ response = self.session.post(
|
|
|
+ f"{self.API_BASE_URL}{path}",
|
|
|
+ data=data,
|
|
|
+ timeout=self.TIMEOUT,
|
|
|
+ headers=self.COMMON_HEADERS
|
|
|
+ )
|
|
|
+ resp_data = response.json()
|
|
|
+ # 检查响应格式
|
|
|
+ if 'code' not in resp_data:
|
|
|
+ self.LocalLog.warning(f"{path}响应缺少code字段,尝试重试")
|
|
|
+ raise ValueError("Missing 'code' in response")
|
|
|
+
|
|
|
+ code = resp_data['code']
|
|
|
+
|
|
|
+ # 成功情况 (code=0)
|
|
|
+ if code == 0:
|
|
|
+ self.LocalLog.info(f"{path}请求成功:{resp_data}")
|
|
|
+ return resp_data
|
|
|
+
|
|
|
+ # 特定错误码不重试
|
|
|
+ if code == 29036:
|
|
|
+ self.LocalLog.warning(f"{path}返回code:29036,消息:{resp_data}")
|
|
|
+ return None
|
|
|
+
|
|
|
+ # 其他错误码重试
|
|
|
+ self.LocalLog.warning(f"{path}返回错误码{code},尝试重试,响应内容:{resp_data}")
|
|
|
+
|
|
|
except Exception as e:
|
|
|
tb_info = traceback.format_exc()
|
|
|
- self.LocalLog.info(f"{path}请求失败:{e} \n{tb_info}")
|
|
|
- self.aliyun_log.logging(
|
|
|
- code="3000",
|
|
|
- message=f"请求 {path} 失败,错误信息: {str(e)}",
|
|
|
- data={"path": path}
|
|
|
- )
|
|
|
- time.sleep(5)
|
|
|
+ self.LocalLog.error(f"{path}请求异常: {str(e)} \n {tb_info}")
|
|
|
+
|
|
|
+ time.sleep(random.randint(5,10))
|
|
|
+
|
|
|
+ # 所有重试失败,记录错误并返回None
|
|
|
+ self.LocalLog.error(f"{path}达到最大重试次数")
|
|
|
+ self.aliyun_log.logging(
|
|
|
+ code="3000",
|
|
|
+ message=f"请求 {path} 失败,达到最大重试次数",
|
|
|
+ data=data
|
|
|
+ )
|
|
|
return None
|
|
|
|
|
|
- def is_response_valid(self, resp, url):
|
|
|
- """
|
|
|
- 检查响应是否有效(状态码为 0 表示有效)。
|
|
|
- :param resp: 响应数据
|
|
|
- :param url: 请求的 URL
|
|
|
- :return: 如果响应有效则返回响应数据,否则返回 None
|
|
|
- """
|
|
|
- try:
|
|
|
- if resp and resp.get('code') != 0:
|
|
|
- self.aliyun_log.logging(
|
|
|
- code="3000",
|
|
|
- message=f"抓取{url}失败,请求失败,响应:{resp}"
|
|
|
- )
|
|
|
- self.LocalLog.info(f"{url}请求失败,响应:{resp}")
|
|
|
- return None
|
|
|
- return resp
|
|
|
- except Exception as e:
|
|
|
- tb_info = traceback.format_exc()
|
|
|
- self.aliyun_log.logging(
|
|
|
- code="3000",
|
|
|
- message=f"检查响应有效性时出错,错误信息: {str(e)}",
|
|
|
- data={"url": url, "resp": resp}
|
|
|
- )
|
|
|
- self.LocalLog.info(f"检查 {url} 响应有效性时出错:{e} \n{tb_info}")
|
|
|
- return None
|
|
|
|
|
|
def req_related_recommend_list(self, content_id):
|
|
|
"""
|
|
@@ -124,8 +124,7 @@ class ZhongQingKanDianRelatedRecommend:
|
|
|
"cursor": ""
|
|
|
})
|
|
|
self.LocalLog.info(f"开始请求相关推荐{body}")
|
|
|
- resp = self.send_request(url, body)
|
|
|
- return self.is_response_valid(resp, url)
|
|
|
+ return self.send_request(url, body)
|
|
|
except Exception as e:
|
|
|
tb_info = traceback.format_exc()
|
|
|
self.aliyun_log.logging(
|
|
@@ -150,7 +149,7 @@ class ZhongQingKanDianRelatedRecommend:
|
|
|
"content_link": content_link
|
|
|
})
|
|
|
resp = self.send_request(url, body)
|
|
|
- if not self.is_response_valid(resp, url):
|
|
|
+ if not resp:
|
|
|
return
|
|
|
data = resp.get("data", {}).get("data", {})
|
|
|
if data.get("content_type") != "video":
|
|
@@ -244,21 +243,6 @@ class ZhongQingKanDianRelatedRecommend:
|
|
|
account_avatar = video_obj["avatar"]
|
|
|
self.db_ops.insert_user(account_id, account_name, account_avatar)
|
|
|
self.LocalLog.info(f"用户{account_id}存入数据库")
|
|
|
- # 检查用户ID是否存在
|
|
|
- # """
|
|
|
- # 需要改为判断redis
|
|
|
- # """
|
|
|
- # is_repeat_user = self.db_ops.check_user_id(account_id)
|
|
|
- # if is_repeat_user:
|
|
|
- # # 更新用户信息,使用异步方法并等待结果
|
|
|
- # self.LocalLog.info(f"用户{account_id}已经存在数据库中")
|
|
|
- # self.db_ops.update_user(account_id, account_name, account_avatar)
|
|
|
- # else:
|
|
|
- # self.LocalLog.info(f"用户{account_id}没在数据库中")
|
|
|
- # # 插入用户信息,使用异步方法并等待结果
|
|
|
- # self.db_ops.insert_user(account_id, account_name, account_avatar)
|
|
|
- # self.aliyun_log.logging(code="1007", message=f"用户数据写入成功,用户ID:{account_id}")
|
|
|
- # self.LocalLog.info(f"用户数据写入成功,用户ID: {account_id}")
|
|
|
|
|
|
if video_duration > self.rule_dict.get("duration", {}).get("max", 1200) or video_duration < self.rule_dict.get("duration", {}).get("min", 30):
|
|
|
self.aliyun_log.logging(
|
|
@@ -296,7 +280,7 @@ class ZhongQingKanDianRelatedRecommend:
|
|
|
rule_dict=self.rule_dict,
|
|
|
env=self.env,
|
|
|
item=mq_obj,
|
|
|
- trace_id=traceback.format_exc()
|
|
|
+ trace_id=trace_id
|
|
|
)
|
|
|
if pipeline.process_item():
|
|
|
title_list = self.title_rule.split(",")
|
|
@@ -355,7 +339,7 @@ class ZhongQingKanDianRelatedRecommend:
|
|
|
if __name__ == '__main__':
|
|
|
ZhongQingKanDianRelatedRecommend(
|
|
|
platform="zhongqingkandianrelated",
|
|
|
- mode="related_recommend",
|
|
|
- rule_dict={"videos_cnt": {"min": 8, "max": 0}},
|
|
|
+ mode="related",
|
|
|
+ rule_dict={"videos_cnt": {"min": 200, "max": 0}},
|
|
|
user_list=[{"uid": 81525095, "link": "中青看点推荐", "nick_name": "善惡"}]
|
|
|
).run()
|