-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtask_import.py
89 lines (67 loc) · 2.33 KB
/
task_import.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
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
"""
@Author:w2n1ck
@Index:http://www.w2n1ck.com/
@task_import.py
imports tasks from 'tasks.json' into the database
"""
import dataset
import json
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
if __name__ == '__main__':
purge = False
# 清除数据
if len(sys.argv) > 1:
if sys.argv[1] == 'purge':
purge = True
print "[*] Purge mode is on, old tasks deleted"
# 更新tasks.json中的数据
tasks_str = open('tasks.json').read().decode('utf-8')
# print tasks_str
# print json.dumps(tasks_str, sort_keys=True, indent=2)
tasks_json = json.loads(tasks_str.decode('utf-8'))
# print tasks_json
# 连接数据库
db = dataset.connect('mysql://root:[email protected]/ctf?charset=utf8')
# db = dataset.connect('sqlite:///ctf.db')
# mysql://username:[email protected]/onlinedb?charset=utf8'
# 题目类别表
cat_table = db['categories']
# 题目详情表
tasks_table = db['tasks']
# 题目所属类别表
cat_task_table = db['cat_task']
if purge:
cat_table.delete()
tasks_table.delete()
cat_task_table.delete()
# 重新处理tasks.json文件,并添加到表中
old_cat_count = len(list(cat_table))
old_task_count = len(list(tasks_table))
# 导入分类
for category in tasks_json['categories']:
cat = category.copy()
del cat['tasks']
if cat_table.find_one(id=cat['id']):
# 更新已经存在的分类
cat_table.update(cat, ['id'])
else:
cat_table.insert(cat)
new_cat_count = len(list(cat_table))
print "[*] Imported %d new categories" % (new_cat_count - old_cat_count)
# 导入题目
for category in tasks_json['categories']:
for task in category['tasks']:
if tasks_table.find_one(id=task['id']):
# 更新已经存在的题目
tasks_table.update(task, ['id'])
else:
tasks_table.insert(task)
# 更新题目所属分类表
row = dict(cat_id=category['id'], task_id=task['id'])
cat_task_table.insert(row)
new_task_count = len(list(tasks_table))
print "[*] Imported %d new tasks" % (new_task_count - old_task_count)