-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathmain.py
298 lines (245 loc) · 8.45 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
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
import os
import time
import urllib
from requests import Response
import util
from douyin import Douyin
from util import logger
def generate_archive_md(searches, stars, lives, musics, brands):
"""生成今日readme
"""
def search(item):
word = item['word']
url = 'https://www.douyin.com/search/' + urllib.parse.quote(word)
return '1. [{}]({})'.format(word, url)
def star(item):
name = item['user_info']['nickname']
uid = item['user_info']['uid']
suid = item['user_info']['sec_uid']
url = 'https://www.iesdouyin.com/share/user/{}?sec_uid={}'.format(
uid, suid)
return '1. [{}]({})'.format(name, url)
def live(item):
uid = item['user']['id']
suid = item['user']['sec_uid']
nickname = item['user']['nickname']
title = item['room']['title']
roomid = item['room']['id']
user_url = 'https://www.iesdouyin.com/share/user/{}?sec_uid={}'.format(
uid, suid)
live_url = 'https://webcast.amemv.com/webcast/reflow/'+str(roomid)
if not title:
title = '看直播'
return '1. [{}]({}) - [{}]({})'.format(title, live_url, nickname, user_url)
def music(item):
info = item['music_info']
title = info['title']
author = info['author']
if 'play_url' in info:
play_url = info['play_url']['uri']
return '1. [{}]({}) - {}'.format(title, play_url, author)
return '1. {} - {}'.format(title, author)
searchMd = '暂无数据'
if searches:
searchMd = '\n'.join([search(item) for item in searches])
starMd = '暂无数据'
if stars:
starMd = '\n'.join([star(item) for item in stars])
liveMd = '暂无数据'
if lives:
liveMd = '\n'.join([live(item) for item in lives])
musicMd = '暂无数据'
if musics:
musicMd = '\n'.join([music(item) for item in musics])
brandsMd = '暂无数据'
if brands:
brandsMd = generate_brand_table_md(brands)
readme = ''
file = os.path.join('template', 'archive.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("{stars}", starMd)
readme = readme.replace("{lives}", liveMd)
readme = readme.replace("{musics}", musicMd)
readme = readme.replace("{brands}", brandsMd)
return readme
def generate_readme(searches, stars, lives, musics, brands):
"""生成今日readme
"""
def search(item):
word = item['word']
url = 'https://www.douyin.com/search/' + urllib.parse.quote(word)
return '1. [{}]({})'.format(word, url)
def star(item):
name = item['user_info']['nickname']
uid = item['user_info']['uid']
suid = item['user_info']['sec_uid']
url = 'https://www.iesdouyin.com/share/user/{}?sec_uid={}'.format(
uid, suid)
return '1. [{}]({})'.format(name, url)
def live(item):
uid = item['user']['id']
suid = item['user']['sec_uid']
nickname = item['user']['nickname']
title = item['room']['title']
roomid = item['room']['id']
user_url = 'https://www.iesdouyin.com/share/user/{}?sec_uid={}'.format(
uid, suid)
live_url = 'https://webcast.amemv.com/webcast/reflow/'+str(roomid)
if not title:
title = '看直播'
return '1. [{}]({}) - [{}]({})'.format(title, live_url, nickname, user_url)
def music(item):
info = item['music_info']
title = info['title']
author = info['author']
if 'play_url' in info:
play_url = info['play_url']['uri']
return '1. [{}]({}) - {}'.format(title, play_url, author)
return '1. {} - {}'.format(title, author)
searchMd = '暂无数据'
if searches:
searchMd = '\n'.join([search(item) for item in searches])
starMd = '暂无数据'
if stars:
starMd = '\n'.join([star(item) for item in stars])
liveMd = '暂无数据'
if lives:
liveMd = '\n'.join([live(item) for item in lives])
musicMd = '暂无数据'
if musics:
musicMd = '\n'.join([music(item) for item in musics])
brandsMd = '暂无数据'
if brands:
brandsMd = generate_brand_table_md(brands)
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("{stars}", starMd)
readme = readme.replace("{lives}", liveMd)
readme = readme.replace("{musics}", musicMd)
readme = readme.replace("{brands}", brandsMd)
return readme
def save_readme(md):
logger.info('today md:%s', md)
util.write_text('README.md', md)
def save_archive_md(md):
logger.info('archive md:%s', md)
name = util.current_date()+'.md'
file = os.path.join('archives', name)
util.write_text(file, md)
def save_raw_response(resp: Response, filename: str):
"""保存原始响应内容
"""
if resp:
content = resp.text
filename = '{}.json'.format(filename)
logger.info('save response:%s', filename)
date = util.current_date()
file = os.path.join('raw', date, filename)
util.write_text(file, content)
def save_brand_raw_response(resp: Response, category: str):
"""保存品牌榜响应内容
"""
if resp:
content = resp.text
date = util.current_date()
filename = '{}.json'.format(category)
logger.info('save response:%s', filename)
file = os.path.join('raw', date, 'brand', filename)
util.write_text(file, content)
def generate_brand_table_md(brand_map: map):
"""品牌榜md
"""
fake_brand = {'name': '-'}
def column(item):
if item is fake_brand:
return fake_brand['name']
name = item['name']
key = urllib.parse.quote(name)
search_url = 'https://www.baidu.com/s?wd={}'.format(key)
return '[{}]({})'.format(name, search_url)
def ensure_same_len(brand_map: map):
max_len = 0
for category in brand_map:
max_len = max(max_len, len(brand_map[category]))
for category in brand_map:
brands: list = brand_map[category]
if len(brands) < max_len:
brands.extend([fake_brand for _ in range(max_len-len(brands))])
return max_len
# 确保品牌列表长度相同
max_len = ensure_same_len(brand_map)
# 表头
table_header = '|'
for category in brand_map:
table_header += ' {} |'.format(category)
table_header += '\n'
table_header += '|'
for _ in range(len(brand_map)):
table_header += ' --- |'
# 表行
table_rows = ''
for i in range(max_len):
row = '|'
for category in brand_map:
brands: list = brand_map[category]
row += ' {} |'.format(column(brands[i]))
table_rows += row + '\n'
return table_header + '\n' + table_rows
def get_all_brands(dy: Douyin):
"""热门品牌
"""
categories, resp = dy.get_brand_category()
save_raw_response(resp, 'brand-category')
time.sleep(1)
brand_map = {}
for category in categories:
# 分类名称
cname = category['name']
cid = int(category['id'])
brands, resp = dy.get_hot_brand(cid)
save_brand_raw_response(resp, cname)
brand_map[cname] = brands
time.sleep(1)
return brand_map
def run():
# 获取数据
dy = Douyin()
# 热搜
searches, resp = dy.get_hot_search()
save_raw_response(resp, 'hot-search')
time.sleep(1)
# 明星
stars, resp = dy.get_hot_star()
save_raw_response(resp, 'hot-star')
time.sleep(1)
# 直播
lives, resp = dy.get_hot_live()
save_raw_response(resp, 'hot-live')
time.sleep(1)
# 音乐
musics, resp = dy.get_hot_music()
save_raw_response(resp, 'hot-music')
time.sleep(1)
# 品牌
brands = get_all_brands(dy)
time.sleep(1)
# 最新数据
todayMd = generate_readme(searches, stars, lives, musics, brands)
save_readme(todayMd)
# 归档
archiveMd = generate_archive_md(searches, stars, lives, musics, brands)
save_archive_md(archiveMd)
if __name__ == "__main__":
try:
run()
except:
logger.exception('run failed')