# -*- coding:utf-8 -*-
import
json
import
time
import
hashlib
import
requests
from
Crypto.PublicKey
import
RSA
from
Crypto.Cipher
import
PKCS1_v1_5
from
base64
import
b64decode
from
base64
import
b64encode
proxy
=
{
'http'
:
'http://139.155.48.55:1089'
,
'https'
:
'http://139.155.48.55:1089'
}
def
get_nonce():
"""
:return: nonce的值
"""
t
=
time.time()
data_time
=
int
(
round
(t
*
1000
))
# 毫秒级时间戳
url
=
"https://passport.ximalaya.com/mobile/nonce/"
+
str
(data_time)
header
=
{
"Cookie"
:
"1&_device=android&59683601-c043-33af-95d4-413ba0788573&9.0.22;channel=Avril22;impl=com.ximalaya.ting.android;osversion=29;fp=009314627x2222q2v644v0500b0000022002000000000000200000000000;device_model=MI+8;XIM=;c-oper=%E6%9C%AA%E7%9F%A5;net-mode=WIFI;freeFlowType=0;res=1080%2C2029;AID=n+Ew49Cv/1Y=;manufacturer=Xiaomi;XD=NSRvWEdehdyqqdzAdbPbJbOfbPFruUAJe3HFBaJ0z438KV7oI/Q/czqtjV2TmpUvQtc0VKk72doYCGDVyUUkqe+kAr9y9XC5FdiYPvv8e66Wf7z4JEulczsJmwNd27g/;umid=0e42f21fc3f4e8704728805785e97255od;xm_grade=0;minorProtectionStatus=0;oaid=5d3cfedc6c1bd5aa;domain=.ximalaya.com;path=/;"
,
"Cookie2"
:
"$version=1"
,
"Accept"
:
"*/*"
,
"user-agent"
:
"ting_9.0.22(MI+8,Android29)"
,
"x-xuid"
:
"78c5c143-aa52-3f77-b3b6-fb4d0c289c3a"
,
"Host"
:
"passport.ximalaya.com"
,
"Connection"
:
"Keep-Alive"
,
"Accept-Encoding"
:
"gzip"
}
req
=
requests.get(url, headers
=
header)
print
(
"nonce响应结果 = "
,req.text)
nonce
=
json.loads(req.text)
nonce
=
nonce[
"nonce"
]
print
(
'nonce = '
, nonce)
return
nonce
def
get_fdsOtp():
"""
:return: fdsOtp的值
"""
t
=
time.time()
data_time
=
int
(
round
(t
*
1000
))
# 毫秒级时间戳
get_token_url
=
"https://mobile.ximalaya.com/captcha-web/check/slide/get?bpId=139&sessionId=59683601-c043-33af-95d4-413ba0788573"
+
str
(data_time)
+
"&requestType=xmClient"
get_token_header
=
{
"Cookie"
:
"1&_device=android&59683601-c043-33af-95d4-413ba0788573&9.0.22;channel=Avril22;impl=com.ximalaya.ting.android;osversion=29;fp=009314627x2222q2v644v0500b0000022002000000000000200000000000;device_model=MI+8;XIM=;c-oper=%E6%9C%AA%E7%9F%A5;net-mode=WIFI;freeFlowType=0;res=1080%2C2029;AID=n+Ew49Cv/1Y=;manufacturer=Xiaomi;XD=NSRvWEdehdyqqdzAdbPbJbOfbPFruUAJe3HFBaJ0z438KV7oI/Q/czqtjV2TmpUvQtc0VKk72doYCGDVyUUkqe+kAr9y9XC5FdiYPvv8e66Wf7z4JEulczsJmwNd27g/;umid=0e42f21fc3f4e8704728805785e97255od;xm_grade=0;minorProtectionStatus=0;oaid=5d3cfedc6c1bd5aa;domain=.ximalaya.com;path=/;"
,
"Cookie2"
:
"$version=1"
,
"Accept"
:
"*/*"
,
"user-agent"
:
"ting_9.0.22(MI+8,Android29)"
,
"x-xuid"
:
"78c5c143-aa52-3f77-b3b6-fb4d0c289c3a"
,
"Accept-Encoding"
:
"gzip"
}
req
=
requests.get(get_token_url, headers
=
get_token_header)
print
(
"fdsOtp响应结果 = "
,req.text)
fdsOtp
=
json.loads(req.text)
fdsOtp
=
fdsOtp[
'token'
]
print
(
"fdsOtp = "
, fdsOtp)
return
fdsOtp
def
sha1_secret_str(s):
"""
:param s: 需要加密的字符串
:return: 做完sha1的结果
"""
sha
=
hashlib.sha1(s.encode(
'utf-8'
))
encrypts
=
sha.hexdigest()
return
encrypts
def
login(account,password,fdsOtp,nonce):
"""
:param account: 用户名
:param password: 密码
:param fdsOtp: fdsOtp
:param nonce: nonce
:return:
"""
post_url
=
"https://passport.ximalaya.com/mobile/login/pwd/v3"
post_header
=
{
"Cookie"
:
"1&_device=android&59683601-c043-33af-95d4-413ba0788573&9.0.22;channel=Avril22;impl=com.ximalaya.ting.android;osversion=29;fp=009314627x2222q2v644v0500b0000022002000000000000200000000000;device_model=MI+8;XIM=;c-oper=%E6%9C%AA%E7%9F%A5;net-mode=WIFI;freeFlowType=0;res=1080%2C2029;AID=n+Ew49Cv/1Y=;manufacturer=Xiaomi;XD=NSRvWEdehdyqqdzAdbPbJbOfbPFruUAJe3HFBaJ0z438KV7oI/Q/czqtjV2TmpUvQtc0VKk72doYCGDVyUUkqRWBD1AcqgIlS9o5aEQyWw97unfTMyod0vYnavI0Tm/o;umid=0e42f21fc3f4e8704728805785e97255od;xm_grade=0;minorProtectionStatus=0;oaid=5d3cfedc6c1bd5aa;domain=.ximalaya.com;path=/;"
,
"Cookie2"
:
"$version=1"
,
"Accept"
:
"*/*"
,
"user-agent"
:
"ting_9.0.22(MI+8,Android29)"
,
"x-xuid"
:
"78c5c143-aa52-3f77-b3b6-fb4d0c289c3a"
,
"Host"
:
"passport.ximalaya.com"
,
"Content-Type"
:
"application/json; charset=utf-8"
,
"Accept-Encoding"
:
"gzip"
,
"Connection"
:
"Keep-Alive"
}
signature
=
"ACCOUNT="
+
account.upper()
+
"&FDSOTP="
+
fdsOtp
+
"&NONCE="
+
nonce.upper()
+
"&PASSWORD="
+
password.upper()
+
"&MOBILE-V1-PRODUCT-7D74899B338B4F348E2383970CC09991E8E8D8F2BC744EF0BEE94D76D718C089"
print
(
"signature = "
, signature)
print
(
"signature sha1 ="
, sha1_secret_str(signature.upper()))
signature_sha1
=
sha1_secret_str(signature)
post_dict
=
{
"password"
: password,
"fdsOtp"
: fdsOtp,
"signature"
: signature_sha1,
"nonce"
: nonce,
"account"
: account
}
print
(
"Body = "
,post_dict)
req
=
requests.post(post_url, data
=
json.dumps(post_dict).encode(
"utf-8"
), headers
=
post_header)
print
(
"登录结果 = "
,req.text)
def
get_end_username(account):
"""
:param account: 没有组成官方格式的用户名
:return: 按照官方格式组成的加密结果
"""
account_1
=
account[:
76
]
account_2
=
account[
76
:
152
]
account_3
=
account[
152
:]
print
(
"account = "
,account)
# print("account_1 = ",account_1)
# print("account_2 = ",account_2)
# print("account_3 = ",account_3)
return
account_1
+
"\n"
+
account_2
+
"\n"
+
account_3
def
get_end_password(password):
"""
:param password: 没有组成官方格式的密码
:return: 按照官方格式组成的加密结果
"""
password_1
=
password[:
76
]
password_2
=
password[
76
:
152
]
password_3
=
password[
152
:]
print
(
"password = "
, password)
# print("password_1 = ", password_1)
# print("password_2 = ", password_2)
# print("password_3 = ", password_3)
return
password_1
+
"\n"
+
password_2
+
"\n"
+
password_3
public_key
=
"MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCVhaR3Or7suUlwHUl2Ly36uVmboZ3+HhovogDjLgRE9CbaUokS2eqGaVFfbxAUxFThNDuXq/fBD+SdUgppmcZrIw4HMMP4AtE2qJJQH/KxPWmbXH7Lv+9CisNtPYOlvWJ/GHRqf9x3TBKjjeJ2CjuVxlPBDX63+Ecil2JR9klVawIDAQAB"
;
def
rsa_encrypt(s):
key
=
b64decode(public_key)
# print(key)
key
=
RSA.importKey(key)
cipher
=
PKCS1_v1_5.new(key)
ciphertext
=
b64encode(cipher.encrypt(bytes(s,
"utf-8"
)))
return
ciphertext
def
main(event, context):
# 获取fdsOtp
fdsOtp
=
get_fdsOtp()
# 获取nonce
nonce
=
get_nonce()
# 账号密码加密
account
=
rsa_encrypt(
"xxxxxx"
).decode()
password
=
rsa_encrypt(
"xxxxxx"
).decode()
# # 重组account
# account = get_end_username(account)
# # password
# password = get_end_password(password)
# print(account)
# print(password)
# 登录
login(account,password,fdsOtp,nonce)
if
__name__
=
=
'__main__'
:
main('
','
')