|
| 1 | +#撩妹专用,祝你撩妹成功 |
| 2 | +#更多请关注公众号:bigsai |
| 3 | +import itchat |
| 4 | +import requests |
| 5 | +import hashlib |
| 6 | +import time |
| 7 | +import urllib.parse |
| 8 | + |
| 9 | +jud=False#默认是先不开启 |
| 10 | +To='en'#翻译成的语言默认是英语 |
| 11 | + |
| 12 | +def nmd5(str):#md5加密 |
| 13 | + m = hashlib.md5() |
| 14 | + b = str.encode(encoding='utf-8') |
| 15 | + m.update(b) |
| 16 | + str_md5 = m.hexdigest() |
| 17 | + return str_md5 |
| 18 | +def formdata(transtr): |
| 19 | + # 待加密信息 |
| 20 | + global To |
| 21 | + headerstr = '5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36' |
| 22 | + bv=nmd5(headerstr) |
| 23 | + ts=str(round(time.time()*1000)) |
| 24 | + salt=ts+'90' |
| 25 | + strexample='fanyideskweb'+transtr+salt+'n%A-rKaT5fb[Gy?;N5@Tj' |
| 26 | + sign=nmd5(strexample) |
| 27 | + i=len(transtr) |
| 28 | + dict={'i':transtr,'from':'AUTO','to':To,'smartresult': 'dict', |
| 29 | + 'client':'fanyideskweb', |
| 30 | + 'salt':salt, |
| 31 | + 'sign':sign, |
| 32 | + 'ts':ts, |
| 33 | + 'bv':bv, |
| 34 | + 'doctype':'json', |
| 35 | + 'version':'2.1', |
| 36 | + 'keyfrom':'fanyi.web', |
| 37 | + 'action':'FY_BY_REALTlME' |
| 38 | + } |
| 39 | + return dict |
| 40 | +url='http://fanyi.youdao.com/translate_o?smartresult=dict&smartresult=rule' |
| 41 | +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', |
| 42 | + 'Referer':'http://fanyi.youdao.com/', |
| 43 | + 'Origin': 'http://fanyi.youdao.com', |
| 44 | + 'Content-Type':'application/x-www-form-urlencoded; charset=UTF-8', |
| 45 | + 'X-Requested-With':'XMLHttpRequest', |
| 46 | + 'Accept':'application/json, text/javascript, */*; q=0.01', |
| 47 | + 'Accept-Encoding':'gzip, deflate', |
| 48 | + 'Accept-Language':'zh-CN,zh;q=0.9', |
| 49 | + 'Connection': 'keep-alive', |
| 50 | + 'Host': 'fanyi.youdao.com', |
| 51 | + 'cookie': '_ntes_nnid=937f1c788f1e087cf91d616319dc536a,1564395185984; OUTFOX_SEARCH_USER_ID_NCOO=; [email protected]; JSESSIONID=; ___rl__test__cookies=1' |
| 52 | + } |
| 53 | +itchat.auto_login(hotReload=True)#登录 |
| 54 | + |
| 55 | + |
| 56 | +# 注册消息响应事件,消息类型为itchat.content.TEXT,文本消息 |
| 57 | +@itchat.msg_register(itchat.content.TEXT) |
| 58 | +def text_reply(msg): |
| 59 | + # 返回信息调用信息 |
| 60 | + global jud |
| 61 | + global To |
| 62 | + text=msg['Text'] |
| 63 | + dict = formdata(text) |
| 64 | + if "就像这样" in text: |
| 65 | + jud=True |
| 66 | + elif "停止" in text: |
| 67 | + jud=False |
| 68 | + elif "英语" in text: |
| 69 | + To = 'en' |
| 70 | + elif "日语" in text: |
| 71 | + To = 'ja' |
| 72 | + elif "韩语" in text: |
| 73 | + To = 'ko' |
| 74 | + elif "法语" in text: |
| 75 | + To = 'fr' |
| 76 | + elif "阿拉伯语" in text: |
| 77 | + To = 'ar' |
| 78 | + if jud:#说明需要运行 |
| 79 | + dict['To']=To |
| 80 | + dict = urllib.parse.urlencode(dict) |
| 81 | + dict = str(dict) |
| 82 | + req = requests.post(url, timeout=1, data=dict, headers=header) |
| 83 | + val = req.json() |
| 84 | + transtr = val['translateResult'][0][0]['tgt'] |
| 85 | + print(msg) |
| 86 | + itchat.send(transtr, toUserName=msg['ToUserName']) |
| 87 | + # return msg['Text']#这个加上是如果对面发消息的监听。比如你是双向翻译可以尝试下 |
| 88 | +# 绑定消息响应事件后,让itchat运行起来,监听消息 |
| 89 | +itchat.run() |
0 commit comments