-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabaseControl.py
More file actions
153 lines (136 loc) · 5.67 KB
/
Copy pathdatabaseControl.py
File metadata and controls
153 lines (136 loc) · 5.67 KB
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import sqlite3
import os
class databaseControl():
def __init__(self):
dir_path = os.path.dirname(os.path.realpath(__file__))
self.path = os.path.join(dir_path, "Manga")
if not (os.path.isfile(os.path.join(dir_path,"manga.db"))):
self.con = sqlite3.connect("manga.db")
self.cur = self.con.cursor()
self.cur.execute('''CREATE TABLE manga
(
id integer not null primary key autoincrement,
name varchar(50),
link varchar(200)
)
''')
self.cur.execute('''
CREATE TABLE chapter
(
id integer not null primary key autoincrement,
name varchar(100),
link varchar(200),
manga_id integer references anime(id) on delete cascade
)
''')
self.con.commit()
else:
self.con = sqlite3.connect("manga.db")
self.cur = self.con.cursor()
def insertIntoAnime(self, name, links):
"""
Inserts data to the table Anime.
name => Name of an anime
links => Links to that anime
"""
id = self.getOneDetail("manga", "id", "name", name)
if not id:
self.cur.execute("INSERT INTO manga (name, link) values (?,?)" , [name, links])
folder = os.path.join(self.path,name.translate({ord(c):" " for c in "!@#$%^&*()[]{};:,./<>?\|`~-=_+'\""})).replace(" ","-")
if not os.path.isdir(folder):
os.mkdir(folder)
print("{} added to the database".format(name))
else:
print("{} already exists in the database".format(name))
def insertIntoManga(self, name, link, manga_name):
"""
Insert data to the Manga table
name => Chapter Name
link => link to that chapter
manga_name => Anime name
"""
id = self.getOneDetail("manga", "id", "name",manga_name)
if "Chapter" in name:
name = name[name.find("Chapter"):]+'_'+manga_name
if not self.getOneDetail("chapter", "id", "name", name):
self.cur.execute("INSERT INTO chapter (name, link, manga_id) values (?,?,?)",[name, link, id])
# folder = os.path.join(os.path.join(self.path,manga_name), name.translate({ord(c):" " for c in "!@#$%^&*()[]{};:,./<>?\|`~-=_+"})).replace(" ","-")
# if not os.path.isdir(folder):
# os.mkdir(folder)
# print("INSERT INTO chapter (name, link, manga_id) values (?,?,?)".replace('?','%s') % [name, link, id])
print("{} added to the database".format(name))
else:
print("{} already exists in the database".format(name))
def getLatestId(self, table_name) -> int:
"""
Get the latest id in a table
table_name => Name of the table
"""
sql = "SELECT id from {} ORDER BY id DESC LIMIT 1".format(table_name)
try:
value = self.cur.execute(sql).fetchone()[0]
except TypeError:
return 1
return value+1
def getOneDetail(self, table, row, comparitor, name, one=True):
"""
Get's one row details from table.
table-> name of the table,
row -> one row that is needed to be extracted.
comparitor -> compare to select data
name -> required data condition
"""
sql = "SELECT {} FROM {} WHERE {} LIKE ?".format(row, table, comparitor)
self.cur.execute(sql, ['%'+name+'%'])
if one:
try:
return self.cur.fetchone()[0]
except TypeError:
return 0
else:
return self.cur.fetchall()
def get_more_details(self, row_to_display, table):
if isinstance(row_to_display, list):
select = ""
for index, row in enumerate(row_to_display):
select = select + row
if not index == len(row_to_display) - 1:
select = select + ','
else:
select = row_to_display
sql = "SELECT {} FROM {}".format(select, table)
self.cur.execute(sql)
return self.cur.fetchall()
def getDetailJoin(self, table1, table2, row_to_display, row1, row2, name_manga, one=True):
if table1 == 'manga':
status = True
table1 = 'SQLINNER'
else:
status = False
table2 = 'SQLINNER'
if isinstance(row_to_display, list):
select = ""
for index, row in enumerate(row_to_display):
select = select + table1 + '.' + row
if not index == len(row_to_display) - 1:
select = select + ", "
else:
select = table1 + '.' + row
# print(select)
table11 = '(SELECT * FROM manga WHERE manga.name="{}") AS SQLINNER'.format(name_manga)
if status:
sql = "SELECT DISTINCT {} FROM {} INNER JOIN {} ON {}.{}={}.{}".format(select, table11, table2, table1, row1, table2, row2)
else:
sql = "SELECT DISTINCT {} FROM {} INNER JOIN {} ON {}.{}={}.{}".format(select, table1, table11, table1, row1, table2, row2)
# print(sql)
self.cur.execute(sql)
if one:
return self.cur.fetchone()[0]
else:
return self.cur.fetchall()
def __del__(self):
self.con.commit()
self.con.close()
if __name__ == '__main__':
trying = databaseControl()
print(trying.getDetailJoin('chapter', 'manga', ['name'], 'manga_id', 'id', 'Attack On Titan'))