-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWxInterface.py
124 lines (109 loc) · 4.63 KB
/
WxInterface.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
import WxBot
class WxInterface:
def __init__(self):
self.bot = WxBot.WxBot()
self.wxToken = {}
def getToken(self):
return self.wxToken
def login(self, QRCodeFilename="qrcode.jpg", autoOpen=True):
import os, time, json, sys
self.wxToken["loginQRToken"] = self.bot.getLoginToken()
try:
with open(QRCodeFilename, "wb") as QRCode:
QRCode.write(self.bot.getQRCode(self.wxToken["loginQRToken"]))
except:
print("写入二维码错误")
sys.exit()
print("使用手机扫描二维码图片" + str(QRCodeFilename) + "以登录")
if autoOpen:
os.system('call %s' % QRCodeFilename)
while True:
wxLoginStatus = self.bot.getLoginStatus(self.wxToken["loginQRToken"])
if wxLoginStatus[1] == "201":
print("已经扫描二维码,请在手机上点击登录")
elif wxLoginStatus[1] == "200":
print("请稍候...")
self.wxToken["redirectURL"] = self.bot.getWxRedirectURL(wxLoginStatus[0])
break
time.sleep(1)
wxWebWechatToken = self.bot.wxRedirect(self.wxToken["redirectURL"])
self.wxToken["skey"] = wxWebWechatToken.split("<skey>")[1].split("</skey>")[0]
self.wxToken["wxsid"] = wxWebWechatToken.split("<wxsid>")[1].split("</wxsid>")[0]
self.wxToken["wxuin"] = wxWebWechatToken.split("<wxuin>")[1].split("</wxuin>")[0]
self.wxToken["pass_ticket"] = wxWebWechatToken.split("<pass_ticket>")[1].split("</pass_ticket>")[0]
if self.wxToken["pass_ticket"] == "":
print("无法获取用户凭据,可能当天登陆次数过多,触发登录频率限制")
sys.exit()
self.wxToken.pop("redirectURL")
self.wxInitData = json.loads(self.bot.wxInit(self.wxToken))
self.wxToken["displayname"] = self.wxInitData["User"]["NickName"]
self.wxToken["username"] = self.wxInitData["User"]["UserName"]
self.wxContacts = json.loads(self.bot.getContact(self.wxToken))
print("==========你好," + self.wxToken["displayname"] + "!==========")
def printRecentContacts(self):
if len(self.wxInitData["ContactList"]):
print("最近联系人为:")
for recentCommunicatePerson in self.wxInitData["ContactList"]:
displayName = recentCommunicatePerson["RemarkName"] or recentCommunicatePerson["NickName"]
print("\t" + displayName)
print("\t目前共有" + str(self.wxContacts["MemberCount"]) + "位好友")
else:
print("没有最近联系人")
def findFriend(self, keyword):
findResult = []
for contacts in self.wxContacts["MemberList"]:
if contacts["NickName"].find(keyword) != -1 or contacts["RemarkName"].find(keyword) != -1:
findResult.append({'NickName': contacts["NickName"], 'RemarkName': contacts["RemarkName"], 'UserName': contacts["UserName"]})
return findResult
def sendMessageUI(self):
import os.path
print('发送消息')
while True:
contactTo = input("请输入要发送消息的联系人:")
if contactTo == "":
return
searchedList = self.findFriend(contactTo)
if len(searchedList):
print("找到以下联系人")
break
else:
print("\t未找到任何联系人")
for person in searchedList:
print("\t\t*" + person["NickName"] + " " + person["RemarkName"] + " " + person["UserName"])
if len(searchedList) > 1:
contactToId = input("请输入需要联系的人员或群ID:")
else:
contactToId = searchedList[0]["UserName"]
wxMessage = input("请输入消息:\n")
if wxMessage != "":
if os.path.exists(wxMessage):
self.bot.webwxuploadmedia(self.wxToken,wxMessage)
self.bot.webwxsendmsgimg(self.wxToken, contactToId , media_id)
# self.bot.webwxuploadmedia(self, image_name):
# self.bot.sendImage(self.wxToken, contactToId, wxMessage)
else:
self.bot.sendMessage(self.wxToken, contactToId, wxMessage)
def exportContactUI(self):
print("正在尝试导出联系人列表")
self.bot.exportContect(self.wxContacts)
print("已导出联系人列表到friendslist.xlsx")
def locationStaticUI(self):
print("正在针对联系人地域信息进行统计:\n", end="")
locationStatic = {}
for person in self.wxContacts["MemberList"]:
personLocation = person["Province"]#+person["City"]
if personLocation == "":
continue
if personLocation not in locationStatic:
locationStatic[personLocation] = 1
else:
locationStatic[personLocation] += 1
locationStaticTmp = sorted(locationStatic.items(), key=lambda x: x[1], reverse=True)
locationStatic = []
for element in locationStaticTmp[:10]:
locationStatic.append(element)
self.bot.exportECharts(locationStatic)
print("已完成")
def exportGroupUI(self):
print('导出群组信息')
self.bot.exportGroup(self.wxContacts)