forked from lonnyzhang423/weibo-hot-hub
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
104 lines (76 loc) · 2.63 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
import os
import util
from util import logger
from weibo import Weibo
def generate_archive_md(searches, topics):
"""生成归档readme
"""
def search(item):
return '1. [{}]({})'.format(item['title'], item['url'])
def topic(item):
return '1. [{}]({})\n - {}\n - {}'.format(item['title'], item['url'], item['detail'], item['info'])
searchMd = '暂无数据'
if searches:
searchMd = '\n'.join([search(item) for item in searches])
topicMd = '暂无数据'
if topics:
topicMd = '\n'.join([topic(item) for item in topics])
readme = ''
file = os.path.join('template', 'archive.md')
with open(file) as f:
readme = f.read()
readme = readme.replace("{updateTime}", util.current_time())
readme = readme.replace("{searches}", searchMd)
readme = readme.replace("{topics}", topicMd)
return readme
def generate_readme(searches, topics):
"""生成今日readme
"""
def search(item):
return '1. [{}]({})'.format(item['title'], item['url'])
def topic(item):
return '1. [{}]({})\n - {}\n - {}'.format(item['title'], item['url'], item['detail'], item['info'])
searchMd = '暂无数据'
if searches:
searchMd = '\n'.join([search(item) for item in searches])
topicMd = '暂无数据'
if topics:
topicMd = '\n'.join([topic(item) for item in topics])
readme = ''
file = os.path.join('template', 'README.md')
with open(file) as f:
readme = f.read()
readme = readme.replace("{updateTime}", util.current_time())
readme = readme.replace("{searches}", searchMd)
readme = readme.replace("{topics}", topicMd)
return readme
def save_readme(md):
logger.debug('readme:%s', md)
util.write_text('README.md', md)
def save_archive_md(md):
logger.debug('archive md:%s', md)
name = '{}.md'.format(util.current_date())
file = os.path.join('archives', name)
util.write_text(file, md)
def save_raw_content(content: str, filePrefix: str):
filename = '{}-{}.html'.format(filePrefix, util.current_date())
file = os.path.join('raw', filename)
util.write_text(file, content)
def run():
weibo = Weibo()
# 热搜
searches, resp = weibo.get_hot_search()
if resp:
save_raw_content(resp.text, 'hot-search')
# 话题榜
topics, resp = weibo.get_hot_topic()
if resp:
save_raw_content(resp.text, 'hot-topic')
# 最新数据
readme = generate_readme(searches, topics)
save_readme(readme)
# 归档
archiveMd = generate_archive_md(searches, topics)
save_archive_md(archiveMd)
if __name__ == "__main__":
run()