-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwechatMassSendV2.0.py
424 lines (350 loc) · 19.2 KB
/
wechatMassSendV2.0.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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
# 用于向指定群发送消息
# coding:utf-8
# v1.1 包含四个按钮,选择群聊列表、登录,发送文件,发送信息
# v1.2 优化报错搜索不到群聊的报错,群聊列表文件选错的报错
# v1.3 完善发送失败报错,并将发送失败的群列表保存在在本地文件中
# V2.0 关联chatroomlist修改了群里表的保存格式,进行获取文件的格式的修改
import itchat
import wx
from threading import Thread
from pubsub import pub
# from wx.lib.pubsub import pub
import time
import filetype
import os
# 创建一个文件对话框,用于选择文件,返回文件所在目录
def choose(self):
global tmp
tmp = ''
wildcard1 = "All files (*.*)|*.*|" \
"Python source (*.py; *.pyc)|*.py;*.pyc"
dlg = wx.FileDialog(self, message="Choose a file", defaultFile="", wildcard=wildcard1,
style=wx.FD_OPEN | wx.FD_CHANGE_DIR)
if dlg.ShowModal() == wx.ID_OK:
paths = dlg.GetPaths()
tmp = paths[0]
else:
tmp = ''
dlg.Destroy()
return tmp
# 创建一个读取本地文件的函数,需以utf8编码方式读取,返回读取的文件内容
def get_file_content(filePath):
with open(filePath, 'r', encoding="UTF-8") as fp:
return fp.read()
# 创建一个登录微信类,该类单独使用一个线程,防止因为发送时间过长导致GUI类MyFrom
class Threadlogin(Thread):
"""Test Worker Thread Class."""
# ----------------------------------------------------------------------
def __init__(self):
"""Init Worker Thread Class."""
Thread.__init__(self)
self.start() # start the thread
def run(self):
notice = "请扫码或手机确认登录"
wx.CallAfter(pub.sendMessage, 'update', re_msg=notice) # 利用PubSub实现wxPython应用程序与其他线程进行通讯
wx.CallAfter(pub.sendMessage, 'update', re_msg="如果没有弹出二维码,请查看该应用所在目录是否生成登录二维码")
itchat.auto_login(hotReload=True)
notice = "登录成功"
wx.CallAfter(pub.sendMessage, 'update', re_msg=notice + '\n' + '请选择需发送的文件或输入信息')
itchat.run()
# 创建一个发送信息的类,该类单独使用一个线程,防止因为发送时间过长导致GUI类MyFrom
class Threadsend(Thread):
"""Test Worker Thread Class."""
# @sendre=<ItchatReturnValue: {'BaseResponse': {'Ret': 0, 'ErrMsg': '请求成功', 'RawMsg': '请求成功'}, 'MsgID': '7224254309590260193', 'LocalID': '15331388206320'}>
# ----------------------------------------------------------------------
def __init__(self):
"""Init Worker Thread Class."""
Thread.__init__(self)
self.start() # start the thread
def run(self):
notice = "准备群发消息"
wx.CallAfter(pub.sendMessage, 'update', re_msg=notice)
# itchat.auto_login()
# notice="登录成功"
# wx.CallAfter(pub.sendMessage,'update',re_msg=notice)
time.sleep(2)
print(sendfiletype)
sendfaillist = [] # 用于保存发送失败的群列表
searchfaillist = [] # 用于保存搜索不到的群列表
num = len(chatlist)
# 根据不同发送内容调用不同发送函数向微信群群发微信消息
# 如果发送的文件是文件类型
if sendfiletype == 'file' or sendfiletype == "archive" or sendfiletype == 'font' or sendfiletype == 'application':
# print("1")
print("向以下群聊发送文件:%s" % message)
wx.CallAfter(pub.sendMessage, 'update', re_msg="向以下群聊发送文件:%s" % message)
i = 0
for chatroom in chatlist:
# print("向以下群聊发送文件:%s" % message)
# wx.CallAfter(pub.sendMessage,'update',re_msg="向以下群聊发送文件:%s" % message)
if itchat.search_chatrooms(chatroom) != []:
chatroomusername = itchat.search_chatrooms(chatroom)[0]['UserName']
sendre = itchat.send("@fil@%s"% message, toUserName=chatroomusername)
if sendre['BaseResponse']['ErrMsg'] == "请求成功":
print("%s:发送成功" % chatroom)
wx.CallAfter(pub.sendMessage, 'update', re_msg="%s:发送成功" % chatroom)
else:
print("%s:发送失败" % chatroom)
wx.CallAfter(pub.sendMessage, 'update', re_msg="%s:发送失败" % chatroom)
sendfaillist.append(chatroom)
else:
print("搜索不到群聊")
wx.CallAfter(pub.sendMessage, 'update', re_msg="%s:搜索失败" % chatroom)
searchfaillist.append(chatroom)
time.sleep(0.5)
i += 1
if i == num:
print("执行完毕")
wx.CallAfter(pub.sendMessage, 'update', re_msg="执行完毕")
# 如果发送的文件是图片类型
elif sendfiletype == 'image':
# print('2')
print("向以下群聊发送图片:%s" % message)
wx.CallAfter(pub.sendMessage, 'update', re_msg="向以下群聊发送图片:%s" % message)
i = 0
for chatroom in chatlist:
if itchat.search_chatrooms(chatroom) != []:
chatroomusername = itchat.search_chatrooms(chatroom)[0]['UserName'] # 搜索群列表会返回一个群列表的类,混合列表和字典,需通过该方式获取群聊的UserName
sendre = itchat.send("@img@%s" % message, toUserName=chatroomusername)
if sendre['BaseResponse']['ErrMsg'] == "请求成功":
print("%s:发送成功" % chatroom)
wx.CallAfter(pub.sendMessage, 'update', re_msg="%s:发送成功" % chatroom)
else:
print("%s:发送失败" % chatroom)
wx.CallAfter(pub.sendMessage, 'update', re_msg="%s:发送失败" % chatroom)
sendfaillist.append(chatroom)
else:
print("搜索不到群聊")
wx.CallAfter(pub.sendMessage, 'update', re_msg="%s:搜索失败" % chatroom)
searchfaillist.append(chatroom)
time.sleep(0.5)
i += 1
if i == num:
print("执行完毕")
wx.CallAfter(pub.sendMessage, 'update', re_msg="执行完毕")
elif sendfiletype == "video" or sendfiletype == "audio":
# print('3')
print("向以下群聊发送视频:%s" % message)
wx.CallAfter(pub.sendMessage, 'update', re_msg="向以下群聊发送视频:%s" % message)
i = 0
for chatroom in chatlist:
# print("向以下群聊发送视频:%s" % message)
# wx.CallAfter(pub.sendMessage,'update',re_msg="向以下群聊发送视频:%s" % message)
if itchat.search_chatrooms(chatroom) != []:
chatroomusername = itchat.search_chatrooms(chatroom)[0]['UserName']
sendre = itchat.send_video(message, toUserName=chatroomusername)
if sendre['BaseResponse']['ErrMsg'] == "请求成功":
print("%s:发送成功" % chatroom)
wx.CallAfter(pub.sendMessage, 'update', re_msg="%s:发送成功" % chatroom)
else:
print("%s:发送失败" % chatroom)
wx.CallAfter(pub.sendMessage, 'update', re_msg="%s:发送失败" % chatroom)
sendfaillist.append(chatroom)
else:
print("搜索不到群聊")
wx.CallAfter(pub.sendMessage, 'update', re_msg="%s:搜索失败" % chatroom)
searchfaillist.append(chatroom)
time.sleep(0.5)
i += 1
if i == num:
print("执行完毕")
wx.CallAfter(pub.sendMessage, 'update', re_msg="执行完毕")
elif sendfiletype == "message":
# print('4')
print("向以下群聊发送信息:%s" % message)
wx.CallAfter(pub.sendMessage, 'update', re_msg="向以下群聊发信息:%s" % message)
i = 0
for chatroom in chatlist:
# wx.CallAfter(pub.sendMessage,'update',re_msg="向以下群聊发信息:%s" % message)
if itchat.search_chatrooms(chatroom) != []:
chatroomusername = itchat.search_chatrooms(chatroom)[0]['UserName']
sendre = itchat.send(message, toUserName=chatroomusername)
if sendre['BaseResponse']['ErrMsg'] == "请求成功":
print("%s:发送成功" % chatroom)
wx.CallAfter(pub.sendMessage, 'update', re_msg="%s:发送成功" % chatroom)
else:
print("%s:发送失败" % chatroom)
wx.CallAfter(pub.sendMessage, 'update', re_msg="%s:发送失败" % chatroom)
sendfaillist.append(chatroom)
else:
print("搜索不到群聊")
wx.CallAfter(pub.sendMessage, 'update', re_msg="%s:搜索失败" % chatroom)
searchfaillist.append(chatroom)
time.sleep(0.5)
i += 1
if i == num:
print("执行完毕")
wx.CallAfter(pub.sendMessage, 'update', re_msg="执行完毕")
else:
# print('5')
print("向以下群聊发送信息:%s" % message)
wx.CallAfter(pub.sendMessage, 'update', re_msg="向以下群聊发送信息:%s" % message)
i = 0
for chatroom in chatlist:
# print("向以下群聊发送信息:%s" %message)
# wx.CallAfter(pub.sendMessage,'update',re_msg="向以下群聊发送信息:%s" % message)
if itchat.search_chatrooms(chatroom) != []:
chatroomusername = itchat.search_chatrooms(chatroom)[0]['UserName']
sendre = itchat.send(message, toUserName=chatroomusername)
if sendre['BaseResponse']['ErrMsg'] == "请求成功":
print("%s:发送成功" % chatroom)
wx.CallAfter(pub.sendMessage, 'update', re_msg="%s:发送成功" % chatroom)
else:
print("%s:发送失败" % chatroom)
wx.CallAfter(pub.sendMessage, 'update', re_msg="%s:发送失败" % chatroom)
sendfaillist.append(chatroom)
else:
print("搜索不到群聊")
wx.CallAfter(pub.sendMessage, 'update', re_msg="%s:搜索失败" % chatroom)
searchfaillist.append(chatroom)
time.sleep(0.5)
i += 1
if i == num:
print("执行完毕")
wx.CallAfter(pub.sendMessage, 'update', re_msg="执行完毕")
# print('发送完毕')
# wx.CallAfter(pub.sendMessage,'update',re_msg="发送完毕\n")
if sendfaillist != [] and searchfaillist == []:
with open(r'sendfaillist.txt', 'w', encoding="UTF-8") as sendfaillistxt:
sendfaillistxt.write('\n'.join(sendfaillist))
print("将发送失败列表文件sendfaillist.txt保存在目:%s \n" % os.getcwd())
wx.CallAfter(pub.sendMessage, 'update', re_msg="将发送失败列表文件sendfaillist.txt保存在目:%s \n" % os.getcwd())
elif searchfaillist != [] and sendfaillist == []:
with open(r'searchfaillist.txt', 'w', encoding="UTF-8") as searchfaillistxt:
searchfaillistxt.write('\n'.join(searchfaillist))
print("将搜索失败列表文件searchfaillist.txt保存在目:%s \n" % os.getcwd())
wx.CallAfter(pub.sendMessage, 'update', re_msg="将搜索失败列表文件searchfaillist.txt保存在目:%s \n" % os.getcwd())
elif sendfaillist != [] and searchfaillist != []:
with open(r'searchfaillist.txt', 'w', encoding="UTF-8") as searchfaillistxt:
searchfaillistxt.write('\n'.join(searchfaillist))
with open(r'sendfaillist.txt', 'w', encoding="UTF-8") as sendfaillistxt:
sendfaillistxt.write('\n'.join(sendfaillist))
print("将发送失败列表文件sendfaillist.txt保存在目:%s \n" % os.getcwd())
print("将搜索失败列表文件searchfaillist.txt保存在目:%s \n" % os.getcwd())
wx.CallAfter(pub.sendMessage, 'update', re_msg="将发送失败列表文件sendfaillist.txt保存在目:%s \n" % os.getcwd())
wx.CallAfter(pub.sendMessage, 'update', re_msg="将搜索失败列表文件searchfaillist.txt保存在目:%s \n" % os.getcwd())
else:
print("%s个群全部发送成功" % num)
wx.CallAfter(pub.sendMessage, 'update', re_msg="%s个群全部发送成功" % num)
# 创建一个GUI类,包含主界面和部分功能实现
class MyForm(wx.Frame):
# 创建主界面
# set the window layout
def __init__(self):
wx.Frame.__init__(self, None, wx.ID_ANY, "微信群发消息(微信群)", size=(500, 410))
global filetext, contents, sendfiletype, filepath
sendfiletype = ''
# chatlist=[]
filepath = ''
bkg = wx.Panel(self, wx.ID_ANY)
# bkg.SetBackgroundColour("Grey")
# 创建获取群群列表按钮
acquirelistButton = wx.Button(bkg, label='选择群列表文件')
acquirelistButton.Bind(wx.EVT_BUTTON, self.acquirelist)
# 创建选择并获取发送文件按钮
acquirefileButton = wx.Button(bkg, label='选择发送文件')
acquirefileButton.Bind(wx.EVT_BUTTON, self.acquirefile)
# 创建登录按钮
loginButton = wx.Button(bkg, label='登录')
loginButton.Bind(wx.EVT_BUTTON, self.login)
# 创建发送信息按钮
sendButton = wx.Button(bkg, label='发送')
sendButton.Bind(wx.EVT_BUTTON, self.send)
# 创建一个显示选择文件路径的文本框,还需能输入多行信息用于发送
filetext = wx.TextCtrl(bkg, style=wx.TE_MULTILINE | wx.HSCROLL | wx.TE_RICH2)
contents = wx.TextCtrl(bkg, style=wx.TE_MULTILINE | wx.HSCROLL | wx.TE_RICH2)
# contents.SetStyle(410,335,wx.TextAttr("RED","YELLOW"))
hbox = wx.BoxSizer()
hbox.Add(filetext, 1, wx.EXPAND)
hbox.Add(acquirelistButton, 0, wx.LEFT, 5)
hbox.Add(loginButton, 0, wx.LEFT, 5)
hbox.Add(acquirefileButton, 0, wx.LEFT, 5)
hbox.Add(sendButton, 0, wx.LEFT, 5)
vbox = wx.BoxSizer(wx.VERTICAL)
vbox.Add(hbox, 0, wx.EXPAND | wx.ALL, 5)
vbox.Add(contents, 1, wx.EXPAND | wx.LEFT | wx.BOTTOM | wx.RIGHT, 5)
bkg.SetSizer(vbox)
# 创建一个pub接收器
pub.subscribe(self.updatedispaly, 'update')
# 创建一个获取群发列表文件并读取内容的函数
def acquirelist(self, event):
global chatlist
chatlist = []
chatlistfilepath = ''
chatlistfilepath = choose(self)
if tmp != "":
print(tmp)
filetext.SetValue(chatlistfilepath)
try:
chatlist = get_file_content(chatlistfilepath).split('\n')
print("以下为获取的群发列表\n\n")
contents.AppendText("以下为获取的群发列表\n\n")
print(chatlist)
for index, chatroom in enumerate(open(chatlistfilepath, encoding='utf-8')):
contents.AppendText('%s : %s \n' % (index + 1, chatroom))
print("已经选择群列表,请点击登录微信\n\n")
contents.AppendText("已经选择群列表,请点击登录微信\n\n")
except:
print("群聊列表文件不符合规范,请重新生成")
contents.AppendText("群聊列表文件不符合规范,请重新生成\n")
# for chatroom in chatlistdict.keys():
else:
print("请选择文件")
contents.AppendText("请选择文件\n")
# chatlistdict=ast.literal_eval(get_file_content(chatlistfilepath))
# 创建一个选择发送文件的函数并返回文件类型
def acquirefile(self, event):
# global filepath
# filepath=''
# sendfiletype=''
global sendfiletype
filepath = choose(self)
if tmp != "":
filetext.SetValue(filepath)
print("选取的文件为:%s \n\n" % filepath)
contents.AppendText("选取的文件为:%s \n" % filepath)
fileguess = filetype.guess_mime(filepath)
if fileguess is None:
sendfiletype = "file"
print("文件类型为:%s \n" % sendfiletype)
contents.AppendText("文件类型为:%s \n" % sendfiletype)
else:
sendfiletype = fileguess.split('/')[0]
print(sendfiletype)
print("文件类型为:%s \n" % sendfiletype)
contents.AppendText("文件类型为:%s \n" % sendfiletype)
# Threadacquire()
# 创建登录微信的函数
def login(self, event):
Threadlogin()
# 信息发送函数
def send(self, event):
global message, sendfiletype
tmppath = filetext.GetValue()
message = tmppath
# if sendfiletype !=''and not os.path.exists(tmppath):
if not os.path.exists(tmppath):
sendfiletype = "message"
if chatlist != [] and sendfiletype != '' and message != '':
getchatroom = itchat.get_chatrooms()
if len(getchatroom) != 0:
print(chatlist, sendfiletype)
Threadsend()
else:
print("请点击登录按钮登录")
contents.AppendText("请点击登录按钮登录\n")
# elif chatlist==[] or sendfiletype==''or message=='':
else:
# print(chatlist,sendfiletype)
print('未选择群发列表或未选择发送文件或未输入发送信息')
contents.AppendText('未选择群发列表或未选择发送文件或未输入发送信息\n')
# 通过多行文本框显示提示和记录的信息
def updatedispaly(self, re_msg):
displaymessage = re_msg
contents.AppendText(displaymessage + '\n\n')
# Main
if __name__ == "__main__":
app = wx.App()
frame = MyForm()
frame.Show()
app.MainLoop()