forked from lizhemingi/zhihu-terminal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Zhuanlan.py
159 lines (144 loc) · 4.96 KB
/
Zhuanlan.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#!/usr/bin/env python
# encoding: utf-8
"""
@version: 1.0
@author: lizheming
@contact: nkdudu@126.com
@site: lizheming.top
@file: Zhuanlan.py
"""
from zhihu import headers, clear, error, session
from bs4 import BeautifulSoup
import re
import webbrowser
import termcolor
import requests
import json
import sys
class Zhuanlan:
url = None
zhuanlan = None
soup = None
originalurl = None
def __init__(self, url):
#https://zhuanlan.zhihu.com/p/20825292
self.originalurl = url
number = re.findall(r"(\d+)", url)[0]
self.url = "http://zhuanlan.zhihu.com/api/posts/" + str(number)
self.headers = headers.copy()
self.headers["Host"] = "zhuanlan.zhihu.com"
def parse(self):
self.se = requests.Session()
for cookie in session.cookies:
self.se.cookies.set_cookie(cookie)
n = 3
res = None
while n > 0:
try:
res = self.se.get(self.url, headers=self.headers, timeout=30)
break
except:
n -= 1
return False
if not res:
print termcolor.colored("网络故障,请检查您的网络设置", "red")
sys.exit()
self.zhuanlan = dict(res.json())
self.soup = BeautifulSoup(self.zhuanlan["content"], "html.parser")
return True
def open_in_browser(self):
webbrowser.open_new(self.originalurl)
def check(self):
if not self.soup:
self.parse()
def get_title(self):
self.check()
return termcolor.colored(self.zhuanlan["title"], "blue")
def get_content(self):
self.check()
from Answer import print_content
print_content(self.soup.contents)
def get_author_info(self):
self.check()
author = dict(self.zhuanlan["author"])
return author["profileUrl"]
def vote(self, type=1):
self.check()
url = self.url + "/rating"
data = {}
if type == 1:
data["value"] = "none"
try:
self.se.put(url, json.dumps(data), headers=self.headers, timeout=15)
except:
print termcolor.colored("网络故障,请检查您的网络设置", "yellow")
return
data["value"] = "like"
else:
data["value"] = "none"
self.headers['Content-Type'] = "application/json;charset=UTF-8"
self.headers["Referer"] = self.originalurl
self.headers["Origin"] = "https://zhuanlan.zhihu.com"
self.headers['X-XSRF-TOKEN'] = self.se.cookies['XSRF-TOKEN']
try:
res = self.se.put(url, json.dumps(data), headers=self.headers, timeout=15)
except:
print termcolor.colored("网络故障,请检查您的网络设置", "yellow")
return
if res.status_code == 204:
s = "取消赞同成功" if data["value"] == "none" else "赞同成功"
print termcolor.colored(s, "blue")
elif res.status_code == 404:
s = "还没有赞同过" if data["value"] == "none" else "已经赞同过了"
print termcolor.colored(s, "blue")
def operate(self):
if not self.parse():
return True
print self.get_title()
while True:
op = raw_input("zhuanlan$ ")
if op == "content":
self.get_content()
elif op == "author":
url = self.get_author_info()
if not url:
print termcolor.colored("当前用户为匿名用户", "red")
else:
from User import User
user = User(url)
if user.operate():
return True
elif op == "voteup":
self.vote(type=1)
elif op == "votecancle":
self.vote(type=2)
elif op == "pwd":
print self.get_title()
elif op == "browser":
self.open_in_browser()
elif op == "clear":
clear()
elif op == "break":
break
elif op == "help":
self.help()
elif op == "quit":
return True
else:
error()
def help(self):
info = "\n" \
"**********************************************************\n" \
"**\n" \
"** content: 查看内容\n" \
"** author: 查看作者\n" \
"** voteup: 赞同\n" \
"** votecancle: 取消赞同\n" \
"** pwd: 显示当前专栏\n" \
"** browser: 在默认浏览器中查看\n" \
"** break: 返回上级操作目录\n" \
"** clear: 清屏\n" \
"** quit: 退出系统\n" \
"**\n" \
"**********************************************************\n"
print termcolor.colored(info, "green")