-
Notifications
You must be signed in to change notification settings - Fork 8
/
yundama.py
71 lines (65 loc) · 1.62 KB
/
yundama.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
'''
Create on 2016-11-8
@author: latelan
'''
import json
import time
import requests
class YunDaMa(object):
'''
docstring for YunDaMa
'''
def __init__(self, username, password, appid=None, appkey=None):
self.base_url = "http://api.yundama.com/api.php"
self.username = username
self.password = password
self.appid = "2859" if not appid else appid
self.appkey = "550644178743dfe5cf36aa7032b3c1d3" if not appkey else appkey
self.req = requests.Session()
def get_captcha(self, filebytes, filename, filetype, codetype='1000', repeat=15):
'''
default timeout is 30s
'''
cid = self.upload(filebytes, filename, filetype, codetype)
if not cid:
return None
while repeat > 0:
code = self.result(cid)
if code:
return code
repeat -= 1
time.sleep(2)
return None
def upload(self, filebytes, filename, filetype, codetype):
data = {
'method': 'upload',
'username': self.username,
'password': self.password,
'codetype': codetype,
'appid': self.appid,
'appkey': self.appkey,
'timeout': 60
}
file = {
'file': (filename, filebytes, filetype)
}
try:
result = self.req.post(self.base_url, data=data, files=file).json()
except Exception as e:
raise
return result['cid']
def result(self, cid):
data = {
'method': 'result',
'cid': cid
}
try:
result = self.req.post(self.base_url, data=data).json()
except Exception as e:
raise
if result['text']:
return result['text']
if __name__ == '__main__':
ydm = YunDaMa(username, password)
code = ydm.get_captcha(requests.get('https://passport.bilibili.com/captcha').content, 'captcha.jpg', 'image/jpeg')
print code