[Python] 纯文本查看 复制代码
import json
import re
import time
import urllib
import execjs
import requests
mobile = "这里填你的手机号"
pwd = '这里填你的密码'
deviceId = "这里填你的设备id" 格式:AAaAa1-aCC1aAAA1,自己抓包或者自己瞎编
headers = {
"Content-Type": "application/x-www-form-urlencoded",
"User-Agent": "Dalvik/2.1.0 (Linux; U; Android 14; 2211133C Build/UKQ1.230804.001) APP/xiaomi.vipaccount APPV/240123 MK/WGlhb21pIDEz "
"SDKV/5.1.0.release.18 PassportSDK/5.1.0.release.21 passport-ui/5.1.0.release.21",
"Host": "account.xiaomi.com",
"Connection": "Keep-Alive",
"Accept-Encoding": "gzip"
}
# 取13位时间戳
def timestamp():
return int(round(time.time() * 1000))
# 32位大写MD5加密
def md5(str):
import hashlib
m = hashlib.md5()
m.update(str.encode('utf-8'))
return m.hexdigest().upper()
# 正则取出{}中的内容
def get_json(text):
start = text.find('{')
end = text.rfind('}')
return text[start:end + 1]
# 登陆
def login():
cookies1 = {
"userId": mobile,
"deviceId": deviceId
}
url1 = "https://account.xiaomi.com/pass/serviceLogin"
params1 = {
"_json": "true",
"sid": "miui_vip",
"_locale": "zh_CN"
}
# print('第一次请求头:', headers)
response1 = requests.get(url1, headers=headers, cookies=cookies1, params=params1)
response1 = json.loads(get_json(response1.text))
# print('第一次响应值:', response1)
# 第一次请求获取qs和_sign
cookies2 = {
"deviceId": deviceId
}
url2 = "https://account.xiaomi.com/pass/serviceLoginAuth2"
data = {
"qs": response1['qs'],
# ?_json=true&sid=miui_vip&_locale=zh_CN
"callback": "https://api.vip.miui.com/sts",
"_json": "true",
"_sign": response1['_sign'],
"user": mobile,
"hash": md5(pwd), # 密码
"sid": "miui_vip",
"_locale": "zh_CN"
}
headers["Content-Length"] = "241"
# print('第二次请求头:', headers)
response2 = requests.post(url2, headers=headers, cookies=cookies2, data=data)
response2 = json.loads(get_json(response2.text))
# print('第二次响应值:', response2)
if '成功' in response2['desc']:
print('登陆成功,开始提取miui_vip所需参数......')
else:
print('登陆失败')
location = response2['location']
# 将location进行url解码
location = urllib.parse.unquote(location)
# 正则表达式模式来匹配auth, nonce, 和_ssign参数的值
auth_pattern = r"auth=([^&]+)"
nonce_pattern = r"nonce=([^&]+)"
ssign_pattern = r"_ssign=([^&]+)"
# 使用re.search找到匹配的字符串
auth_match = re.search(auth_pattern, location)
nonce_match = re.search(nonce_pattern, location)
ssign_match = re.search(ssign_pattern, location)
# 如果找到匹配项,提取参数值
auth_value = auth_match.group(1) if auth_match else None
nonce_value = nonce_match.group(1) if nonce_match else None
ssign_value = ssign_match.group(1) if ssign_match else None
# 打印提取的值
print("auth:", auth_value)
print("nonce:", nonce_value)
print("_ssign:", ssign_value)
print('数据提取完成,等待发起miui_vip请求......')
return response2['nonce'], response2['ssecurity'], auth_value, nonce_value, ssign_value
login_info = login()
with open(r"xiaomi-sha-1-base64.js") as f:
ctx = execjs.compile(f.read())
clientSign = ctx.call('sha', 'nonce=' + str(login_info[0]) + '&' + login_info[1])
def get_miui_vip():
cookies = {
"deviceId": deviceId
}
url = "https://api.vip.miui.com/sts"
params = {
"d": deviceId,
"ticket": "0",
"pwd": "1",
"p_ts": str(timestamp()),
"fid": "0",
"p_lm": "1",
"auth": login_info[2],
"m": "1",
"_group": "DEFAULT",
"tsl": "0",
"p_ca": "0",
"p_ur": "CN",
"p_idc": "China",
"nonce": login_info[3],
"_ssign": login_info[4],
"_userIdNeedEncrypt": "true",
"clientSign": clientSign
}
del headers['Content-Length']
headers['Host'] = 'api.vip.miui.com'
response = requests.get(url, headers=headers, cookies=cookies, params=params)
if response.json() == {'R': '', 'S': 'OK'}:
print('##################miui_vip数据获取成功##################')
else:
print('miui_vip数据获取失败')
# for i in response.cookies:
# print(i)
return response.cookies['cUserId'], response.cookies['miui_vip_ph'], response.cookies['miui_vip_serviceToken'], response.cookies['miui_vip_slh']
miui_vip_info = get_miui_vip()
print('cUserId:', miui_vip_info[0])
print('miui_vip_ph:', miui_vip_info[1])
print('miui_vip_serviceToken:', miui_vip_info[2])
print('miui_vip_slh:', miui_vip_info[3])
# 活动请求头
act_headers = {
"Host": "api.vip.miui.com",
"Connection": "keep-alive",
"Pragma": "no-cache",
"Cache-Control": "no-cache",
"sec-ch-ua": "\"Android WebView\";v=\"117\", \"Not;A=Brand\";v=\"8\", \"Chromium\";v=\"117\"",
"Accept": "application/json",
"Content-Type": "multipart/form-data; boundary=----WebKitFormBoundaryEW1J8DDHB8YZntmZ",
"sec-ch-ua-mobile": "?1",
"User-Agent": "Mozilla/5.0 (Linux; Android 14; 2211133C Build/UKQ1.230804.001; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/117.0.0.0 Mobile Safari/537.36XiaoMi/HybridView/ app/vipaccount/dev.240123",
"sec-ch-ua-platform": "\"Android\"",
"Origin": "https://web.vip.miui.com",
"X-Requested-With": "com.xiaomi.vipaccount",
"Sec-Fetch-Site": "same-site",
"Sec-Fetch-Mode": "cors",
"Sec-Fetch-Dest": "empty",
"Referer": "https://web.vip.miui.com/",
"Accept-Encoding": "gzip, deflate, br",
"Accept-Language": "zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7"
}
# 活动cookies
act_cookies = {
"cUserId": miui_vip_info[0],
"miui_vip_ph": miui_vip_info[1],
"miui_vip_slh": miui_vip_info[3],
"miui_vip_serviceToken": miui_vip_info[2]
}
# 每日任务—拔萝卜
def Pluck_turnips():
print('开始每日任务拔萝卜......')
url = "https://api.vip.miui.com/api/carrot/pull"
params = {
"miui_vip_ph": miui_vip_info[1]
}
headers = act_headers
headers["Content-Length"] = '166'
data = {
"------WebKitFormBoundarytBideCUbNWgxfi9W\nContent-Disposition: form-data; name": "\"miui_vip_ph\"\n\n" + miui_vip_info[
1] + "\n------WebKitFormBoundarytBideCUbNWgxfi9W--\n"
}
response = requests.post(url, headers=headers, cookies=act_cookies, params=params, data=data).json()
print(response)
# 每日任务-浏览帖子10s
def BROWSE_POST_10S():
url = "https://api.vip.miui.com/mtop/planet/vip/member/addCommunityGrowUpPointByActionV2"
params = {
"miui_vip_ph": miui_vip_info[1]
}
headers = act_headers
headers['Content-Length'] = '274'
data = {
"------WebKitFormBoundaryEW1J8DDHB8YZntmZ\nContent-Disposition: form-data; name": "\"action\"\n\nBROWSE_POST_10S\n------WebKitFormBoundaryEW1J8DDHB8YZntmZ\nContent-Disposition: form-data; name=\"miui_vip_ph\"\n\n" +
miui_vip_info[
1] + "\n------WebKitFormBoundaryEW1J8DDHB8YZntmZ--\n"
}
response = requests.post(url, headers=headers, cookies=act_cookies, params=params, data=data).json()
print(response)
BROWSE_POST_10S()
Pluck_turnips()