-
Notifications
You must be signed in to change notification settings - Fork 105
/
Copy pathHistoryRecorder.py
37 lines (35 loc) · 1.33 KB
/
HistoryRecorder.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
from time import time
from datetime import datetime
from ProcessInterface import ProcessInterface
from utilities import *
from itchat.content import *
import os
class HistoryRecorder(ProcessInterface):
def __init__(self):
self.client = client
self.coll = self.client[dbName][collName]
self.imgFolder = 'HistoryImages'
if not os.path.exists(self.imgFolder):
os.mkdir(self.imgFolder)
logging.info('HistoryRecorder initialized.')
# Record an itchat message to mongodb
# Currently only support text messages in group chats
def process(self, msg, type):
if type == PICTURE:
fn = msg['FileName']
newfn = os.path.join(self.imgFolder, fn)
msg['Text'](fn)
os.rename(fn, newfn)
msg['Content'] = '<<<IMG:{0}>>>'.format(newfn)
if type == TEXT or type == PICTURE:
timestamp = time()
rtime = datetime.utcfromtimestamp(timestamp)
r = {
'content': msg['Content'],
'from': msg['ActualNickName'],
'fromId': msg['ToUserName'],
'to': msg['User']['NickName'] if 'User' in msg and 'UserName' in msg['User'] else 'N/A',
'timestamp': timestamp,
'time': rtime
}
self.coll.insert(r)