import json
from urllib.parse import urlencode
import requests
def call_api_web_server(req_type, req_data):
headers = {
"Accept": "application/json, text/plain, */*",
"Accept-Language": "zh-CN,zh;q=0.9",
"Cache-Control": "no-cache",
"Connection": "keep-alive",
"Content-Type": "application/json;charset=UTF-8",
"Pragma": "no-cache",
}
api_url = "https://api.addcoder.com/" + req_type
req_params = {"api_auth_key": "601137664d318ffde7c095381db55434"}
response = requests.post(
api_url,
headers=headers,
params=req_params,
data=json.dumps(req_data, separators=(",", ":")),
timeout=20,
)
if response.json().get("respCode") == 0:
return response.json()["respData"]
else:
print(
"[api]-[error]-[%s]-[%s]"
% (response.json().get("respMsg"), response.json().get("error"))
)
if __name__ == "__main__":
# GET
params = {"category_id": "", "app_name": "aweme", "num": "50", "date": "20210814"}
info = {
"url": "https://trendinsight.oceanengine.com/api/open/brand/get_brand_index_weekly?"
+ urlencode(params),
"method": "GET",
}
respData = call_api_web_server("insight_trend_xhr", info)
req_url = respData["data"]
print(req_url)
# POST
data = {
"keyword_list": ["小米"],
"start_date": "20200905",
"end_date": "20210905",
"app_name": "toutiao",
}
info = {
"url": "https://trendinsight.oceanengine.com/api/open/index/get_multi_keyword_hot_trend",
"method": "POST",
"data": json.dumps(data, separators=(",", ":")),
}
respData = call_api_web_server("insight_trend_xhr", info)
req_url = respData["data"]
print(req_url)