-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathinfo2.py
110 lines (86 loc) · 2.87 KB
/
info2.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
import json, mysql.connector
file = open("text.txt", "r")
res = file.read()
response = json.loads(res.encode())
selfmydb = mysql.connector.connect(
host="localhost",
user="root",
passwd="root",
database = "tv"
)
info = response['info']
other = response['other']
link = response['link']
sql1 = ""
sql2 = ""
data = []
i = 0
length = len(info)
for key, value in info.items():
if i != 0:
sql1 += ","
sql2 += ","
sql1 += "`" + key + "`"
sql2 += "%s"
i = i + 1
if type(value) == type({}):
data.append(json.dumps(value))
else:
data.append(str(value))
db = selfmydb.cursor()
command = "SELECT * FROM `play` WHERE `chinese` LIKE '" + response['info']['chinese'] + "' AND `tid` =" + str(response['info']['tid'])
db.execute(command)
if not db.fetchone():
# -------------------增加此影片基本信息----------------------
command = "INSERT INTO `tv`.`play` (" + sql1 + ") VALUES (" + sql2 + ")"
db.execute(command, data)
selfmydb.commit()
# -----------------------完毕------------------------------
# -------------------获取本次插入的ID----------------------
sql = "select max(id) from `play`"
db.execute(sql)
playid = str(db.fetchone()[0])
# ----------------------获取完毕--------------------------
# --------------------插入link信息------------------------
sql1 = ""
sql2 = ""
dataAll = []
for res in response['link']:
sql1 = ""
sql2 = ""
res['pid'] = playid
data = []
i=0
for key, value in res.items():
if i != 0:
sql1 += ","
sql2 += ","
sql1 += "`" + key + "`"
sql2 += "%s"
i = i + 1
if type(value) == type({}):
data.append(json.dumps(value))
else:
data.append(str(value))
dataAll.append(tuple(data))
command = "INSERT INTO `tv`.`link` (" + sql1 + ") VALUES (" + sql2 + ")"
db.executemany(command, dataAll)
selfmydb.commit()
# ---------------------------------------------------------------
# ---------------------------------------------------------------
if len(response['other']['type'])!=0:
type_data = []
for res in response['other']['type']:
type_data.append((res,playid))
command = "INSERT INTO `tv`.`classify` (`value`,`pid`) VALUES (%s,%s)"
db.executemany(command, type_data)
selfmydb.commit()
if len(response['other']['performer'])!=0:
performer_data = []
for res in response['other']['performer']:
performer_data.append((res,playid))
command = "INSERT INTO `tv`.`performer` (`name`,`pid`) VALUES (%s,%s)"
db.executemany(command, performer_data)
selfmydb.commit()
else:
print("已存在")