-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathsummary.py
319 lines (288 loc) · 11.8 KB
/
summary.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
import json
from pathlib import Path
from datetime import datetime, timezone
BASE_URL = "https://l-m-sherlock.github.io/ZhiHuLegend"
nicknames = {
"ma-qian-zu": "马前卒",
"huo-hua-de-41": "霍华德",
"dang.xinran": "立党",
"li-yin-61-82": "李归农",
"maple-syrup-41": "tim未来之光",
"dantes-15": "勃勃",
"miloyip": "Milo Yip",
"Himself65": "扩散性百万甜面包",
"sinya": "李新野",
"Deceiver-Kil": "Deceiver",
"excalibur-11-11": "Coldstream",
"gong-ke-75": "山高县",
"volunteertravel": "东南亚漂",
"xbjf": "玄不救非氪不改命",
"deng-bo-yun": "邓铂鋆",
"fang-liang-0423": "方亮",
"liu-jin-ze-71-59": "刘金泽",
"Sicherchen": "安玲",
"yu-feng-78-98": "御风",
"fangkc": "方可成",
"zhang-zha-60-7": "zhang zha",
"shinianhanshuang": "十年寒霜",
"zhijiangchuan": "松平信纲",
"hun-luan-feng-bao-33": "弗兰德斯",
"wo-bu-zuo-lian-dao-hen-duo-nian": "我不做镰刀很多年",
"an-guo-da-jiang-jun": "安国大将军",
"shengjingjianke": "盛京剑客",
"pandamalone": "Long Yu",
"gmqsunliancheng": "宇宙区长孙连城",
}
def generate_summary(username: str):
# Collect all articles
articles = []
for file in Path(f"{username}/article").glob("*.json"):
with open(file, "r", encoding="utf-8") as f:
data = json.load(f)
data["file_stem"] = file.stem
articles.append(data)
# Collect all answers
answers = []
for file in Path(f"{username}/answer").glob("*.json"):
with open(file, "r", encoding="utf-8") as f:
try:
data = json.load(f)
data["file_stem"] = file.stem
if "error" in data:
print(data["error"], file.stem)
file.unlink()
continue
answers.append(data)
except json.JSONDecodeError:
print(file.stem, "is not a valid json file")
# Sort by voteup_count
articles.sort(key=lambda x: x["voteup_count"], reverse=True)
answers.sort(key=lambda x: x["voteup_count"], reverse=True)
# Generate HTML content with tabs
html_content = (
f"""<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>{nicknames[username]}的知乎索引</title>
<meta property="og:type" content="website">
<meta property="og:title" content="{nicknames[username]}的知乎索引">
<meta property="og:site_name" content="ZhiHu Legend">
<meta property="og:url" content="https://l-m-sherlock.github.io/ZhiHuLegend/{username}.html">
<meta name="description" property="og:description" content="{nicknames[username]}的知乎文章和回答索引">
<meta name="google-site-verification" content="U7ZAFUgGNK60mmMqaRygg5vy-k8pwbPbDFXNjDCu7Xk" />
<meta property="twitter:card" content="summary">
<meta name="twitter:title" property="og:title" itemprop="name" content="{nicknames[username]}的知乎索引">
<meta name="twitter:description" property="og:description" itemprop="description" content="{nicknames[username]}的知乎文章和回答索引">
"""
+ """
<style>
body { max-width: 800px; margin: 0 auto; padding: 20px; }
.item { margin: 10px 0; }
.votes { color: #666; font-size: 0.9em; }
.created_time { color: #999; font-size: 0.9em; }
.censored { background-color: #ffeb3b; }
/* Tab styles */
.tabs { margin-bottom: 20px; }
.tab-button {
padding: 10px 20px;
border: none;
background: #f0f0f0;
cursor: pointer;
font-size: 16px;
}
.tab-button.active {
background: #007bff;
color: white;
}
.tab-content {
display: none;
}
.tab-content.active {
display: block;
}
/* Add styles for domain switcher */
.domain-switcher {
margin: 20px 0;
}
.domain-switcher label {
margin-right: 10px;
}
.domain-explanation {
margin: 15px 0;
font-size: 0.9em;
color: #666;
}
.domain-explanation ul {
margin: 5px 0;
padding-left: 20px;
}
.domain-explanation li {
margin: 3px 0;
}
</style>
<script>
function openTab(evt, tabName) {
const activeTab = document.querySelector('.tab-content.active');
const activeButton = document.querySelector('.tab-button.active');
if (activeTab) activeTab.classList.remove('active');
if (activeButton) activeButton.classList.remove('active');
document.getElementById(tabName).classList.add('active');
evt.currentTarget.classList.add('active');
}
function toggleDomain() {
const useZhihu = document.getElementById('useZhihu').checked;
const links = document.getElementsByTagName('a');
for (let link of links) {
if (link.href.includes('fxzhihu.com')) {
link.href = link.href.replace('fxzhihu.com', 'zhihu.com');
} else if (link.href.includes('zhihu.com')) {
link.href = link.href.replace('zhihu.com', 'fxzhihu.com');
}
}
}
</script>
</head>
<body>"""
+ f"""
<p><a href="./">← 返回封神榜</a></p>
<hr>
<h1>{nicknames[username]}的知乎索引</h1>
<div class="domain-switcher">
<div class="domain-explanation">是否已登录知乎账号</div>
<label>
<input type="radio" name="domain" id="useZhihu" checked onclick="toggleDomain()"> 是
</label>
<label>
<input type="radio" name="domain" id="useFxzhihu" onclick="toggleDomain()"> 否
</label>
</div>
<div class="domain-explanation">
<ul>
<li>是:跳转到知乎访问(推荐)</li>
<li>否:无需登录,但有时候不稳定</li>
</ul>
</div>
<div class="tabs">
<button class="tab-button active" onclick="openTab(event, 'answers-tab')">回答 ({len(answers)})</button>
<button class="tab-button" onclick="openTab(event, 'articles-tab')">文章 ({len(articles)})</button>
</div>
<div id="answers-tab" class="tab-content active">
<h2>回答</h2>
"""
)
# Add answers
for answer in answers:
question_title = (
answer["question"]["title"]
if "question" in answer and "title" in answer["question"]
else "Untitled"
)
html_content += f"""
<div class="item">
<a href="https://www.zhihu.com/answer/{answer['file_stem']}" target="_blank">{question_title}</a>
<span class="votes">({answer['voteup_count']} 赞同)</span>
<span class="created_time">({datetime.fromtimestamp(answer['created_time']).strftime('%Y-%m-%d')})</span>
</div>
"""
html_content += """
</div>
<div id="articles-tab" class="tab-content">
<h2>文章</h2>
"""
# Add articles
for article in articles:
html_content += f"""
<div class="item">
<a href="https://zhuanlan.zhihu.com/p/{article['file_stem']}" target="_blank">{article['title']}</a>
<span class="votes">({article['voteup_count']} 赞同)</span>
<span class="created_time">({datetime.fromtimestamp(article['created']).strftime('%Y-%m-%d')})</span>
</div>
"""
html_content += """
</div>
</body>
</html>
"""
Path(f"./docs").mkdir(exist_ok=True)
# Write the HTML file
with open(f"./docs/{username}.html", "w", encoding="utf-8") as f:
f.write(html_content)
def generate_index_html():
"""Generate index.html as the entry point for ZhiHuLegend website"""
index_content = """
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>ZhiHu Legend</title>
<meta property="og:type" content="website">
<meta property="og:title" content="知乎封神榜">
<meta property="og:site_name" content="ZhiHu Legend">
<meta property="og:url" content="https://l-m-sherlock.github.io/ZhiHuLegend/">
<meta name="description" property="og:description" content="ZhiHu Legend - 知乎封神榜">
<meta name="google-site-verification" content="U7ZAFUgGNK60mmMqaRygg5vy-k8pwbPbDXNjDCu7Xk" />
<meta property="twitter:card" content="summary">
<meta name="twitter:title" property="og:title" itemprop="name" content="知乎封神榜">
<meta name="twitter:description" property="og:description" itemprop="description" content="ZhiHu Legend - 知乎封神榜">
<style>
body { max-width: 800px; margin: 0 auto; padding: 20px; }
.user-list { margin: 20px 0; }
.user-item { margin: 10px 0; }
.footer { margin-top: 30px; padding-top: 20px; border-top: 1px solid #eee; }
</style>
</head>
<body>
<h1>知乎封神榜</h1>
<p>
<a href="https://github.com/l-m-sherlock/ZhiHuLegend">
<img src="https://img.shields.io/github/stars/l-m-sherlock/ZhiHuLegend?style=social" alt="GitHub stars">
</a>
</p>
<div class="introduction">
<p>本项目旨在收集知乎上各位被知乎封禁或自主销号的大佬的回答和文章,并生成索引页面。</p>
<p>本项目不存储任何数据,所有索引页面均基于公开可访问的知乎数据生成。</p>
<p>欢迎各位读者提名更多的大佬,要求如下:</p>
<ul>
<li>被知乎封禁或自主销号</li>
<li>有足够多的回答和文章</li>
<li>内容还可以在知乎访问</li>
</ul>
</div>
<hr>
<div class="user-list">
"""
# Add links to each user's summary page
for username, nickname in nicknames.items():
index_content += f"""
<div class="user-item">
<a href="{username}.html">{nickname}的知乎索引</a>
</div>
"""
index_content += """
</div>
<div class="footer">
<div class="links-section">
<h2>友情链接</h2>
<a href="https://l-m-sherlock.github.io/ZhiHuArchive/" target="_blank">Thoughts Memo 和 Jarrett Ye 的知乎文章和回答备份目录</a>
</div>
</div>
</body>
</html>
"""
# Write the index.html file
with open("./docs/index.html", "w", encoding="utf-8") as f:
f.write(index_content)
for username in nicknames:
generate_summary(username)
# Generate index.html after all summary pages are created
generate_index_html()
# Generate sitemap.txt
with open("./docs/sitemap.txt", "w", encoding="utf-8") as f:
# Write base URL
f.write("https://l-m-sherlock.github.io/ZhiHuLegend/\n")
# Write each user's page URL
for username in nicknames:
f.write(f"https://l-m-sherlock.github.io/ZhiHuLegend/{username}.html\n")