预览效果
有道ai写作python版
import hashlib
import time
import json
import ssl
import base64
import uuid
from urllib. parse import quote
import requests
from requests_toolbelt. multipart. encoder import MultipartEncoder
from Crypto. Cipher import AES
from Crypto. Util. Padding import pad, unpad
from sseclient import SSEClient
yduuid= str ( uuid. uuid4( ) ) . replace( '-' , '' )
session = requests. session( )
lastModified = requests. get( 'https://fanyi.youdao.com/index.html' ) . headers[ 'last-Modified' ]
_nlmf = int ( time. mktime( time. strptime( lastModified, "%a, %d %b %Y %H:%M:%S GMT" ) ) )
session. headers = {
'Referer' : 'https://fanyi.youdao.com/' ,
'User-Agent' : 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36' ,
}
session. get( f'https://rlogs.youdao.com/rlog.php?_npid=fanyiweb&_ncat=pageview&_ncoo=935962676.0432019&_nssn=NULL&_nver=1.2.0&_ntms= {
time. time( ) } &_nref=&_nurl=https%3A%2F%2Ffanyi.youdao.com%2Findex.html%23%2F&_nres=1920x1080&_nlmf= {
_nlmf} &_njve=0&_nchr=utf-8&_nfrg=%2F&/=NULL&screen=1920*1080' )
def get_md5 ( s, is_hex= True ) :
md5= hashlib. md5( )
md5. update( s. encode( ) )
if is_hex:
return md5. hexdigest( )
return md5. digest( )
def decrypt ( encrypted_data, key, iv) :
cipher = AES. new( key, AES. MODE_CBC, iv)
decrypted_data = cipher. decrypt( base64. b64decode( encrypted_data) )
return decrypted_data. decode( 'utf-8' )
def get_token_and_key ( ) :
params = {
'product' : 'webdict' ,
'appVersion' : '1.0' ,
'client' : 'web' ,
'mid' : '1' ,
'vendor' : 'web' ,
'screen' : '1' ,
'model' : '1' ,
'imei' : '1' ,
'network' : 'wired' ,
'keyfrom' : 'webdict' ,
'keyid' : 'ai-write' ,
'mysticTime' : str ( int ( time. time( ) * 1000 ) ) ,
'yduuid' : yduuid,
}
sorted_params = sorted ( params. items( ) , key= lambda x: x[ 0 ] )
sorted_params. append( ( "key" , "xuiC95RuooxC8Q51UJtdod1plLUhdAmt" ) )
encoded_params = '&' . join( [ f" {
key} = {
value} " for key, value in sorted_params] )
print ( "encoded_params:::::" + encoded_params)
sign= get_md5( encoded_params)
params. update( {
"sign" : sign} )
params. update( {
"pointParam" : "appVersion,client,imei,keyfrom,keyid,mid,model,mysticTime,network,product,screen,vendor,yduuid,key" } )
response = session. get( 'https://luna-ai.youdao.com/write/ai/web/ktf' , params= params, verify= False )
print ( response. text)
e= response. text. replace( "-" , "+" ) . replace( "_" , "/" )
key= get_md5( "IIS0fzL@zGv1^zO2%NcZiTcM=98WYxdun$CJE1KUKFfEhz&zpEC0fdXfvGqy*N!r" , is_hex= False )
iv= get_md5( "kG_oSxAXx#xjbiOKAkf9915Ko-XclOBP-GSgfo6E9ZB%9WvgcY6Y7dmje!+m5g#d" , is_hex= False )
s= decrypt( e, key, iv) . replace( '\x02' , '' )
print ( "解密数据::::" + s)
decode_data= json. loads( json. loads( s) )
return decode_data
def get_result ( query) :
decode_data= get_token_and_key( )
params = {
'product' : 'webdict' ,
'appVersion' : '1.0' ,
'client' :