-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchatroomlistV2.0.py
153 lines (101 loc) · 4.23 KB
/
chatroomlistV2.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
#用于向指定群发送消息
# coding:utf-8
#v1.1 支持获取群列表并选择需要保存的群列表写入本地文件中
#v2.0 修改了群列表在本地保存格式
import itchat
import wx
from threading import Thread
from pubsub import pub
#from wx.lib.pubsub import pub
import time
import os
class Threadacquire(Thread):
"""Test Worker Thread Class."""
#----------------------------------------------------------------------
def __init__(self):
"""Init Worker Thread Class."""
Thread.__init__(self)
self.start() # start the thread
def run(self):
global chatroomlist
chatroomlist={}
notice="请扫码或手机确认登录"
wx.CallAfter(pub.sendMessage,'update',re_msg=notice)
itchat.auto_login(hotReload=True)
notice="登录成功"
wx.CallAfter(pub.sendMessage,'update',re_msg=notice)
chatlist=itchat.get_chatrooms(update=True)
if chatlist != []:
wx.CallAfter(pub.sendMessage,'update',re_msg="以下为群列表\n\n")
time.sleep(1)
for chatroom in chatlist:
chatroomNickname=chatroom["NickName"]
chatlistUsername=chatroom['UserName']
chatdict={chatroomNickname: chatlistUsername}
chatroomlist.update(chatdict)
print(chatroomNickname)
wx.CallAfter(pub.sendMessage,'update',re_msg=chatroomNickname)
else:
print("通讯录无群聊")
wx.CallAfter(pub.sendMessage,'update',re_msg="通讯录无群聊")
itchat.run()
#界面
class MyForm(wx.Frame):
#-------------------------------------------------------------------
#set the window layout
def __init__(self):
wx.Frame.__init__(self, None, wx.ID_ANY, "微信撤回消息查看", size =(500,410))
global nickname,contents,acquireButton,saveButton
bkg=wx.Panel(self,wx.ID_ANY)
#bkg.SetBackgroundColour("Grey")
acquireButton=wx.Button(bkg,label='获取群信息')
acquireButton.Bind(wx.EVT_BUTTON,self.acquire)
saveButton=wx.Button(bkg,label='保存需要群发的群列表')
saveButton.Disable()
saveButton.Bind(wx.EVT_BUTTON,self.save)
#nickname=wx.TextCtrl(bkg)
contents=wx.TextCtrl(bkg,style=wx.TE_MULTILINE | wx.HSCROLL )
#contents.SetStyle(410,335,wx.TextAttr("RED","YELLOW"))
hbox=wx.BoxSizer()
#hbox.Add(nickname,1,wx.EXPAND)
hbox.Add(acquireButton,0,wx.LEFT,5)
hbox.Add(saveButton,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 acquire(self,event):
Threadacquire()
acqbtn=event.GetEventObject()
acqbtn.Disable()
saveButton.Enable()
#保存用户选择的群聊记录在本地文件中,保存信息包括群聊NickName和Username
def save(self,event):
roomlist=''
linenum=contents.GetNumberOfLines()
for i in range(1,linenum):
chatassign=contents.GetLineText(i)
if chatroomlist.get(chatassign) is not None:
roomlist=roomlist+'\n'+chatassign
with open(r'chatroomlist.txt' ,'w',encoding="utf8") as chatroomfile:
chatroomfile.write(roomlist)
#os.getcwd()
print("保存成功")
contents.AppendText("保存成功\n保存目录为:%s \n" % os.getcwd())
itchat.logout()
contents.AppendText("已退出登录\n")
saveButton.Disable()
acquireButton.Enable()
#通过多行文本框显示提示和记录的信息
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()