forked from sausame/coupon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wx.py
195 lines (126 loc) · 5.63 KB
/
wx.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
#!/usr/bin/env python
# -*- coding:utf-8 -*-
import random
import itchat
import time
from qwd import QWD
from schedule import Schedule
from search import SearchingKeyRegex
from special import Searcher
from utils import getProperty, randomSleep, reprDict
class WX(Schedule):
def __init__(self, configFile, shareFile):
self.qwd = QWD(shareFile)
Schedule.__init__(self, configFile, self.qwd)
self.searcher = Searcher(configFile, self.qwd)
self.configFile = configFile
def login(self, exitCallback, uuid=None):
def isLoginned(uuid):
for count in range(10):
status = int(itchat.check_login(uuid))
if status is 200:
return True
if status is 201:
print 'Wait for confirm in mobile #', count
randomSleep(1, 2)
continue
print 'Error status:', status
return False
return False
if uuid is None:
statusFile = getProperty(self.configFile, 'wechat-status-file')
itchat.auto_login(hotReload=True, statusStorageDir=statusFile)
else:
if not isLoginned(uuid):
raise Exception('Failed to login with {}'.format(uuid))
userInfo = itchat.web_init()
itchat.show_mobile_login()
itchat.get_friends(True)
itchat.start_receiving(exitCallback)
self.me = itchat.search_friends()
print self.me['NickName'], 'is working'
self.updateWatchers()
self.searchReplyPlate = getProperty(self.configFile, 'search-reply-plate')
self.searchNotFoundReplyPlate = getProperty(self.configFile, 'search-not-found-reply-plate')
self.searchFoundReplyPlate = getProperty(self.configFile, 'search-found-reply-plate')
itchat.run(blockThread=False) # Run in a new thread
self.run()
def updateWatchers(self):
self.watchFriends = list()
names = getProperty(self.configFile, 'wechat-watch-friends').split(';')
for name in names:
friends = itchat.search_friends(name=name)
self.watchFriends.extend(friends)
self.friendsActions = getProperty(self.configFile, 'wechat-friends-actions').split('|')
self.watchGroups = list()
names = getProperty(self.configFile, 'wechat-watch-groups').split(';')
for name in names:
groups = itchat.search_chatrooms(name=name)
self.watchGroups.extend(groups)
self.groupsActions = getProperty(self.configFile, 'wechat-groups-actions').split('|')
@staticmethod
def sendTo(obj, plate=None, image=None):
print '================================================================'
print 'Send a message to', obj['NickName']
if plate is not None:
interval = random.random() * 10
time.sleep(interval)
ret = obj.send(plate)
print 'Result of text message:', ret['BaseResponse']['ErrMsg']
print '----------------------------------------------------------------'
print plate
print '----------------------------------------------------------------'
if image is not None:
interval = random.random() * 10
time.sleep(interval)
ret = obj.send_image(image)
print 'Result of', image, ':', ret['BaseResponse']['ErrMsg']
print '================================================================'
def text(self, msg):
if 'search' not in self.friendsActions:
return
for friend in self.watchFriends:
if msg['FromUserName'] == friend['UserName']:
break
else:
return
print '================================================================'
print msg['User']['NickName'], 'sends a message:'
print '----------------------------------------------------------------'
print msg['Content']
print '================================================================'
self.search(friend, msg['Content'])
def textInGroup(self, msg):
if 'search' not in self.groupsActions:
return
for group in self.watchGroups:
if msg['FromUserName'] == group['UserName']:
break
else:
return
print '================================================================'
print msg['User']['NickName'], 'sends a message:'
print '----------------------------------------------------------------'
print msg['Content']
print '================================================================'
self.search(group, msg['Content'])
def send(self, plate, image):
if 'schedule' in self.friendsActions:
for friend in self.watchFriends:
WX.sendTo(friend, plate, image)
if 'schedule' in self.groupsActions:
for group in self.watchGroups:
WX.sendTo(group, plate, image)
def search(self, obj, content):
content = SearchingKeyRegex.parse(content)
if content is None:
return
print 'Searching', content
WX.sendTo(obj, self.searchReplyPlate.format(content.replace('#', ' ')))
template = self.searchNotFoundReplyPlate
if self.searcher.search(content):
template = self.searchFoundReplyPlate
else:
template = self.searchNotFoundReplyPlate
WX.sendTo(obj, template.format(content.replace('#', ' ')))
WX.sendTo(obj, self.searcher.plate, self.searcher.image)