-
Notifications
You must be signed in to change notification settings - Fork 4
/
dynamic_watch.py
63 lines (57 loc) · 2.38 KB
/
dynamic_watch.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
import json
import time
import updynamic
uid = int(input('UP主UID: '))
upwh = updynamic.UploaderDynamic(uid)
with open('dynamic_types.json', 'r') as load_file:
dynamic_types = json.load(load_file)
# noinspection PyBroadException
try:
with open('diagnosis.json', 'r') as load_file:
diagnosis = json.load(load_file)
except Exception:
diagnosis = {'diagnosis': []}
with open('diagnosis.json', 'w') as dump_file:
json.dump(diagnosis, dump_file)
uploader_name = upwh.uploader_name
print('开始监视UP主<{}>的更新...'.format(uploader_name))
print()
while True:
try:
new_dynamics = upwh.get_update()
if len(new_dynamics) > 0:
for dynamic in new_dynamics:
dynamic_id = dynamic['desc']['dynamic_id']
dynamic_type = str(dynamic['desc']['type'])
content = "未解析"
type_contains_title = False
title = ""
if dynamic_type in dynamic_types['types']:
type_data = dynamic_types['types'][dynamic_type]
type_name = type_data['name']
type_contains_title = type_data['contains_title']
type_content_path = type_data['path']
content = dynamic.copy()
for k in type_content_path:
content = content[k]
if type_contains_title:
type_title_path = type_data['title_path']
title = dynamic.copy()
for k in type_title_path:
title = title[k]
else:
type_name = '[未知类型动态]'
diagnosis['diagnosis'].append(dynamic)
with open('diagnosis.json', 'w') as dump_file:
json.dump(diagnosis, dump_file)
print('发现不能被识别的动态类型,请将"diagnosis.json"提交给开发者')
print('您关注的UP主<{}>发布了新的{}'.format(uploader_name, type_name))
print('动态ID: {}'.format(dynamic_id))
print('动态内容: {}'.format(content))
if type_contains_title:
print('稿件标题: {}'.format(title))
print()
except Exception as e:
print(e)
finally:
time.sleep(30)