-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.py
72 lines (61 loc) · 2.91 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
import os
import requests
from datetime import datetime
import json
class GetHot():
def __init__(self, url):
self.url = url
self.list = []
def format_list(self, origin):
newList = map(lambda x:{"title": x['target']['title'], "excerpt": x['target']['excerpt'], "url": f"https://www.zhihu.com/question/{x['target']['id']}"}, origin)
return list(newList)
def getList(self):
response = requests.get(self.url)
self.list = json.loads(response.text)['data']
return self.format_list(self.list)
def read_old_list(self, datetime_str):
try:
with open(datetime_str, 'r') as f:
oldList = json.load(f)
return oldList
except FileNotFoundError:
with open(datetime_str, "w") as f:
return []
def write_list(self):
if not os.path.exists('./data'):
os.makedirs('./data')
dir_path = os.path.dirname(os.path.realpath(__file__)) + '/data'
datetime_str = f"{dir_path}/{datetime.now().strftime('%Y-%m-%d')}.json";
current_list = self.getList()
oldList = self.read_old_list(datetime_str)
# 写数据之前判断这个数据是否存在
mergeList = []
if len(oldList) == 0:
with open(datetime_str, "w") as f:
json.dump(current_list, f, ensure_ascii=False, indent=4)
print(f"写入文件成功,文件名为{datetime_str}")
else:
mergeList = oldList + current_list
# 去掉重复的数据
result = [dict(t) for t in {tuple(d.items()) for d in mergeList}]
with open(datetime_str, "w") as f:
json.dump(result, f, ensure_ascii=False, indent=4)
self.create_readme(result)
print(f"写入文件成功,文件名为{datetime_str}")
def create_readme(self, newList):
message = f"""# zhihu-trending-hot-questions
参考[项目](https://github.com/justjavac/zhihu-trending-hot-questions), 由Python改写,仅为学习Python,无任何其他用途
知乎热门话题,记录从 2023-7-19
日开始的知乎热门话题。每小时抓取一次数据,按天[归档](./data)。\n{self.create_readme_list(newList)}\n### License
[zhihu-trending-hot-questions](https://github.com/yaogengzhu/zhihu-trending-hot-questions)
的源码使用 MIT License 发布。具体内容请查看 [LICENSE](./LICENSE) 文件。
"""
with open("./README.md", "w") as f:
f.write(message)
def create_readme_list(self, newList):
question_list = "\n".join(f"1. [{x['title']}]({x['url']})" for x in newList)
return f"<!-- BEGIN -->\n<!-- 最后更新时间 {datetime.now()} -->\n{question_list}\n<!-- END -->"
if __name__ == "__main__":
url = 'https://www.zhihu.com/api/v3/feed/topstory/hot-lists/total?limit=100'
get = GetHot(url)
get.write_list()