-
Notifications
You must be signed in to change notification settings - Fork 255
Expand file tree
/
Copy pathaccount.py
More file actions
126 lines (112 loc) · 4.62 KB
/
account.py
File metadata and controls
126 lines (112 loc) · 4.62 KB
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
# encoding: utf-8
import logging
import re
from zhihu.models import Model
from zhihu.models import RequestDataType
from zhihu.url import URL
from zhihu.error import ZhihuError
class Account(Model):
def __init__(self, **kwargs):
"""
初始化
"""
super(Account, self).__init__(**kwargs)
def login(self, account, password, **kwargs):
"""
账户登录
:param account: email或者手机号码
:param password:
:param kwargs:
:return:
"""
email_regex = r"(^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$)"
phone_regex = r"\+?\d{10,15}$"
email_pattern = re.compile(email_regex)
phone_pattern = re.compile(phone_regex)
if email_pattern.match(account):
return self._login_with_email(account, password, **kwargs)
elif phone_pattern.match(account):
return self._login_with_phone(account, password, **kwargs)
else:
self.log("无效的用户名", level=logging.ERROR)
return {"r": 1, "msg": "无效的用户名"}
def _login_with_phone(self, phone, password, **kwargs):
data = {
'_xsrf': self._get_xsrf(),
'password': password,
'phone_num': phone,
"captcha": self._get_captcha(),
"remeber_me": "true",
}
return self._login_execute(url=URL.phone_login(), data=data, **kwargs)
def _login_with_email(self, email, password, **kwargs):
data = {'email': email,
'password': password,
'_xsrf': self._get_xsrf(**kwargs),
"captcha": self._get_captcha(**kwargs),
'remember_me': 'true'}
return self._login_execute(url=URL.email_login(), data=data, **kwargs)
def _login_execute(self, url=None, data=None, **kwargs):
#r = super(Account, self)._execute(method="post", url=url, data=data, data_type=RequestDataType.FORM_DATA,
# **kwargs)
r = self._execute(method="post", url=url, data=data, data_type=RequestDataType.FORM_DATA,
**kwargs)
if r.ok:
result = r.json()
if result.get("r") == 0:
self.log(result.get("msg"))
self._session.cookies.save(ignore_discard=True) # 保存登录信息cookies
return result
else:
self.log(result.get("msg"), level=logging.ERROR)
return result
else:
self.log("登录失败", level=logging.ERROR)
self.log(r.text)
return {'r': 1, "msg": "登录失败"}
def _register_validate(self, data):
"""
注册前的验证,是否已经注册
:return:
"""
r = super(Account, self)._execute(method="post",
url=URL.register_validate(),
data=data,
data_type=RequestDataType.FORM_DATA)
if r.ok and r.json().get("r") == 0:
return True
else:
if r.ok and r.json().get("r") == 1:
self.log(r.text)
return False
def register(self, name=None, phone_num=None, password=None):
data = {
"fullname": name,
"phone_num": phone_num,
"password": password,
"_xsrf": super(Account, self)._get_xsrf(),
"captcha": super(Account, self)._get_captcha(_type="register"),
"captcha_source": "register",
}
valid = self._register_validate(data)
if valid:
self.log("账号验证成功,发送短信验证码")
params = {"phone_num": phone_num, "captcha_source": "register"}
# 发送验证码
r = self._execute(method="get", url=URL.register_sms_code(), params=params)
self.log(r.json().get("msg"))
code = input("输入短信验证码:")
data['verification_code'] = code
data.pop("captcha")
r = self._execute(method="post", url=URL.register(), data=data, data_type=RequestDataType.FORM_DATA)
if r.ok and r.json().get("r") == 0:
self.log("注册成功")
return r.json()
else:
if r.ok and r.json().get("r") != 0:
self.log(r.json().get("msg"), level=logging.ERROR)
if not r.ok:
self.log("请求失败", level=logging.ERROR)
return r.json()
else:
raise ZhihuError("验证失败,请查看日志")