lawyer_assitance / functions /get_express_delivery_information.py
qgyd2021's picture
[update]add express delivery check
2886d2a
raw
history blame
5.71 kB
#!/usr/bin/python3
# -*- coding: utf-8 -*-
import json
import re
import time
import requests
def get_operator_by_track_id_baidu(track_id: str):
url = "https://alayn.baidu.com/express/appdetail/get_com"
params = {
"num": track_id
}
resp = requests.request(
"GET",
url=url,
params=params
)
if resp.status_code == 200:
match = re.search(r"\{.*\}", resp.text, flags=re.IGNORECASE)
if match is not None:
json_str = match.group(0)
js = json.loads(json_str)
message = js["message"]
if message == "fail":
return None
else:
company = js["data"]["company"]
return company
return None
def get_operator_by_track_id(track_id: str):
url = "https://trace.fkdex.com/auto/{}".format(track_id)
headers = {
# 不给出 Referer 时,它会生成随机的物流信息。
"Referer": "https://www.ickd.cn/",
}
params = {
"mailNo": track_id,
}
resp = requests.request(
"GET",
url=url,
headers=headers,
params=params
)
print(resp.status_code)
print(resp.text)
if resp.status_code == 200:
js = resp.json()
exp_text_name = js["expTextName"]
exp_spell_name = js["expSpellName"]
message = js["message"]
return exp_spell_name
return resp.text
def get_express_delivery_information(track_id: str, four_digits: str = None, captcha: str = None):
"""
如果函数的名字叫作 “get_express_delivery_information” ,
那么就需要一个同名 python 脚本 “get_express_delivery_information.py” ,
同时其中应包含一个名为 “get_express_delivery_information” 的函数。
https://www.ickd.cn/
:param track_id: str, egs: 433542074086264.
:param four_digits: str, 收件人或寄件人手机号码后四位. egs: 4154.
:param captcha: str, egs: b2Nbm.
:return: info: str.
"""
operator = get_operator_by_track_id(track_id)
if four_digits is None:
url = "https://trace.fkdex.com/{}/{}".format(operator, track_id)
else:
url = "https://trace.fkdex.com/{}/{}:{}".format(operator, track_id, four_digits)
headers = {
# 不给出 Referer 时,它会生成随机的物流信息。
"Referer": "https://www.ickd.cn/",
"Cookie": "PHPSESSID=95c179f9fa0c117c31a8e3f440257d24"
}
params = {
"mailNo": "{}:{}".format(track_id, four_digits),
"verifyCode": captcha
}
resp = requests.request(
"GET",
url=url,
headers=headers,
params=params
)
print(resp.status_code)
print(resp.text)
if resp.status_code == 200:
js = resp.json()
error_code = js["errCode"]
message = js["message"]
exp_text_name = js["expTextName"]
exp_spell_name = js["expSpellName"]
data = js["data"]
if error_code == 1:
# 请输入正确的单号
return message
elif error_code == 2:
# 请输入【收件人】或【寄件人】\n电话号码后4位
return message
elif error_code == 14:
# 需要验证码
message_ = """需要验证码: 请输入此链接中展示的图像中的验证码。\nhttps://trace.fkdex.com/captcha"""
return message_
track_info = ""
for row in data:
date = row["time"]
context = row["context"]
row_ = "{}: {}".format(date, context)
track_info += row_
track_info += "\n"
result = "快递公司名称: {}\n快递单号: {}\n手机号码后四位: {}\n物流信息如下: \n\n{}".format(
exp_text_name, track_id, four_digits, track_info
)
print(result)
return result
return "物流信息查询失败。"
def kwargs():
return ["track_id", "four_digits", "captcha"]
def main():
tools = [
{
"type": "function",
"function": {
"name": "get_express_delivery_information",
"description": "在此根据跟踪号查询物流信息。",
"parameters": {
"type": "object",
"properties": {
"track_id": {
"type": "string",
"description": "物流跟踪号码。",
},
"four_digits": {
"type": "string",
"description": "出于对用户隐私的考虑,有些物流商可能会要求你提供收件人或寄件人手机号码后四位。",
},
"captcha": {
"type": "string",
"description": "有些物流商的查询可能要求你提供图像验证码。",
}
},
"required": ["track_id"],
},
},
}
]
tools_ = json.dumps(tools, ensure_ascii=False)
print(tools_)
print(tools_.replace("\"", "\\\""))
# track_id = "33212432323"
# four_digits = None
# track_id = "433542074086264"
# four_digits = "4154"
# four_digits = None
track_id = "4577035024"
four_digits = None
captcha = "VWDYW"
# result = get_operator_by_track_id(track_id)
# print(result)
result = get_express_delivery_information(track_id, four_digits, captcha)
print(result)
return
if __name__ == '__main__':
main()