-
Notifications
You must be signed in to change notification settings - Fork 0
/
qqweb.py
99 lines (88 loc) · 3.42 KB
/
qqweb.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
#!/usr/bin/python
#QQDown -- An open-sourced implementation of tencent offline download
#Copyright (C) 2011-2012 Tydus <Tydus@Tydus.org>
#
#This program is free software: you can redistribute it and/or modify
#it under the terms of the GNU General Public License as published by
#the Free Software Foundation, either version 3 of the License, or
#(at your option) any later version.
#
#This program is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
#GNU General Public License for more details.
#
#You should have received a copy of the GNU General Public License
#along with this program. If not, see <http://www.gnu.org/licenses/>.
# QQWeb interface
import json_rpc
from random import random
from hashlib import md5
from string import upper
from re import sub
class LoginError(Exception): pass
def md5_3(a):
''' Performs an 3-time MD5 (md5_3 as Tencent spec) '''
a=md5(a).digest()
a=md5(a).digest()
return upper(md5(a).hexdigest())
def prompt_for_verifycode(vc):
from os import system
fn='/tmp/vc.png'
fd=open(fn,'wb')
fd.write(vc)
fd.close()
system("display "+fn)
return raw_input('Input verifycode: ')
def func2list(x):
import pdb
pdb.set_trace()
x=sub(r'^try{(.+)}catch\(.*\){.*};?$',r'\1',x)
x=sub(r'^(\w+)\((.+)\);?',r"['\1',\2]",x)
return eval(x)
class QQWeb(json_rpc.Json_RPC):
def __init__(self,username,password,is_pass_md53=False,nologin=False):
self.username=username
json_rpc.Json_RPC.__init__(self)
if not is_pass_md53:
password=md5_3(password)
if nologin:
return
appid=567008010
# Get Verify Code
ret=func2list(self.http_rpc("http://ptlogin2.qq.com/check",
query=dict(uin=username,appid=appid,r=random())
))
verifycode=ret[2]
if ret[1]=='1':
ret=self.http_rpc("http://captcha.qq.com/getimage",
query=dict(uin=username,
aid=appid,
r=random(),
vc_type=verifycode)
)
verifycode=prompt_for_verifycode(ret)
password_md5=md5(password+verifycode.upper()).hexdigest()
ret=func2list(
self.http_rpc("http://ptlogin2.qq.com/login",
query=dict(u=username,
p=password_md5,
verifycode=verifycode,
aid=appid,
u1="http://lixian.qq.com/main.html",
h=1,
# ptredirect=1,
# ptlang=2052,
from_ui=1,
# dumy="",
# mibao_css="",
fp="loginerroralert"
# action="2-13-237255",
)
)
)
if ret[1]!='0':
# Login Failed
raise LoginError((ret[1],ret[5]))
def __repr__(self):
return "<qqweb.QQWeb object with user '%s'>"%self.username