调用有道翻译

有道翻译的salt计算中,需要从fanyi.mini.js中调整常量值


import requests
import hashlib
import time
import urllib.parse
import re
import os

# 创建md5对象
def nmd5(str):
    m = hashlib.md5()
    # Tips
    # 此处必须encode
    # 若写法为m.update(str)  报错为: Unicode-objects must be encoded before hashing
    # 因为python3里默认的str是unicode
    # 或者 b = bytes(str, encoding='utf-8'),作用相同,都是encode为bytes
    b = str.encode(encoding='utf-8')
    m.update(b)
    str_md5 = m.hexdigest()
    return  str_md5

def formdata(transtr):
    # 待加密信息
    headerstr = '5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Geco) Chrome/73.0.3683.86 Safari/537.36'
    #print(round(time.time()*1000))
    bv=nmd5(headerstr)
    ts=str(round(time.time()*1000))
    salt=ts+'90'
    strexample='fanyideskweb'+transtr+salt+'Y2FYu%TNSbMCxc3t2u^XT'
    sign=nmd5(strexample)
    #print(sign)
    i=len(transtr)
    #print(i)
    # print('MD5加密前为 :' + headerstr)
    # print('MD5加密后为 :' + bv)
    dict={'i':transtr,'from':'ja','TO':'zh-CHS','smartresult': 'dict',
          'client':'fanyideskweb',
          'salt':salt,
          'sign':sign,
          'ts':ts,
          'bv':bv,
          'doctype':'json',
          'version':'2.1',
          'keyfrom':'fanyi.web',
          'action':'FY_BY_REALTlME'
    }

    return dict

def trans(name):

    url='http://fanyi.youdao.com/translate_o?smartresult=dict&smartresult=rule'
    header={'User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36',
    'Referer':'http://fanyi.youdao.com/',
    'Origin': 'http://fanyi.youdao.com',
    'Content-Type':'application/x-www-form-urlencoded; charset=UTF-8',
    'X-Requested-With':'XMLHttpRequest',
    'Accept':'application/json, text/javascript, */*; q=0.01',
    'Accept-Encoding':'gzip, deflate',
    'Accept-Language':'zh-CN,zh;q=0.9',
    'Connection': 'keep-alive',
    'Host': 'fanyi.youdao.com',
    'cookie':'_ntes_nnid=937f1c788f1e087cf91d616319dc536a,1564395185984; OUTFOX_SEARCH_USER_ID_NCOO=; [email protected]; JSESSIONID=; ___rl__test__cookies=1'
    }

    dict=formdata(name)
    dict=urllib.parse.urlencode(dict)
    dict=str(dict)
    #dict=urllib.parse.urlencode(dict).encode('utf-8')
    req=requests.post(url,data=dict,headers=header)
    val=req.json()
    #print(val)
    return(val['translateResult'][0][0]['tgt'])

def get_pic_url(MovieNumber):
    req_url = 'https://www.javbus.com/'+MovieNumber  # 创建连接接口
    proxies = {
    "https": "127.0.0.1:1081"
}
    res = requests.get(req_url,proxies=proxies) #提交数据并解析
    res.encoding = 'utf-8'
    pattern = re.compile(r'\|]" # '/ \ : * ? " < > |'
    new_title = re.sub(rstr, "_", title) # 替换为下划线
    return new_title
def renamedir(DirName):

    for root, dirs, files in os.walk(DirName, topdown=False):
        for name in dirs:
            print(name)
            pattern = re.compile(r'([A-Z]{3,5})-(\d{3})')
            matchs = pattern.finditer(name.upper())
            for item in matchs:
                Movie_Number=''
                Movie_Title=''
                Movie_Number=item.group(1)+'-'+item.group(2)
                Movie_Title=get_MovieTitle(Movie_Number)
                if(Movie_Title!=''):
                    Movie_Title=validateTitle(trans(Movie_Title))    
                OldDirName=os.path.join(root, name)
                NewDirName=os.path.join(root,Movie_Number+' '+validateTitle(Movie_Title))             
                if(OldDirName!=NewDirName):
                    os.rename(OldDirName, NewDirName)

def renamefile(DirName):
    for root, dirs, files in os.walk(DirName, topdown=False):
        for name in files:
            pattern = re.compile(r'([A-Z]{3,5})-(\d{3})(.*?).(MKV|AVI|MP4|SRT)')
            matchs = pattern.finditer(name.upper())
            for item in matchs:
                print(name.upper())
                Movie_Number=''
                Movie_Title=''
                Movie_Number=item.group(1)+'-'+item.group(2)
                picurl=get_pic_url(Movie_Number)
                if(picurl!=''):
                    Movie_Title=get_MovieTitle(Movie_Number)
                    if(Movie_Title!=''):
                        Movie_Title=validateTitle(trans(Movie_Title))  
                    get_pic(os.path.join(root,Movie_Number+' '+Movie_Title+'.jpg'), picurl)
                OldDirName=os.path.join(root, name)
                
                NewDirName=os.path.join(root,Movie_Number+' '+Movie_Title+'.'+item.group(4))
                if(OldDirName!=NewDirName):
                    os.rename(OldDirName, NewDirName)

def deleteblacklistfile():
    blacklist=[]
    for dirpath, dirnames, filenames in os.walk('d:/blacklist/'):
        for filename in filenames:
            blacklist.append(filename)  
    dirs = []
    # r=root d=dirs f=files
    for root, dirs, files in os.walk("e:/", topdown=False):
        for name in files:
            if(name in blacklist):
                blackfile=os.path.join(root, name)
                print(blackfile)
                os.remove(blackfile)
            elif (name.find('梦幻天堂·龙网(www.LWgod.org).')>=0):
                orgfile=os.path.join(root, name)
                os.rename(orgfile,orgfile.replace('梦幻天堂·龙网(www.LWgod.org).',''))            
                print(orgfile)

    for root, dir, filename in os.walk('e:/'):
        for name in dir:

            dirs.append(os.path.join(root, name))
    for d in dirs:
        e=d.replace('【更多高清电影访问 www.BBQDDQ.com】','')
        f=e.replace('梦幻天堂·龙网(www.LWgod.org).1080p.','')
        g=f.replace('梦幻天堂·龙网(www.LWgod.org).','')
        if(d!=g):
            os.rename(d,g)
            print(d)
            print(f)    
if __name__ == '__main__':
    renamedir('E:\\ADN')
    renamefile('E:\\ADN')
    #print(get_pic_url('ADN-001'))
 
···

你可能感兴趣的:(调用有道翻译)