Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
lzjun567 committed Jul 25, 2017
1 parent 8b403fe commit c464551
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 26 deletions.
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,4 @@
"pytest",
]
)

13 changes: 8 additions & 5 deletions zhihu/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import requests.utils

from zhihu.models.account import Account
from .models.zhihu import Zhihu

try:
input = raw_input # py2
Expand All @@ -17,14 +18,16 @@ def need_login(func):
"""

def wrapper(self, *args, **kwargs):
success = False
if 'z_c0' not in requests.utils.dict_from_cookiejar(self._session.cookies):
while not success:
try:
pass
# Zhihu().user(user_slug='zhijun-liu')
except:
account = input("请输入Email或者手机号码:")
password = input("请输入密码:")
success = Account().login(account, password)
if success:
Account().login(account, password)
else:
self._session.cookies.load(ignore_discard=True)
return func(self, *args, **kwargs)
return func(self, *args, **kwargs)

return wrapper
2 changes: 1 addition & 1 deletion zhihu/error.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@

class ZhihuError(Exception):
def __init__(self, *args, **kwargs):
Exception.__init__(self, *args, **kwargs)
super(ZhihuError, self).__init__(*args, **kwargs)
39 changes: 19 additions & 20 deletions zhihu/url.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,109 +10,108 @@ class URL(object):
host = "https://www.zhihu.com"
zhuanlan_host = "https://zhuanlan.zhihu.com"

# 邮箱登录
@classmethod
def email_login(cls):
# 邮箱登录
return cls.host + "/login/email"

# 手机登录
@classmethod
def phone_login(cls):
# 手机登录
return cls.host + "/login/phone_num"

# 私信
@classmethod
def message(cls):
# 私信
return cls.host + "/api/v4/messages"

# 验证码
@classmethod
def captcha(cls, _type="login"):
# 验证码
return cls.host + "/captcha.gif?r={timestamp}&type={type}".format(timestamp=str(int(time.time() * 1000)),
type=_type)

# 首页
@classmethod
def index(cls):
# 首页
return cls.host + ""

# 用户信息
@classmethod
def profile(cls, user_slug):
# 用户信息
return cls.host + "/api/v4/members/{user_slug}".format(user_slug=user_slug)

# 关注用户
@classmethod
def follow_people(cls, user_slug):
# 关注用户
return cls.host + "/api/v4/members/{user_slug}/followers".format(user_slug=user_slug)

# 赞同/反对/中立
@classmethod
def vote_up(cls, answer_id):
# 赞同/反对/中立
return cls.host + "/api/v4/answers/{id}/voters".format(id=answer_id)

vote_down = vote_neutral = vote_up

# 某答案下感谢答主/取消感谢
@classmethod
def thank(cls, answer_id):
# 某答案下感谢答主/取消感谢
return cls.host + "/api/v4/answers/{id}/thankers".format(id=answer_id)

thank_cancel = thank

# 某答案没有帮助/撤销没有帮助
@classmethod
def nothelp(cls, answer_id):
# 某答案没有帮助/撤销没有帮助
return cls.host + "/api/v4/answers/{id}/unhelpers".format(id=answer_id)

nothelp_cancel = nothelp

# 关注某问题/取消关注某问题
@classmethod
def follow_question(cls, question_id):
# 关注某问题/取消关注某问题
return cls.host + "/api/v4/questions/{id}/followers".format(id=question_id)

unfollow_question = follow_question

# 专栏
@classmethod
def column(cls, slug):
# 专栏
return "https://zhuanlan.zhihu.com/api/columns/{slug}".format(slug=slug)

# 专栏主页
@classmethod
def column_index(cls, slug):
# 专栏主页
return cls.zhuanlan_host + "/{slug}".format(slug=slug)

# 专栏的关注者
@classmethod
def column_followers(cls, slug):
a = cls.zhuanlan_host + "/api/columns/{slug}/followers".format(slug=slug)
return a
# 专栏的关注者
return cls.zhuanlan_host + "/api/columns/{slug}/followers".format(slug=slug)

# 关注某专栏/取消关注某专栏
@classmethod
def follow_column(cls, slug):
# 关注某专栏/取消关注某专栏
return cls.zhuanlan_host + "/api/columns/{slug}/follow".format(slug=slug)

unfollow_column = follow_column

# 注册用的短信验证码
@classmethod
def register_sms_code(cls):
# 注册用的短信验证码
return cls.host + "/send_register_verification_code/sms"

# 注册验证URL
@classmethod
def register_validate(cls):
# 注册验证URL
return cls.host + "/register/phone_num/validation"

@classmethod
def register(cls):
return cls.host + "/register/phone_num"

# 粉丝列表URL
@classmethod
def followers(cls, user_slug):
# 粉丝列表URL
return cls.host + "/api/v4/members/{slug}/followers?include=data[*].answer_count,gender,follower_count," \
"badge[?(type=best_answerer)].topics".format(slug=user_slug)

0 comments on commit c464551

Please sign in to comment.