-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadd-danxuan.py
70 lines (61 loc) · 1.96 KB
/
add-danxuan.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
import requests
import json
from openpyxl import load_workbook
from util import Properties
props = Properties('add-danxuan.ini').getProperties()
print(props['ip'])
# 登录
net_url = 'http://' + props['ip'] + ':8000'
login_endpoint = props['login_endpoint']
excel_file = "danxuan.xlsx"
sheet_name = "工作表1"
headers = {'Content-Type': 'application/json', 'Connection': 'keep-alive'}
workbook = load_workbook(filename=excel_file)
sheet = workbook[sheet_name]
session = requests.Session()
response = session.post(f'{net_url}{login_endpoint}', headers=headers, data=json.dumps({
"userName": props['usr'],
"password": props['pwd'],
"remember": True
}))
print(response.text)
for row in sheet.iter_rows(min_row=2, values_only=True):
print(str(row[3]) + ' -- is adding...')
questionType = row[0]
gradeLevel = row[1]
subjectId = row[2]
title = row[3]
options_str = row[4]
analyze = row[5]
correct = row[6]
score = row[7]
difficult = row[8]
# 拆分字符串得到选项列表
options_list = options_str.split(';')
json_data = []
for i, option in enumerate(options_list, start=1):
json_obj = {
"prefix": chr(64 + i),
"content": option.strip()
}
json_data.append(json_obj)
payload = {
"id": None,
"questionType": questionType,
"gradeLevel": gradeLevel,
"subjectId": subjectId,
"title": title,
"items": json_data,
"analyze": analyze,
"correct": correct,
"score": score,
"difficult": row[8]
}
response = session.post(net_url + '/api/admin/question/edit', headers=headers, data=json.dumps(payload))
if response.status_code < 300:
data = json.dumps(response.json(), indent=4)
print(data)
else:
print(f"失败,状态码:{response.status_code}")
print(response.text)
workbook.close()