""" @author: luojunhui """ from __future__ import annotations import json import requests from tenacity import retry from applications import log from applications.utils import proxy, request_retry from .use_js import call_js_function retry_desc = request_retry(retry_times=3, min_retry_delay=2, max_retry_delay=30) @retry(**retry_desc) def get_associated_recommendation(article_id: str, cookie: str): """ toutiao related recommendation """ ms_token = "-aYwLj97uyCi3oghPfhz2nXaekLoFR5YnYUBA5SuyQZae_NLllO4zC30-CeVLth0A6Hmm7MuGr4_IN9MjHUn8wkq-UQKXJxoGmIAokpUsPsOLjdQKffe-cGWCiZ6xqgh7XE%3D" query_params = [ 0, 1, 14, "min_behot_time=0&channel_id=91558184576&category=pc_profile_channel&disable_raw_data=true&client_extra_params=%7B%22playparam%22%3A%22codec_type%3A0%2Cenable_dash%3A1%2Cunwatermark%3A1%22%2C%22group_id%22%3A%22{}%22%7D&aid=24&app_name=toutiao_web&msToken={}".format( article_id, ms_token, ms_token), "", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/133.0.0.0 Safari/537.36" ] a_bogus = call_js_function(query_params) url = f"https://www.toutiao.com/api/pc/list/feed?min_behot_time=0&channel_id=91558184576&category=pc_profile_channel&disable_raw_data=true&client_extra_params=%7B%22playparam%22%3A%22codec_type%3A0%2Cenable_dash%3A1%2Cunwatermark%3A1%22%2C%22group_id%22%3A%22{article_id}%22%7D&aid=24&app_name=toutiao_web&msToken={ms_token}&a_bogus={a_bogus}" headers = { 'accept': 'application/json, text/plain, */*', 'accept-language': 'zh', 'referer': 'https://www.toutiao.com/video/{}/'.format(article_id), 'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/133.0.0.0 Safari/537.36', 'Cookie': cookie } try: response = requests.get(url, headers=headers, proxies=proxy()) response.raise_for_status() return response.json() except requests.exceptions.RequestException as e: log( task="toutiao account crawler", function="get_toutiao_account_video_list", message=f"API请求失败: {e}", data={"account_id": article_id}, ) except json.JSONDecodeError as e: log( task="toutiao account crawler", function="get_toutiao_account_video_list", message=f"响应解析失败: {e}", data={"account_id": article_id}, ) return None