-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsmartqq.py
380 lines (355 loc) · 15.5 KB
/
smartqq.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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
# -*- coding: utf-8 -*-
__author__ = 'lius'
import tkinter
import requests
import random
import time
import json
from multiprocessing import Process
class SmartQQ():
def __init__(self):
self.headers = {'User-Agent': "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.75 Safari/537.36 LBBROWSER",
'Referer' : 'https://ui.ptlogin2.qq.com/cgi-bin/login?daid=164&target=self&style=16&mibao_css=m_webqq&appid=501004106&enable_qlogin=0&no_verifyimg=1&s_url=http%3A%2F%2Fw.qq.com%2Fproxy.html&f_url=loginerroralert&strong_login=1&login_state=10&t=20131024001'}
self.ssession= requests.session()
self.ssession.headers.update(self.headers)
self.cookies_qrsig = ""
self.ptwebqq = ""
self.vfwebqq = ""
self.psessionid = ""
self.uin = 0
self.face = 0
self.qqnum = 0
self.qqname = ''
self.getmsgcount = 0
def show_QRC(self,content):
'''
通过 tkinter 显示二维码.
'''
if(content != None):
root = tkinter.Tk() #显示二维码
root.title("扫描二维码")
root.geometry('200x150') #设置窗口大小
root.resizable(width=False, height=False) #窗口大小不可变
size = '%dx%d+%d+%d' % (200, 150, (root.winfo_screenwidth() - 200) / 2, (root.winfo_screenheight() - 150) / 2)
root.geometry(size)
img = tkinter.PhotoImage(data=content)
label = tkinter.Label(root, image=img)
label.pack()
root.mainloop()
else:
print("二维码获取失败.")
def get_ptqrtoken(self):
'''
# ptqrtoken 计算
'''
token = 0
for i in range(len(self.cookies_qrsig)):
token += (token << 5) + ord(self.cookies_qrsig[i])
return 2147483647 & token
def check_login_status(self,p):
'''
# 登录状态检测
'''
url = "https://ssl.ptlogin2.qq.com/ptqrlogin?ptqrtoken=" + str(self.get_ptqrtoken()) +\
"&webqq_type=10&remember_uin=1&login2qq=1&aid=501004106" \
"&u1=http%3A%2F%2Fw.qq.com%2Fproxy.html%3Flogin2qq%3D1%26webqq_type%3D10" \
"&ptredirect=0&ptlang=2052&daid=164&from_ui=1&pttype=1&dumy=&fp=loginerroralert" \
"&action=0-0-" + str(random.randint(1000, 30000)) +\
"&mibao_css=m_webqq&t=1&g=1&js_type=0&js_ver=10216&login_sig=&pt_randsalt=2"
# 死循环检测扫码登陆状态
while 1:
content = self.ssession.get(url=url).content.decode("utf-8")
# 登陆成功跳出循环
if "二维码" not in content:
data = content.split(',')
# print(data)
print("二维码认证成功.\n登录用户:%s\n" % (data[5])[:-3])
# 结束进程 p
if p.is_alive():
p.terminate()
self.login_status = True
return data[2].replace("'","")
print("二维码认证中.....(请用手机QQ扫描二维码并确认登陆.)")
time.sleep(3)
def login(self):
'''
# qq登陆过程
'''
# 下面 url 中 t = ?,?为获取二维码请求随机数。
url = "https://ssl.ptlogin2.qq.com/ptqrshow?appid=501004106&e=0&l=M&s=5&d=72&v=4&t=0.562284958449" + str(random.randint(10000,100000))
content = self.ssession.get(url=url).content #获取二维码数据(二进制)
self.cookies_qrsig = (requests.utils.dict_from_cookiejar(self.ssession.cookies))["qrsig"]
#使用进程显示二维码,等待扫描。
p = Process(target=self.show_QRC,args=(content,))
#启动进程 p
p.start()
time.sleep(1)
#获取验证成功后返回的请求地址
url = self.check_login_status(p)
self.headers.pop("Referer")
self.headers['Upgrade-Insecure-Requests'] = "1"
self.ssession.headers.update(self.headers)
# 请求获取 cookies 中 ptwebqq
self.ssession.get(url=url)
self.ptwebqq = (requests.utils.dict_from_cookiejar(self.ssession.cookies))["ptwebqq"]
# print("ptwebqq : %s" % self.ptwebqq)
# 请求获取 json 中 vfwebqq
url = "http://s.web2.qq.com/api/getvfwebqq?ptwebqq=" + str(self.ptwebqq) +\
"&clientid=53999199&psessionid=&t=1493177741164"
j_data = json.loads(self.ssession.get(url=url).content.decode("utf-8"))
self.vfwebqq = j_data["result"]["vfwebqq"]
# print("vfwebqq : %s" % self.vfwebqq)
# 请求获取 psessionid uin
self.headers.pop("Upgrade-Insecure-Requests")
self.headers["Referer"] = "http://d1.web2.qq.com/proxy.html?v=20151105001&callback=1&id=2"
self.headers["Origin"] = "http://d1.web2.qq.com"
self.ssession.headers.update(self.headers)
url = "http://d1.web2.qq.com/channel/login2"
p_data = {"ptwebqq": str(self.ptwebqq),"clientid":53999199,"psessionid":"","status":"online"}
r_data = {"r":json.dumps(p_data)}
j_data = json.loads(self.ssession.post(url=url,data=r_data).content.decode("utf-8"))
self.psessionid = j_data["result"]["psessionid"]
self.uin = j_data["result"]["uin"]
# print("psessionid : %s" % self.psessionid)
print("恭喜,SmartQQ登录成功。\n")
def get_hash(self):
'''
# hash 值计算
'''
uin = int(self.uin)
ptwebqq = self.ptwebqq
ptb = [0,0,0,0]
for i in range(len(ptwebqq)):
ptb[i % 4] ^= ord(ptwebqq[i])
uin = int(uin)
uinByte = [0,0,0,0]
uinByte[0] = uin >> 24 & 255 ^ 69 # E
uinByte[1] = uin >> 16 & 255 ^ 67 # C
uinByte[2] = uin >> 8 & 255 ^ 79 # O
uinByte[3] = uin & 255 ^ 75 # K
result = [0 for x in range(8)]
for i in range(0,8):
if(i % 2 == 0):
result[i] = ptb[i >> 1]
else:
result[i] = uinByte[i >> 1]
hex = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F']
buf = ""
for i in result:
buf += hex[i >> 4 & 15]
buf += hex[i & 15]
return buf
def recur_list(self,lst):
'''
递归将list的元素处理成字符串,函数主要针对list内有list。
'''
con = ""
for ele in lst:
if isinstance(ele, list):
con += self.recur_list(ele)
else:
con += str(ele)
return con
def get_self_info(self):
'''
# 获取个人信息
'''
url = "http://s.web2.qq.com/api/get_self_info2?t=1493263376886"
try:
j_data = json.loads(self.ssession.get(url=url).content.decode("utf-8"))
self.face = j_data["result"]["face"]
self.qqname = j_data["result"]["nick"]
self.qqaccount = j_data["result"]["account"]
print("我的QQ资料:%s" % j_data["result"])
return j_data["result"]
except:
return None
def get_group_list(self):
'''
# 获取QQ群
'''
url = "http://s.web2.qq.com/api/get_group_name_list_mask2"
p_data = {"vfwebqq": str(self.vfwebqq), "hash": self.get_hash()}
r_data = {"r": json.dumps(p_data)}
try:
j_data = json.loads(self.ssession.post(url=url, data=r_data).content.decode("utf-8"))
groups_data = { k['name']: k for k in j_data["result"]["gnamelist"] }
print("QQ群:%s" % groups_data)
return groups_data
except:
return None
def get_friends_info(self):
'''
# 获取QQ好友
'''
url = "http://s.web2.qq.com/api/get_user_friends2"
p_data = {"vfwebqq": str(self.vfwebqq),"hash": self.get_hash()}
r_data = {"r": json.dumps(p_data)}
try:
j_data = json.loads(self.ssession.post(url=url, data=r_data).content.decode("utf-8"))
friends_data = { k['nick']: k for k in j_data["result"]["info"] }
print("QQ好友信息:%s" % friends_data)
return friends_data
except:
return None
def get_chat_msg(self):
'''
# 接收消息
'''
if self.getmsgcount > 40:
self.get_online_buddies2()
self.getmsgcount = 0
else:
self.getmsgcount += 1
get_msg_url = "https://d1.web2.qq.com/channel/poll2"
self.headers["Origin"] = "https://d1.web2.qq.com"
self.headers["Referer"] = "https://d1.web2.qq.com/cfproxy.html?v=20151105001&callback=1"
self.ssession.headers.update(self.headers)
p_data = {"ptwebqq": str(self.ptwebqq),
"clientid": 53999199,
"psessionid": str(self.psessionid),
"key": ""}
r_data = {"r": json.dumps(p_data)}
try:
j_data = json.loads(self.ssession.post(url=get_msg_url, data=r_data).content.decode("utf-8"))
if "errmsg" in j_data.keys():
print(j_data)
return None
if "result" in j_data.keys():
content = self.recur_list(j_data["result"][0]["value"]["content"][1:])
data = {"poll_type": j_data["result"][0]["poll_type"],
"from_uin": j_data["result"][0]["value"]["from_uin"],
"content": content,
}
if data["poll_type"] == "group_message":
data["send_uin"] = j_data["result"][0]["value"]["send_uin"]
else:
data["send_uin"] = None
return data
return None
except:
print('Fetch message exception!')
return None
# {"result":[{"poll_type":"group_message",
# "value":{
# "content":[["font",{"color":"000000","name":"微软雅黑","size":10,"style":[0,0,0]}],"好"],
# "from_uin":2324333159,
# "group_code":2324333159,
# "msg_id":18317,"msg_type":4,
# "send_uin":1550070579,"time":1496933934,"to_uin":979885605}}],"retcode":0}
# {"result":[{"poll_type":"message",
# "value":{
# "content":[["font",{"color":"000000","name":"微软雅黑","size":10,"style":[0,0,0]}],"哈哈哈哈"],
# "from_uin":1550070579,
# "msg_id":18321,
# "msg_type":1,
# "time":1496934177,
# "to_uin":979885605}}],"retcode":0}
def get_online_buddies2(self):
'''
# 获取QQ在线好友
'''
url = "http://d1.web2.qq.com/channel/get_online_buddies2?vfwebqq=" + str(self.vfwebqq) +\
"&clientid=53999199&psessionid="+ str(self.psessionid) +\
"&t=149429685" + str(random.randint(1000,10000))
# headers = {"User-Agent":self.headers["User-Agent"]}
# headers["Host"] = "d1.web2.qq.com"
# headers["Referer"] = "http://d1.web2.qq.com/proxy.html?v=20151105001&callback=1&id=2"
# self.ssession.headers.update(headers)
try:
j_data = json.loads(self.ssession.get(url=url).content.decode("utf-8"))
print("QQ在线好友:%s" % j_data["result"])
return j_data["result"]
except:
return None
def get_recent_list2(self):
'''
# 获取最近列表
'''
url = "http://d1.web2.qq.com/channel/get_recent_list2"
# self.headers["Host"] = "d1.web2.qq.com"
# self.headers["Origin"] = "http://d1.web2.qq.com"
# self.headers["Referer"] = "http://d1.web2.qq.com/proxy.html?v=20151105001&callback=1&id=2"
# self.ssession.headers.update(self.headers)
p_data = {"vfwebqq":str(self.vfwebqq),"clientid":53999199,"psessionid": str(self.psessionid)}
r_data = {"r": json.dumps(p_data)}
try:
j_data = json.loads(self.ssession.post(url=url, data=r_data).content.decode("utf-8"))
if "errmsg" in j_data.keys():
print(j_data)
return None
print("QQ最近列表:%s" % j_data["result"])
return j_data["result"]
except:
return None
def send_qun_msg(self,group_uin,msg):
'''
# 发送QQ群信息
'''
url = "https://d1.web2.qq.com/channel/send_qun_msg2"
p_data = {"group_uin":group_uin,
"content":'[\"' + str(msg) + '\",[\"font\",{\"name\":\"宋体\",\"size\":10,\"style\":[0,0,0],\"color\":\"000000\"}]]',
"face":self.face,
"clientid":53999199,
"msg_id":1530000 + random.randint(1000,10000),
"psessionid":str(self.psessionid)}
r_data = {"r": json.dumps(p_data)}
j_data = json.loads(self.ssession.post(url=url, data=r_data).content.decode("utf-8"))
if "errCode" in j_data.keys() and j_data["msg"] == "send ok":
print("群消息发成功.")
return True
else:
print("群消息发失败.")
return False
def send_buddy_msg(self,uin,msg):
'''
# 发送QQ好友消息
'''
url = "https://d1.web2.qq.com/channel/send_buddy_msg2"
p_data = {"to": uin,
"content": "[\""+ str(msg) +"\",[\"font\",{\"name\":\"宋体\",\"size\":10,\"style\":[0,0,0],\"color\":\"000000\"}]]",
"face": self.face,
"clientid": 53999199,
"msg_id": 1530000 + random.randint(1000,10000),
"psessionid": str(self.psessionid)}
r_data = {"r": json.dumps(p_data)}
j_data = json.loads(self.ssession.post(url=url, data=r_data).content.decode("utf-8"))
if "errCode" in j_data.keys() and j_data["msg"] == "send ok":
print("好友消息发成功.")
return True
else:
print("好友消息发失败.")
return False
def get_self_img(self):
'''
# 获取自己的头像
'''
# self.headers["Accept"] = "image/webp,image/*,*/*;q=0.8"
# self.headers["Host"] = "q.qlogo.cn"
# self.headers["Referer"] = "http://w.qq.com/"
self.ssession.headers.update(self.headers)
url = 'http://q.qlogo.cn/g?b=qq&nk='+ str(self.uin) +'&s=100&t=149673935' + str(random.randint(1000, 10000))
try:
content = self.ssession.get(url=url).content
return content
except:
return None
def get_group_info(self,gcode):
'''
# 获取群资料
'''
# self.headers["Host"] = "s.web2.qq.com"
# self.headers["Referer"] = "http://s.web2.qq.com/proxy.html?v=20130916001&callback =1&id=1"
self.ssession.headers.update(self.headers)
url = 'http://s.web2.qq.com/api/get_group_info_ext2?gcode=' + str(gcode) + '&vfwebqq='+ str(self.vfwebqq) +'&t=149734532' + str(random.randint(1000, 10000))
try:
j_data = json.loads(self.ssession.get(url=url).content.decode("utf-8"))
if "result" in j_data.keys():
print("群成员信息:%s" % j_data['result']['minfo'])
return {"ginfo":{"name":j_data['result']['ginfo']['name'],"gid":j_data['result']['ginfo']['gid']},"minfo":j_data['result']['minfo']}
else:
print("未获取到群资料. %s" % j_data)
return None
except:
print("获取群资料异常.")
return None