-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjson2db.py
115 lines (103 loc) · 3.06 KB
/
json2db.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
113
114
115
import json, sqlite3
from pydantic import BaseModel
from datetime import datetime
class Location(BaseModel):
location: str
locationName: str
latitude: str | None
longitude: str | None
class ShowTime(Location):
time: str
endTime: str
onSales: str | bool
price: str
def __init__(self, **data):
super().__init__(**data)
self.onSales = True if data["onSales"] == "Y" else False
class showInfo(BaseModel):
UID: str
version: str
title: str
category: str
discountInfo: str
descriptionFilterHtml: str
imageUrl: str
webSales: str
sourceWebPromote: str
sourceWebName: str
comment: str
editModifyDate: str
hitRate: int
showInfo: list[ShowTime]
masterUnit: list[str]
supportUnit: list[str]
otherUnit: list[str]
subUnit: list[str]
showUnit: str | list[str]
def __init__(self, **data):
super().__init__(**data)
if not self.showUnit:
self.showUnit = []
else:
self.showUnit = self.showUnit.split(";")
with open("SearchShowAction.json", encoding="utf-8") as f:
j = json.loads(f.read())
con = sqlite3.connect("test.db")
cur = con.cursor()
for i in j:
a = showInfo(**i)
cur.execute(
"INSERT INTO SHOW_INFO VALUES(?,?,?,?,?,?,?,?,?,?,?,?)",
(
a.UID,
a.version,
a.title,
a.category,
a.discountInfo,
a.descriptionFilterHtml,
a.imageUrl,
a.webSales,
a.sourceWebPromote,
a.comment,
a.editModifyDate,
a.hitRate,
),
)
for unit in a.supportUnit:
cur.execute("INSERT INTO SUPPORT_UNIT VALUES(?,?)", (unit, a.UID))
for unit in a.masterUnit:
cur.execute("INSERT INTO MASTER_UNIT VALUES(?,?)", (unit, a.UID))
for unit in a.otherUnit:
cur.execute("INSERT INTO OTHER_UNIT VALUES(?,?)", (unit, a.UID))
for unit in a.subUnit:
cur.execute("INSERT INTO SUB_UNIT VALUES(?,?)", (unit, a.UID))
for unit in a.showUnit:
cur.execute("INSERT INTO SHOW_UNIT VALUES(?,?)", (unit, a.UID))
cur.execute("INSERT INTO SOURCE_WEB_NAME VALUES(?,?)", (a.sourceWebName, a.UID))
for time in a.showInfo:
cur.execute(
"SELECT location_id FROM LOCATION WHERE name=?", (time.locationName,)
)
row = cur.fetchone()
if row:
loaction_ID = row[0]
else:
cur.execute(
"""INSERT INTO LOCATION
(address,name,lat,lng)
VALUES (?,?,?,?)""",
(time.location, time.locationName, time.latitude, time.longitude),
)
loaction_ID = int(cur.lastrowid)
cur.execute(
"""INSERT INTO SHOW_TIME (
show_uid,
location_id,
time,
on_sales,
price,
end_time)
VALUES (?,?,?,?,?,?)""",
(a.UID, loaction_ID, time.time, time.onSales, time.price, time.endTime),
)
con.commit()