-
Notifications
You must be signed in to change notification settings - Fork 60
/
QQ_History.py
297 lines (265 loc) · 10 KB
/
QQ_History.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
import hashlib
import sqlite3
import time
import os
import traceback
import json
import base64
from proto.RichMsg_pb2 import PicRec
from proto.RichMsg_pb2 import Elem
from proto.RichMsg_pb2 import Msg
_crc64_init = False
_crc64_table = [0] * 256
def crc64(s):
global _crc64_init
if not _crc64_init:
for i in range(256):
bf = i
for j in range(8):
if bf & 1 != 0:
bf = bf >> 1 ^ -7661587058870466123
else:
bf >>= 1
_crc64_table[i] = bf
_crc64_init = True
v = -1
for i in range(len(s)):
v = _crc64_table[(ord(s[i]) ^ v) & 255] ^ v >> 8
return v
class QQoutput():
def __init__(self, path, qq_self, qq, mode, emoji, with_img, combine_img):
self.db_path = path
self.key = self.get_key() # 解密用的密钥
db = os.path.join(path, "databases", qq_self + ".db")
self.c1 = sqlite3.connect(db).cursor()
db = os.path.join(path, "databases", "slowtable_" + qq_self + ".db")
self.c2 = sqlite3.connect(db).cursor()
self.qq_self = qq_self
self.qq = qq
self.mode = mode
self.emoji = emoji
self.with_img = with_img
self.combine_img = combine_img
self.num_to_name = {}
self.emoji_map = self.map_new_emoji()
def decrypt(self, data, msg_type=-1000):
msg = b''
if type(data) == bytes:
msg = b''
for i in range(0, len(data)):
msg += bytes([data[i] ^ ord(self.key[i % len(self.key)])])
elif type(data) == str:
msg = ''
for i in range(0, len(data)):
msg += chr(ord(data[i]) ^ ord(self.key[i % len(self.key)]))
return msg
if msg_type == -1000 or msg_type == -1049 or msg_type == -1051:
try:
return msg.decode('utf-8')
except:
# print(msg)
pass
return '[decode error]'
if not self.with_img:
return None
elif msg_type == -2000:
return self.decode_pic(msg)
elif msg_type == -1035:
return self.decode_mix_msg(msg)
elif msg_type == -5008:
return self.decode_share_url(msg)
elif msg_type == -5012 or msg_type == -5018:
return '[戳一戳]'
# for debug
# return '[unknown msg_type {}]'.format(msg_type)
return None
def add_emoji(self, msg):
pos = msg.find('\x14')
while pos != -1:
lastpos = pos
num = ord(msg[pos + 1])
if str(num) in self.emoji_map:
index = self.emoji_map[str(num)]
if self.emoji == 1:
filename = "new/s" + index + ".png"
else:
filename = "old/" + index + ".gif"
emoticon_path = os.path.join('emoticon', filename)
if self.combine_img:
emoticon_path = self.get_base64_from_pic(emoticon_path)
msg = msg.replace(
msg[pos:pos + 2], '<img src="{}" alt="{}" />'.format(emoticon_path, index))
else:
msg = msg.replace(msg[pos:pos + 2],
'[emoji:{}]'.format(str(num)))
pos = msg.find('\x14')
if pos == lastpos:
break
return msg
def message(self):
# mode=1 friend
# mode=2 troop
num = self.qq.encode("utf-8")
md5num = hashlib.md5(num).hexdigest().upper()
if self.mode == 1:
cmd = "select msgData,senderuin,time,msgtype from mr_friend_{}_New order by time".format(
md5num)
self.get_friends()
else:
cmd = "select msgData,senderuin,time,msgtype from mr_troop_{}_New order by time".format(
md5num)
# print('Groups {} -> {}'.format(num, md5num))
self.get_troop_members()
cursors = self.fill_cursors(cmd)
allmsg = []
for cs in cursors:
for row in cs:
msgdata = row[0]
if not msgdata:
continue
uin = row[1]
ltime = time.localtime(row[2])
sendtime = time.strftime("%Y-%m-%d %H:%M:%S", ltime)
msg_type = row[3]
msg_final = self.decrypt(msgdata, msg_type)
if msg_final is None:
continue
allmsg.append(
[sendtime, msg_type, self.decrypt(uin), msg_final])
return allmsg
def get_friends(self):
cmd = "SELECT uin, remark FROM Friends"
cursors = self.fill_cursors(cmd)
for cs in cursors:
for row in cs:
num = self.decrypt(row[0])
name = self.decrypt(row[1])
self.num_to_name[num] = name
def get_troop_members(self):
cmd = "SELECT troopuin, memberuin, friendnick, troopnick FROM TroopMemberInfo"
cursors = self.fill_cursors(cmd)
for cs in cursors:
for row in cs:
if self.decrypt(row[0]) != self.qq:
continue
num = self.decrypt(row[1])
name = self.decrypt(row[3]) or self.decrypt(row[2])
self.num_to_name[num] = name
def fill_cursors(self, cmd):
cursors = []
# slowtable might not contain related message, so just skip it
try:
cursors.append(self.c2.execute(cmd))
except:
pass
cursors.append(self.c1.execute(cmd))
return cursors
def output(self):
name1 = "我"
file = str(self.qq) + ".html"
f2 = open(file, "w", encoding="utf-8")
f2.write(
"<head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" /></head>"
)
allmsg = self.message()
f2.write("<div style='white-space: pre-line'>")
for ts, _, uid, msg in allmsg:
if not msg:
continue
if uid == str(self.qq_self):
f2.write("<p align='right'>")
f2.write("<font color=\"green\">")
f2.write(ts)
f2.write("</font>-----<font color=\"blue\"><b>")
f2.write(name1)
f2.write("</font></b></br>")
else:
f2.write("<p align='left'>")
f2.write("<font color=\"blue\"><b>")
f2.write(self.num_to_name.get(uid) or uid)
f2.write("</b></font>-----<font color=\"green\">")
f2.write(ts)
f2.write("</font></br>")
f2.write(self.add_emoji(msg))
f2.write("</br></br>")
f2.write("</p>")
f2.write("</div>")
def get_key(self):
self.unify_path()
kc_path = os.path.join(self.db_path, "files", "kc")
kc_file = open(kc_path, "r")
return kc_file.read()
# unify databases path of different phones
def unify_path(self):
if os.path.isdir(os.path.join(self.db_path, "f")):
os.rename(os.path.join(self.db_path, "f"),
os.path.join(self.db_path, "files"))
if os.path.isdir(os.path.join(self.db_path, "db")):
os.rename(os.path.join(self.db_path, "db"),
os.path.join(self.db_path, "databases"))
if not os.path.isfile(os.path.join(self.db_path, "files", "kc")):
raise OSError(
"File not found. Please report your directory layout.")
def map_new_emoji(self):
with open('./emoticon/face_config.json', encoding='utf-8') as f:
emojis = json.load(f)
new_emoji_map = {}
for e in emojis['sysface']:
if self.emoji == 1:
new_emoji_map[e["AQLid"]] = e["QSid"]
else:
if len(e["EMCode"]) == 3:
new_emoji_map[e["AQLid"]] = str(int(e["EMCode"]) - 100)
return new_emoji_map
def get_base64_from_pic(self, path):
with open(path, "rb") as image_file:
return (b'data:image/png;base64,' + base64.b64encode(image_file.read())).decode("utf-8")
def decode_pic(self, data):
try:
doc = PicRec()
doc.ParseFromString(data)
url = 'chatimg:' + doc.md5
filename = hex(crc64(url))
filename = 'Cache_' + filename.replace('0x', '')
rel_path = os.path.join("./chatimg/", filename[-3:], filename)
if os.path.exists(rel_path):
w = 'auto' if doc.uint32_thumb_width == 0 else str(
doc.uint32_thumb_width)
h = 'auto' if doc.uint32_thumb_height == 0 else str(
doc.uint32_thumb_height)
if self.combine_img:
rel_path = self.get_base64_from_pic(rel_path)
return '<img src="{}" width="{}" height="{}" />'.format(rel_path, w, h)
except:
pass
return '[图片]'
def decode_mix_msg(self, data):
try:
doc = Msg()
doc.ParseFromString(data)
message = ''
for elem in doc.elems:
if elem.picMsg:
message += self.decode_pic(elem.picMsg)
else:
message += elem.textMsg.decode('utf-8')
return message
except:
pass
return '[混合消息]'
def decode_share_url(self, msg):
# TODO
return '[分享卡片]'
def main(db_path, qq_self, qq, mode, emoji, with_img, combine_img):
try:
q = QQoutput(db_path, qq_self, qq, mode, emoji, with_img, combine_img)
q.output()
except Exception as e:
with open('log.txt', 'w') as f:
f.write(repr(e))
f.write(traceback.format_exc())
print(traceback.format_exc())
if repr(e).split(":")[0] == "OperationalError('no such table":
raise ValueError("信息填入错误")
else:
raise BaseException("Error! See log.txt")