-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathstart.py
112 lines (100 loc) · 3.8 KB
/
start.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
import json
from typing import cast
import mysql.connector
from colorama import Fore
from mysql.connector import MySQLConnection
from console_py import console
from db import importAuthors, importData
def start():
console.logPath = "./log.txt"
begin = console.log("開始處理文件", Fore.CYAN)
# 获取各种参数
configFile = open("./config.json", "r", encoding="utf-8")
config = json.loads(configFile.read())
table = config["table"]
tableAuthor = config["tableAuthor"]
source = config["source"]
data = config["files"]["data"]
include = config["files"]["include"]
exclude = config["files"]["exclude"]
authors = config["files"]["authors"]
# 连接数据库
connect = cast(MySQLConnection, mysql.connector.connect(**config["mysql"]))
cursor = connect.cursor()
cursor.execute("SET names 'utf8mb4'")
# 删除旧表, 创建新表
# 诗词表
if len(table):
cursor.execute(f"DROP TABLE IF EXISTS `{table}`")
sql = f"""CREATE TABLE `{table}` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`author` text DEFAULT NULL,
`dynasty` text NOT NULL,
`title` text DEFAULT NULL,
`rhythmic` text DEFAULT NULL,
`chapter` text DEFAULT NULL,
`paragraphs` text NOT NULL,
`notes` text DEFAULT NULL,
`collection` text NOT NULL,
`section` text DEFAULT NULL,
`content` longtext DEFAULT NULL,
`comment` text DEFAULT NULL,
`tags` text DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;"""
cursor.execute(sql)
# 作者表
if len(tableAuthor):
cursor.execute(f"DROP TABLE IF EXISTS `{tableAuthor}`")
sql = f"""CREATE TABLE `{tableAuthor}` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(100) NOT NULL,
`description` text,
`short_description` text,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;"""
cursor.execute(sql)
cursor.close()
# 循环处理json文件
arr = []
maxLenCollection = 0
maxLenTime = 0
total = 0
res = None
hasInclude = len(include) > 0
hasExclude = len(exclude) > 0
if len(table):
for info in data:
if hasInclude and info["collection"] not in include:
continue
if hasExclude and info["collection"] in exclude:
continue
res = importData(connect, source, table, info)
arr.append(res)
maxLenCollection = max(maxLenCollection, console.strLen(res["collection"]))
maxLenTime = max(maxLenTime, console.strLen(res["time"]))
if isinstance(res["count"], int):
total += res["count"]
if len(tableAuthor):
if isinstance(authors, str):
authors = [authors]
res = importAuthors(connect, source, tableAuthor, authors)
arr.append(res)
maxLenCollection = max(maxLenCollection, console.strLen(res["collection"]))
maxLenTime = max(maxLenTime, console.strLen(res["time"]))
if isinstance(res["count"], int):
total += res["count"]
connect.commit()
connect.close()
# 最后输出统计信息
end = console.log("所有文件處理完畢", Fore.GREEN, begin)
console.log()
for v in arr:
collection = console.strAlign(v["collection"], maxLenCollection, "L")
time = console.strAlign(v["time"], maxLenTime, "L")
console.log(f"{collection} 用時:{time} 記錄數:{v['count']}")
console.log(f"共計用時:{console.round(end-begin)}s")
console.log(f"記錄總數:{total}")
console.log()
if __name__ == "__main__":
start()