-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathmain.py
153 lines (120 loc) · 4.14 KB
/
main.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
import os
import urllib.parse
import requests
from bs4 import BeautifulSoup
from requests.adapters import HTTPAdapter
from urllib3.util.retry import Retry
import util
from util import logger
from zhihu import Zhihu
def generate_archive_md(searches, questsions, videos):
"""生成归档readme
"""
def search(item):
title = item['queryDisplay']
q = urllib.parse.quote(item['realQuery'])
url = 'https://www.zhihu.com/search?q={}'.format(q)
return '1. [{}]({})'.format(title, url)
def question(item):
target = item['target']
title = target['title_area']['text']
url = target['link']['url']
return '1. [{}]({})'.format(title, url)
def video(item):
target = item['target']
title = target['title_area']['text']
url = target['link']['url']
return '1. [{}]({})'.format(title, url)
searchMd = '暂无数据'
if searches:
searchMd = '\n'.join([search(item) for item in searches])
questionMd = '暂无数据'
if questsions:
questionMd = '\n'.join([question(item) for item in questsions])
videoMd = '暂无数据'
if videos:
videoMd = '\n'.join([video(item) for item in videos])
md = ''
file = os.path.join('template', 'archive.md')
with open(file) as f:
md = f.read()
now = util.current_time()
md = md.replace("{updateTime}", now)
md = md.replace("{searches}", searchMd)
md = md.replace("{questions}", questionMd)
md = md.replace("{videos}", videoMd)
return md
def generate_readme(searches, questsions, videos):
"""生成readme
"""
def search(item):
title = item['queryDisplay']
q = urllib.parse.quote(item['realQuery'])
url = 'https://www.zhihu.com/search?q={}'.format(q)
return '1. [{}]({})'.format(title, url)
def question(item):
target = item['target']
title = target['title_area']['text']
url = target['link']['url']
return '1. [{}]({})'.format(title, url)
def video(item):
target = item['target']
title = target['title_area']['text']
url = target['link']['url']
return '1. [{}]({})'.format(title, url)
searchMd = '暂无数据'
if searches:
searchMd = '\n'.join([search(item) for item in searches])
questionMd = '暂无数据'
if questsions:
questionMd = '\n'.join([question(item) for item in questsions])
videoMd = '暂无数据'
if videos:
videoMd = '\n'.join([video(item) for item in videos])
readme = ''
file = os.path.join('template', 'README.md')
with open(file) as f:
readme = f.read()
now = util.current_time()
readme = readme.replace("{updateTime}", now)
readme = readme.replace("{searches}", searchMd)
readme = readme.replace("{questions}", questionMd)
readme = readme.replace("{videos}", videoMd)
return readme
def saveReadme(md):
logger.debug('today md:%s', md)
util.write_text('README.md', md)
def saveArchiveMd(md):
logger.debug('archive md:%s', md)
name = util.current_date()+'.md'
file = os.path.join('archives', name)
util.write_text(file, md)
def saveRawContent(content: str, filePrefix: str, fileSuffix='json'):
logger.debug('raw content:%s', content)
name = '{}-{}.{}'.format(filePrefix, util.current_date(), fileSuffix)
file = os.path.join('raw', name)
util.write_text(file, content)
def run():
zhihu = Zhihu()
# 热搜数据
searches, resp = zhihu.get_hot_search()
if resp:
saveRawContent(resp.text, 'hot-search', 'html')
# 问题数据
questions, resp = zhihu.get_hot_question()
if resp:
text = util.cnsafe_json(resp.text)
saveRawContent(text, 'hot-question', 'json')
# 视频数据
videos, resp = zhihu.get_hot_video()
if resp:
text = util.cnsafe_json(resp.text)
saveRawContent(text, 'hot-video', 'json')
# 最新数据
todayMd = generate_readme(searches, questions, videos)
saveReadme(todayMd)
# 归档
archiveMd = generate_archive_md(searches, questions, videos)
saveArchiveMd(archiveMd)
if __name__ == "__main__":
run()