-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanalysis.py
112 lines (97 loc) · 3.68 KB
/
analysis.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
#!/usr/bin/python
# -*- coding: utf-8 -*-
# @Time : 7/13/22 10:57
# @Author : StevenL
# @Email : stevenl365404@gmail.com
# @File : analysis.py
import json
from notion_client import Client as NotionClient
from redisbloom.client import Client as RedisBloomClient
from utils.utils import stu_question2options
# 加载文件
file_name = '毛概-8'
subject = file_name.split('-', 1)[0]
path = r"../notion-create-exam-databace/origin_json_data/" + file_name + ".json"
file = open(path, 'r', encoding='utf-8')
# 创建 API 客户端
# notion = NotionClient(auth=os.environ['NOTION_TOKEN'])
notion = NotionClient(auth='替换成自己的token')
# 创建redis_client
redis_client = RedisBloomClient(host='localhost', port=6379, db=0)
# 解析数据
list_data = json.load(file)
for data in list_data:
stuQuestions = data["stuQuestions"]
index = 1
for stuQuestion in stuQuestions:
print('执行第' + str(index) + '遍')
stuQuestion_type = stuQuestion["questionType"]
print('问题类型:' + str(stuQuestion_type))
# switchCase = switchCase()
# switchCase.case_to_function(stuQuestion_type)
stuQuestion_id = stuQuestion["id"]
stuQuestion_title = stuQuestion["title"][0:1000]
stuQuestion_answer = stuQuestion["answer"]
stuQuestion_options_done = []
# 单选和多选题直接获取选项
if stuQuestion_type == 1 or stuQuestion_type == 2:
stuQuestion_options = stuQuestion["options"]
stuQuestion_options_done = stu_question2options(stuQuestion_options)
# 如果是阅读理解有子题目,则获取子题目答案.
if stuQuestion_type == 8:
stuSubQuestions = stuQuestion["stuSubQuestion"]
stuQuestion_answer = []
for stuSubQuestion in stuSubQuestions:
stuSubQuestion_answer = stuSubQuestion["answer"]
stuQuestion_answer.append(stuSubQuestion_answer)
if redis_client.bfExists('id', stuQuestion_id):
print('已存在的id:', stuQuestion_id)
index += 1
continue
# 写入数据库
notion.pages.create(
**{
'parent': {'database_id': 'f406642b43ba49ff99844e621608f8d3'},
'properties': {
'Id': {
'title': [
{
'text': {
'content': stuQuestion_id
}
}
]
},
'Title': {
'rich_text': [{
'text': {
'content': stuQuestion_title,
'link': None
}
}]
},
'Answer': {
'rich_text': [{
'text': {
'content': str(stuQuestion_answer),
'link': None
},
}]
},
'Options': {
'multi_select': stuQuestion_options_done,
},
'QuestionType': {
'number': stuQuestion_type,
},
'Subject': {
'select': {
'name': subject
},
}
}
}
)
index += 1
# 把id写入redis布隆过滤器
redis_client.bfAdd('id', stuQuestion_id)