This repository has been archived by the owner on Nov 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Database.py
195 lines (178 loc) · 7.07 KB
/
Database.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
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
import json
import os
import sqlite3
class Database(object):
"""docstring for Database"""
def __init__(self, path, filename):
self.path = path
self.filename = filename + '.sqlite'
if os.path.exists(self.path + '/' + self.filename):
self.file_exists = True
else:
self.file_exists = False
self.c = sqlite3.connect(self.path + '/' + self.filename)
if not self.file_exists:
self.create_database()
self.verify_database()
self.check_categories()
self.check_status()
self.load_values()
@property
def last_entry(self):
cur = self.c.cursor()
cur.execute('SELECT * FROM torrents ORDER BY torrent_id DESC LIMIT 1;')
try:
return cur.fetchone()[0]
except:
return 0
def create_database(self):
print('Creating database...')
self.c.execute('CREATE TABLE categories (category_id INTEGER NOT NULL, \
category_name TEXT NOT NULL, PRIMARY KEY (category_id))')
self.c.execute(
'CREATE TABLE sub_categories (sub_category_id INTEGER NOT NULL, \
sub_category_name TEXT NOT NULL, PRIMARY KEY (sub_category_id))'
)
self.c.execute('CREATE TABLE status (status_id INTEGER NOT NULL, \
status_name TEXT NOT NULL, PRIMARY KEY (status_id))')
self.c.execute('CREATE TABLE torrents \
(torrent_id INTEGER NOT NULL, torrent_name TEXT NOT NULL, \
torrent_hash TEXT NOT NULL, category_id INTEGER NOT NULL, \
sub_category_id INTEGER NOT NULL, status_id INTEGER NOT NULL, \
PRIMARY KEY (torrent_id), \
FOREIGN KEY (category_id) REFERENCES categories(category_id), \
FOREIGN KEY (sub_category_id) \
REFERENCES sub_categories(sub_category_id), \
FOREIGN KEY (status_id) REFERENCES status(status_id))')
def check_categories(self):
with open(self.path + '/categories.json') as f:
if self.filename == 'sukebei.sqlite':
category_json = json.load(f)['Sukebei']
else:
category_json = json.load(f)['Nyaa']
cur = self.c.cursor()
cur.execute('SELECT * FROM categories')
categories = cur.fetchall()
if len(categories) == 0:
for i, cat in enumerate(category_json, start=1):
self.write_category((i, cat))
elif len(categories) > 0:
t1, t2 = zip(*categories)
next_id = len(categories) + 1
for cat in category_json:
if cat not in t2:
self.write_category((next_id, cat))
next_id += 1
cur.execute('SELECT * FROM sub_categories')
sub_categories = cur.fetchall()
try:
t1, t2 = zip(*sub_categories)
except:
t2 = ()
next_id = len(sub_categories) + 1
for cat in category_json:
for sub_cat in category_json[cat]:
if sub_cat not in t2:
self.write_subcategory((next_id, sub_cat))
cur.execute('SELECT * FROM sub_categories')
t1, t2 = zip(*cur.fetchall())
next_id += 1
def check_status(self):
cur = self.c.cursor()
cur.execute('SELECT * FROM status')
status = ['normal', 'remake', 'trusted', 'a+']
db_status = cur.fetchall()
if len(db_status) == 0:
for i, stat in enumerate(status, start=1):
self.write_status((i, stat))
elif len(db_status) > 0:
t1, t2 = zip(*db_status)
next_id = len(db_status) + 1
for stat in status:
if stat not in t2:
self.write_status((next_id, stat))
def entry_exists(self, id):
cur = self.c.cursor()
t = (id,)
cur.execute('SELECT * FROM torrents WHERE torrent_id = ?', t)
if cur.fetchall() == []:
return False
else:
return True
def load_values(self):
cur = self.c.cursor()
self.categories = {}
self.sub_categories = {}
self.status = {}
cur.execute('SELECT * FROM categories')
for t1, t2 in cur.fetchall():
self.categories[t2] = t1
cur.execute('SELECT * FROM sub_categories')
for t1, t2 in cur.fetchall():
self.sub_categories[t2] = t1
cur.execute('SELECT * FROM status')
for t1, t2 in cur.fetchall():
self.status[t2] = t1
def verify_database(self):
print('Verifying database...')
cur = self.c.cursor()
comparison = [
(0, 'category_id', 'INTEGER', 1, None, 1),
(1, 'category_name', 'TEXT', 1, None, 0)
]
cur.execute('PRAGMA table_info(categories);')
if cur.fetchall() == comparison:
print('Table \'categories\' verified.')
else:
print('Table \'categories\' broken.')
exit()
comparison = [
(0, 'sub_category_id', 'INTEGER', 1, None, 1),
(1, 'sub_category_name', 'TEXT', 1, None, 0)
]
cur.execute('PRAGMA table_info(sub_categories);')
if cur.fetchall() == comparison:
print('Table \'sub_categories\' verified.')
else:
print('Table \'sub_categories\' broken.')
exit()
comparison = [
(0, 'status_id', 'INTEGER', 1, None, 1),
(1, 'status_name', 'TEXT', 1, None, 0)
]
cur.execute('PRAGMA table_info(status);')
if cur.fetchall() == comparison:
print('Table \'status\' verified.')
else:
print('Table \'status\' broken.')
exit()
comparison = [
(0, 'torrent_id', 'INTEGER', 1, None, 1),
(1, 'torrent_name', 'TEXT', 1, None, 0),
(2, 'torrent_hash', 'TEXT', 1, None, 0),
(3, 'category_id', 'INTEGER', 1, None, 0),
(4, 'sub_category_id', 'INTEGER', 1, None, 0),
(5, 'status_id', 'INTEGER', 1, None, 0)
]
cur.execute('PRAGMA table_info(torrents);')
if cur.fetchall() == comparison:
print('Table \'torrents\' verified.')
else:
print('Table \'torrents\' broken.')
exit()
def write_category(self, data):
print('Writing category \'{}\' into database...'.format(data[1]))
self.c.execute('INSERT INTO categories VALUES (?, ?)', data)
self.c.commit()
def write_subcategory(self, data):
print('Writing subcategory \'{}\' into database...'.format(data[1]))
self.c.execute('INSERT INTO sub_categories VALUES (?, ?)', data)
self.c.commit()
def write_status(self, data):
print('Writing status \'{}\' into database...'.format(data[1]))
self.c.execute('INSERT INTO status VALUES (?, ?)', data)
self.c.commit()
def write_torrent(self, data):
'''id, name, hash, category, sub_category, status'''
self.c.execute('INSERT INTO torrents VALUES (?, ?, ?, ?, ?, ?)', data)
self.c.commit()