PythonSDK
更新时间:2024-02-22
1、数据接入
1.1、物料数据接入
import os
import time
import requests
import json
def post_m(dic):
ctx = json.dumps(dic, ensure_ascii=False)
ctx = ctx.encode("utf-8")
rex = requests.post(url, ctx, headers=headers)
return rex
url = "https://airec.baidu.com/airec/api/omni/cms/batch-sync"
ak = "323475515d4bfccc1af3f3676ec0256d"
headers = {"Content-Type": "application/json", "X-Gw-Ak": ak, "Alias-Name": "in9iamvk-26"}
demo = [{
"nid": "nid_1",
"site_name": "mr.yesterday",
"title": "杂诗-王维-1",
"content": "双燕初命子,五桃新作花, 来日绮窗前,寒梅著花未。",
"publish_time": 1648561924764,
"status": 1,
"tags": ["杂诗王维"],
# 如果需要频道推荐则使用此字段
"channels": ["科技"],
"ts": int(time.time() * 1000)
}]
begin = time.time()
rex = post_m(demo)
end = time.time()
print("using time: {}, status {}, ret content: {}".format(round(end - begin, 6), rex.status_code, rex.text))
1.2、用户数据接入
import time
import requests
import json
def post_m(dic):
ctx = json.dumps(dic, ensure_ascii=False)
ctx = ctx.encode("utf-8")
rex = requests.post(url, ctx, headers=headers)
return rex
url = "https://airec.baidu.com/airec/api/omni/ums/batch-sync"
ak = "323475515d4bfccc1af3f3676ec0256d"
headers = {"Content-Type": "application/json", "X-Gw-Ak": ak, "Alias-Name": "in9iamvk-26"}
demo = [{
"uid": f"8-b8xvi89a_1",
"city": "北京",
"uid_type": "1",
"tags": ["杂诗王维"]
}]
begin = time.time()
rex = post_m(demo)
end = time.time()
print("using time: {}, status {}, ret content: {}".format(round(end - begin, 6), rex.status_code, rex.text))
1.3、行为数据接入
import time
import requests
import json
def post_m(dic):
ctx = json.dumps(dic, ensure_ascii=False)
ctx = ctx.encode("utf-8")
rex = requests.post(url, ctx, headers=headers)
return rex
url = "https://airec.baidu.com/airec/api/omni/ims/batch-sync"
ak = "323475515d4bfccc1af3f3676ec0256d"
headers = {"Content-Type": "application/json", "X-Gw-Ak": ak, "Alias-Name": "in9iamvk-26"}
demo = [{
"uid": "uid_1",
"nid": "nid_1",
"event": "show",
"traceid": "232808585383426618326892988534043392144",
"ts": int(time.time() * 1000)
}]
print("data:", json.dumps(demo, ensure_ascii=False))
begin = time.time()
rex = post_m(demo)
end = time.time()
print("using time: {}, status {}, ret content: {}".format(round(end - begin, 6), rex.status_code, rex.text))
2、服务调用
2.1、个性化推荐接口
import os
import time
import requests
import json
def post_m(dic):
rex = requests.post(url, json=dic, headers=headers)
return rex
url = "https://airec.baidu.com/airec/api/rec/p_rec"
ak = "323475515d4bfccc1af3f3676ec0256d"
headers = {"Content-Type": "application/json", "X-Gw-Ak": ak, "Alias-Name": "in9iamvk-26"}
body = {
"uid": "uid_1",
# 如果使用频道推荐, 则传入此字段, 同时确保物料中已经传入此字段
# "channels": "tv,科技",
"req_cnt": 20
}
begin = time.time()
rex = post_m(body)
end = time.time()
print("using time: {}, status {}, ret content: {}".format(round(end - begin, 6), rex.status_code, rex.text))
2.2、相关推荐
import os
import time
import requests
import json
def post_m(dic):
rex = requests.post(url, json=dic, headers=headers)
return rex
url = "https://airec.baidu.com/airec/api/rec/r_rec"
ak = "323475515d4bfccc1af3f3676ec0256d"
headers = {"Content-Type": "application/json", "X-Gw-Ak": ak, "Alias-Name": "in9iamvk-26"}
body = {
"uid": "uid_1",
"nids": "nid_1",
"req_cnt": 20
}
begin = time.time()
rex = post_m(body)
end = time.time()
print("using time: {}, status {}, ret content: {}".format(round(end - begin, 6), rex.status_code, rex.text))
2.3、智能搜索
import os
import time
import requests
import json
def post_m(dic):
rex = requests.post(url, json=dic, headers=headers)
return rex
url = "https://airec.baidu.com/airec/api/search/main"
ak = "323475515d4bfccc1af3f3676ec0256d"
headers = {"Content-Type": "application/json", "X-Gw-Ak": ak, "Alias-Name": "in9iamvk-26"}
body = {
"uid": "uid_1",
"query": "王维"
}
begin = time.time()
rex = post_m(body)
end = time.time()
print("using time: {}, status {}, ret content: {}".format(round(end - begin, 6), rex.status_code, rex.text))