-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdamatuWeb.py
121 lines (103 loc) · 3.63 KB
/
damatuWeb.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
#python版本3.4
import hashlib
import http.client
import urllib.request
import urllib
import json
import base64
def md5str(str): #md5加密字符串
m=hashlib.md5(str.encode(encoding = "utf-8"))
return m.hexdigest()
def md5(byte): #md5加密byte
return hashlib.md5(byte).hexdigest()
class DamatuApi():
ID = '40838'
KEY = 'ca9507e17e8d5ddf7c57cd18d8d33010'
HOST = 'http://api.dama2.com:7766/app/'
def __init__(self,username,password):
self.username=username
self.password=password
def getSign(self,param=b''):
return (md5(bytes(self.KEY, encoding = "utf8") + bytes(self.username, encoding = "utf8") + param))[:8]
def getPwd(self):
return md5str(self.KEY +md5str(md5str(self.username) + md5str(self.password)))
def post(self,path,params={}):
data = urllib.parse.urlencode(params).encode('utf-8')
url = self.HOST + path
response = urllib.request.Request(url,data)
return urllib.request.urlopen(response).read()
#查询余额 return 是正数为余额 如果为负数 则为错误码
def getBalance(self):
data={'appID':self.ID,
'user':self.username,
'pwd':self.getPwd(),
'sign':self.getSign()
}
res = self.post('d2Balance',data)
res = str(res, encoding = "utf-8")
jres = json.loads(res)
if jres['ret'] == 0:
return jres['balance']
else:
return jres['ret']
#上传验证码 参数filePath 验证码图片路径 如d:/1.jpg type是类型,查看http://wiki.dama2.com/index.php?n=ApiDoc.Pricedesc return 是答案为成功 如果为负数 则为错误码
def decode(self,filePath,type):
f=open(filePath,'rb')
fdata=f.read()
filedata=base64.b64encode(fdata)
f.close()
data={'appID':self.ID,
'user':self.username,
'pwd':self.getPwd(),
'type':type,
'fileDataBase64':filedata,
'sign':self.getSign(fdata)
}
res = self.post('d2File',data)
res = str(res, encoding = "utf-8")
jres = json.loads(res)
if jres['ret'] == 0:
#注意这个json里面有ret,id,result,cookie,根据自己的需要获取
return(jres['result'])
else:
return jres['ret']
#url地址打码 参数 url地址 type是类型(类型查看http://wiki.dama2.com/index.php?n=ApiDoc.Pricedesc) return 是答案为成功 如果为负数 则为错误码
def decodeUrl(self,url,type):
data={'appID':self.ID,
'user':self.username,
'pwd':self.getPwd(),
'type':type,
'url':urllib.parse.quote(url),
'sign':self.getSign(url.encode(encoding = "utf-8"))
}
res = self.post('d2Url',data)
res = str(res, encoding = "utf-8")
jres = json.loads(res)
if jres['ret'] == 0:
#注意这个json里面有ret,id,result,cookie,根据自己的需要获取
return(jres['result'])
else:
return jres['ret']
#报错 参数id(string类型)由上传打码函数的结果获得 return 0为成功 其他见错误码
def reportError(self,id):
#f=open('0349.bmp','rb')
#fdata=f.read()
#print(md5(fdata))
data={'appID':self.ID,
'user':self.username,
'pwd':self.getPwd(),
'id':id,
'sign':self.getSign(id.encode(encoding = "utf-8"))
}
res = self.post('d2ReportError',data)
res = str(res, encoding = "utf-8")
jres = json.loads(res)
return jres['ret']
#调用类型实例:
#1.实例化类型 参数是打码兔用户账号和密码
# dmt=DamatuApi("test","test")
#2.调用方法:
#print(dmt.getBalance()) #查询余额
#print(dmt.decode('0349.bmp',200)) #上传打码
#print(dmt.decodeUrl('http://captcha.qq.com/getimage?aid=549000912&r=0.7257105156128585&uin=3056517021',200)) #上传打码
#print(dmt.reportError('894657096')) #上报错误