Spaces:
Running
Running
File size: 5,714 Bytes
2886d2a |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 |
#!/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()
|