12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364 |
- """
- @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_toutiao_account_video_list(
- account_id: str, cookie: str, max_behot_time=0
- ) -> dict | None:
- """
- get toutiao account video list
- :param account_id: toutiao account id
- :param cookie: cookie maybe expire not quite sure
- :param max_behot_time: max behot time
- :return: toutiao account video list
- """
- ms_token = "mFs9gU4FJc23gFWPvBfQxFsBRrx1xBEJD_ZRTAolHfPrae84kTEBaHQR3s8ToiLX4-U9hgATTZ2cVHlSixmj5YCTOPoVM-43gOt3aVHkxfXHEuUtTJe-wUEs%3D"
- query_params = [
- 0,
- 1,
- 14,
- "category=pc_user_hot&token={}&aid=24&app_name=toutiao_web&msToken={}".format(
- account_id, 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/user/feed?category=pc_profile_video&token={account_id}&max_behot_time={max_behot_time}&hot_video=0&entrance_gid=&aid=24&app_name=toutiao_web&msToken={ms_token}&a_bogus={a_bogus}"
- headers = {
- "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3",
- "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": account_id},
- )
- except json.JSONDecodeError as e:
- log(
- task="toutiao account crawler",
- function="get_toutiao_account_video_list",
- message=f"响应解析失败: {e}",
- data={"account_id": account_id},
- )
- return None
|