-
Notifications
You must be signed in to change notification settings - Fork 0
/
wechat.py
363 lines (352 loc) · 15.3 KB
/
wechat.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
import html.parser as HTMLparser;
import urllib.request
import sys
import time
import json
import random
import jsonformat
from time import sleep
import urllib
import http.cookiejar as co
import re
import os
import mimetypes
import requests
import base64
from pydub import AudioSegment
cj = co.CookieJar()
opener = urllib.request.build_opener(urllib.request.HTTPCookieProcessor(cj))
def http(url,data,type):
req = urllib.request.Request(url,json.dumps(data).encode(encoding='UTF8'),method=type)
initdata = opener.open(req).read()
return initdata
def imageWrite():
url = "https://login.wx.qq.com/jslogin?appid=wx782c26e4c19acffb&redirect_uri=https%3A%2F%2Fwx.qq.com%2Fcgi-bin%2Fmmwebwx-bin%2Fwebwxnewloginpage&fun=new&lang=zh_CN&_=1469174552228%20Request%20Method:GET"
object = open("C://Users/Administrator/Desktop/123.png","wb")
data=http(url,None,"get").decode('UTF-8')
global uuid
uuid=data[data.find('"')+1:-2]
imgurl="https://login.weixin.qq.com/qrcode/"+uuid
object.write(http(imgurl,None,"get"))
object.close();
#等待扫码,扫码后调用登录,返回一个url
def loginCheck():
loginurl='https://login.weixin.qq.com/cgi-bin/mmwebwx-bin/login?tip=%s&r=%s&uuid=%s&_=%s' % (1,int(time.time()), uuid, int(time.time()*1000))
print(loginurl)
global logindata
logindata = urllib.request.urlopen(loginurl).read().decode('UTF-8');
print(logindata)
global code
code=int(logindata[logindata.find('code=')+5:logindata.find('\n')-1])
def sendMsg(msg,sendUserId,type):
if type==1:
msgurl='https://wx.qq.com/cgi-bin/mmwebwx-bin/webwxsendmsg?lang=zh_CN'
timeC=str(int(time.time() * 1000)) + str(random.random())[:5].replace('.', '')
msgdata={
'BaseRequest' : params['BaseRequest'],
"Msg":{"Type":1,
"Content":msg,
"FromUserName":userId,
"ToUserName":sendUserId,
"LocalID":timeC,
"ClientMsgId":timeC
},"Scene":0}
str1=jsonformat.someutil().toJson(msgdata,"")
req = urllib.request.Request(msgurl,str1.encode(encoding='UTF8'),method="POST")
req.add_header('ContentType','application/json; charset=UTF-8' )
data = opener.open(req).read()
return data
if type==3:
sendmsgimg(msg,sendUserId)
if type==47:
msgurl='https://wx.qq.com/cgi-bin/mmwebwx-bin/webwxsendemoticon?fun=sys&pass_ticket=%s' % pass_ticket
timeC=str(int(time.time() * 1000)) + str(random.random())[:5].replace('.', '')
msgdata={
'BaseRequest' : params['BaseRequest'],
"Msg":{"Type":47,
"EmojiFlag":2,
"EMoticonMd5":msg,
"FromUserName":userId,
"ToUserName":sendUserId,
"LocalID":timeC,
"ClientMsgId":timeC
},"Scene":0}
data = http(msgurl,msgdata,"POST")
return data
def msgAction(msg):
returnMsg = msg["Content"]
if msg["MsgType"]==1:
if msg['FromUserName'][:2] == '@@':
content = msg["Content"].split(':<br/>')[1]
returnMsg = content
else:
content = msg["Content"]
returnMsg = content
if msg["MsgType"]==3:
returnMsg = getmsgimg(msg["MsgId"])
if msg["MsgType"]==34:
returnMsg = getvoice(msg["MsgId"])
if msg["MsgType"]==47:
returnMsg= msg["Content"].replace('<', '<').replace('>', '>')
returnMsg=returnMsg[returnMsg.find("md5=\"")+5:returnMsg.find("\" len")]
if msg["MsgType"]==51:
returnMsg=""
return returnMsg;
def getvoice(msgId):
apiKey="jHLsG7y4pCHoxGVexittHTLw61xpiVMK"
SecretKey="zjjZlOGSZ4PzsAIhMAwKQ69hG0i7dxgc"
token=json.loads(http("https://openapi.baidu.com/oauth/2.0/token?grant_type=client_credentials&client_id=%s&client_secret=%s" % (apiKey,SecretKey),None,"GET").decode('UTF8'))["access_token"]
req = urllib.request.Request("https://wx.qq.com/cgi-bin/mmwebwx-bin/webwxgetvoice?msgid=%s&skey=%s" % (msgId,skey),method="GET")
file= opener.open(req)
song = AudioSegment.from_mp3(file) #读取mp3格式文件
song.export("C://Users/Administrator/Desktop/4.wav", format="wav",bitrate="8k")
file_object1 = open('C://Users/Administrator/Desktop/4.wav',"rb")
file=file_object1.read()
file_object1.close()
data={
"format":"wav",
"rate": 8000,
"channel":"1",
"token":token,
"cuid":"7757437",
"len":len(file),
"speech":base64.b64encode(file).decode('UTF8'),
}
req = urllib.request.Request("http://vop.baidu.com/server_api",json.dumps(data).encode(encoding='UTF8'),method="POST")
req.add_header("Content-Type", "application/json; charset=utf-8")
msg=urllib.request.urlopen(req).read().decode('UTF-8')
print(msg)
jsondata = json.loads(msg)
msg=jsondata["result"][0] if jsondata["err_no"]==0 else " "
return msg;
def getmsgimg(msgId):
dirName='C://Users/Administrator/Desktop/img_' + msgId + '.jpg'
object = open(dirName,"wb")
object.write(http("https://wx.qq.com/cgi-bin/mmwebwx-bin/webwxgetmsgimg?&MsgID=%s&skey=%s&type=slave" % (msgId,skey),None,"get"))
object.close();
return dirName
def sendmsgimg(msgId,sendUserId):
dirName=msgId
jsondata = webwxuploadmedia(dirName)
if jsondata==None:
return;
media_id = jsondata['MediaId']
url = 'https://wx.qq.com/cgi-bin/mmwebwx-bin/webwxsendmsgimg?fun=async&f=json&pass_ticket=%s' % pass_ticket
clientMsgId = str(int(time.time() * 1000)) + \
str(random.random())[:5].replace('.', '')
data_json = {
"BaseRequest": params['BaseRequest'],
"Msg": {
"Type": 3,
"MediaId": media_id,
"FromUserName": userId,
"ToUserName": sendUserId,
"LocalID": clientMsgId,
"ClientMsgId": clientMsgId
}
}
headers = {'content-type': 'application/json; charset=UTF-8'}
data = json.dumps(data_json, ensure_ascii=False).encode('utf8')
r = requests.post(url, data=data, headers=headers)
dic = r.json()
return dirName
def webwxuploadmedia(image_name):
url = 'https://file.wx.qq.com/cgi-bin/mmwebwx-bin/webwxuploadmedia?f=json'
# 计数器
global media_count
media_count = media_count + 1
# 文件名
file_name = image_name
# MIME格式
# mime_type = application/pdf, image/jpeg, image/png, etc.
mime_type = mimetypes.guess_type(image_name, strict=False)[0]
# 微信识别的文档格式,微信服务器应该只支持两种类型的格式。pic和doc
# pic格式,直接显示。doc格式则显示为文件。
media_type = 'pic' if mime_type.split('/')[0] == 'image' else 'doc'
# 上一次修改日期
lastModifieDate = 'Tue Dec 13 2016 17:14:47 GMT+0800 (中国标准时间)'
# 文件大小
file_size = os.path.getsize(file_name)
# clientMediaId
client_media_id = str(int(time.time() * 1000)) + \
str(random.random())[:5].replace('.', '')
# webwx_data_ticket
webwx_data_ticket = ''
for item in cj:
if item.name == 'webwx_data_ticket':
webwx_data_ticket = item.value
break
if (webwx_data_ticket == ''):
return "None Fuck Cookie"
uploadmediarequest = json.dumps({
"UploadType": 2,
"BaseRequest": params['BaseRequest'],
"ClientMediaId": client_media_id,
"TotalLen": file_size,
"StartPos": 0,
"DataLen": file_size,
"MediaType": 4
}, ensure_ascii=False)
# "FromUserName":"@bbe75da1320ccae39ec60ec150997a0c14715ff14ecece84a97e163cace535a0","ToUserName":"filehelper","FileMd5":"5eb5f4102da4715f1136906838dde53b"}
multipart_encoder = [
{'id': 'WU_FILE_' + str(media_count)},
{'name': file_name},
{'type': mime_type},
{'lastModifiedDate': lastModifieDate},
{'size': str(file_size)},
{'mediatype': media_type},
{'uploadmediarequest': uploadmediarequest},
{'webwx_data_ticket': webwx_data_ticket},
{'pass_ticket': pass_ticket},
{'filename': (file_name, open(file_name, 'rb'), mime_type)}
]
# {
# 'id': 'WU_FILE_' + str(media_count),
# 'name': file_name,
# 'type': mime_type,
# 'lastModifieDate': lastModifieDate,
# 'size': str(file_size),
# 'mediatype': media_type,
# 'uploadmediarequest': uploadmediarequest,
# 'webwx_data_ticket': webwx_data_ticket,
# 'pass_ticket': pass_ticket,
# 'filename': (file_name, open(file_name, 'rb'), mime_type.split('/')[1])
# }
boundary='------WebKitFormBoundarysVhHEUB6HWQIhDbb'
data=b'';
#python3要全转为byte拼接
for k in multipart_encoder:
v=k.get(list(k.keys())[0])
k=list(k.keys())[0]
print(k)
print(v)
data+=(boundary+"\r\n").encode(encoding='utf_8')
if k=="filename":
data+=('Content-Disposition: form-data; name="%s"; filename="' % k +v[0]+'"\r\n').encode(encoding='utf_8')
data+=('Content-Type: '+v[2]).encode(encoding='utf_8')
else:
data+=('Content-Disposition: form-data; name="%s"\r\n' % k).encode(encoding='utf_8')
data+="\r\n".encode(encoding='utf_8')
if k=="filename":
data+="\r\n".encode(encoding='utf_8')
# data+="\n"
c=v[1].read()
data=data+c
data+="\r\n".encode(encoding='utf_8')
# print(c)
# print(base64.b64encode(c).decode('UTF8'))
# data+=base64.b64encode(c).decode('UTF8')+"\n"
else:
data+=(v+"\r\n").encode(encoding='utf_8')
data+=(boundary+"--\r\n").encode(encoding='utf_8')
print(data)
headers = {
'Host': 'file.wx.qq.com',
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36',
'Accept': '*/*',
'Accept-Language': 'zh-CN,zh;q=0.8',
'Accept-Encoding': 'gzip, deflate, br',
'Referer': 'https://wx.qq.com/',
'Content-Type':'multipart/form-data; boundary=----WebKitFormBoundarysVhHEUB6HWQIhDbb',
'Origin': 'https://wx.qq.com',
'Connection': 'keep-alive',
'Pragma': 'no-cache',
'Cache-Control': 'no-cache'
}
req = urllib.request.Request(url,data,method="POST",headers=headers)
response_json = json.loads(urllib.request.urlopen(req).read().decode('UTF-8'))
print(response_json)
if response_json['BaseResponse']['Ret'] == 0:
return response_json
return None
xiaobingId=''
msgData={}
uuid="";
imageWrite();
code=0
media_count=0
while code!=200:
time.sleep(3)
loginCheck();
xmldata = http(logindata[logindata.find('uri="')+5:-2]+"&fun=new","","GET").decode('UTF-8');
skey=xmldata[xmldata.find("<skey>")+6:xmldata.find("</skey>")]
sid=xmldata[xmldata.find("<wxsid>")+7:xmldata.find("</wxsid>")]
uin=xmldata[xmldata.find("<wxuin>")+7:xmldata.find("</wxuin>")]
pass_ticket=xmldata[xmldata.find("<pass_ticket>")+13:xmldata.find("</pass_ticket>")]
print(xmldata)
print(skey,sid,uin,pass_ticket)
DeviceId='e' + repr(random.random())[2:17]
params = {
'BaseRequest' : {
'Uin': int(uin),
'Sid': sid,
'Skey': skey,
'DeviceID': DeviceId,
}
}
initData=http("https://wx.qq.com/cgi-bin/mmwebwx-bin/webwxinit?r=%s&pass_ticket=%s" % (int(time.time()),pass_ticket),params,"POST").decode('UTF-8');
initData=json.loads(initData)
userId = initData['User']['UserName']
synckey='|'.join(
[str(keyVal['Key']) + '_' + str(keyVal['Val']) for keyVal in initData['SyncKey']['List']])
print(userId)
params2={
'BaseRequest' : params['BaseRequest'],
"Code":3,
"FromUserName":userId,
"ToUserName":userId,
"ClientMsgId":int(time.time()*1000)
}
notifyData = http("https://wx.qq.com/cgi-bin/mmwebwx-bin/webwxstatusnotify?pass_ticket=%s" % (pass_ticket),params2,"POST").decode("UTF-8");
print(notifyData)
getcontactData=http("https://wx.qq.com/cgi-bin/mmwebwx-bin/webwxgetcontact?lang=zh_CN&pass_ticket=%s&seq=0&skey=%s" % (pass_ticket,skey), "", "GET").decode("UTF-8");
# print(getcontactData)
SyncKeyObj = initData['SyncKey']
while True:
sleep(2)
params3 = {
'r': int(time.time()*1000),
'sid': sid,
'uin': uin,
'skey': skey,
'deviceid': DeviceId,
'synckey': synckey,
'_': int(time.time()*1000),
}
synccheckData=http("https://webpush.wx.qq.com/cgi-bin/mmwebwx-bin/synccheck?"+ urllib.parse.urlencode(params3), "", "GET").decode("UTF-8");
print(synccheckData)
pm = re.search(
r'window.synccheck={retcode:"(\d+)",selector:"(\d+)"}', synccheckData)
retcode = pm.group(1)
selector = pm.group(2)
if retcode=='0' and selector!='0':
params4={
'BaseRequest' : params['BaseRequest'],
"SyncKey":SyncKeyObj
}
webwxsyncData=http("https://wx.qq.com/cgi-bin/mmwebwx-bin/webwxsync?sid=%s&skey=%s&lang=zh_CN&pass_ticket=%s" % (sid,skey,pass_ticket),params4,"POST").decode("UTF-8");
print(webwxsyncData)
webwxsyncData=json.loads(webwxsyncData)
global synckey
global SyncKeyObj
synckey='|'.join(
[str(keyVal['Key']) + '_' + str(keyVal['Val']) for keyVal in webwxsyncData['SyncKey']['List']])
SyncKeyObj=webwxsyncData['SyncKey']
global xiaobingId
if xiaobingId =='':
xiaobingId=input("小冰ID:")
ToUserName=input("监听对象:")
print(xiaobingId)
# if xiaobingId !='':
# sendMsg("小冰你好",xiaobingId)
else:
for msgAdd in webwxsyncData['AddMsgList']:
if msgAdd['ToUserName']==ToUserName :
message = msgAction(msgAdd)
if msgAdd["MsgType"]==34:
sendMsg(message,ToUserName,1)
message=""#停止回复小冰
if message!="":
sendMsg(message,xiaobingId,msgAdd['MsgType'])
if msgAdd['FromUserName']==xiaobingId:
sendMsg(msgAction(msgAdd),ToUserName,msgAdd['MsgType'])