Skip to content

Commit 36a0c16

Browse files
committed
update database queries
1 parent 6708c00 commit 36a0c16

14 files changed

+1066
-567
lines changed

CMakeLists.txt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,10 +166,12 @@ if(NOT TEST)
166166
${CMAKE_SOURCE_DIR}/database/scrape_common.py
167167
${CMAKE_SOURCE_DIR}/database/scrape_trle.py
168168
${CMAKE_SOURCE_DIR}/database/scrape_trle_download.py
169-
${CMAKE_SOURCE_DIR}/database/tombll_add_data.py
170169
${CMAKE_SOURCE_DIR}/database/tombll_common.py
171-
${CMAKE_SOURCE_DIR}/database/tombll_get_data.py
170+
${CMAKE_SOURCE_DIR}/database/tombll_create.py
171+
${CMAKE_SOURCE_DIR}/database/tombll_delete.py
172172
${CMAKE_SOURCE_DIR}/database/tombll_manage_data.py
173+
${CMAKE_SOURCE_DIR}/database/tombll_read.py
174+
${CMAKE_SOURCE_DIR}/database/tombll_update.py
173175
${CMAKE_SOURCE_DIR}/database/make_tombll_database.py
174176
DESTINATION ${CMAKE_INSTALL_PREFIX}/share/${PROJECT_NAME}
175177
)

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,8 @@ If you did just follow the command above you can use:
9595

9696

9797
```shell
98-
python3 tombll_get_data.py 3684
98+
python tombll_manage_data.py -h
99+
python3 tombll_manage_data.py -a 3684
99100

100101
```
101102
Now that you have an data.json file you get a chance to edit it.
@@ -110,7 +111,7 @@ But it has to be a zip file at this point.
110111
```
111112

112113
```shell
113-
python3 tombll_add_data.py data.json
114+
python3 tombll_manage_data.py -af data.json
114115

115116
```
116117

database/make_tombll_database.py

Lines changed: 45 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
"""
2-
Make database file for trle.net and TRCustoms.org
3-
"""
1+
"""Make database file for trle.net and TRCustoms.org."""
42
import sqlite3
53
import json
64
import os
@@ -44,8 +42,8 @@
4442
duration INT,
4543
type INT NOT NULL,
4644
class INT,
47-
trleID INT,
48-
trcustomsID INT,
45+
trleID INT UNIQUE,
46+
trcustomsID INT UNIQUE,
4947
FOREIGN KEY (difficulty) REFERENCES InfoDifficulty(InfoDifficultyID),
5048
FOREIGN KEY (duration) REFERENCES InfoDuration(InfoDurationID),
5149
FOREIGN KEY (type) REFERENCES InfoType(InfoTypeID),
@@ -76,7 +74,7 @@ class INT,
7674
name TEXT NOT NULL UNIQUE,
7775
size FLOAT NOT NULL,
7876
md5sum TEXT NOT NULL,
79-
url TEXT,
77+
url TEXT UNIQUE,
8078
version INT,
8179
release DATE
8280
)''')
@@ -123,7 +121,7 @@ class INT,
123121
CREATE TABLE Level (
124122
LevelID INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
125123
body TEXT NOT NULL,
126-
walkthrough TEXT NOT NULL,
124+
walkthrough TEXT NOT NULL,
127125
infoID INTEGER NOT NULL,
128126
FOREIGN KEY (infoID) REFERENCES Info(InfoID)
129127
)''')
@@ -230,10 +228,8 @@ class INT,
230228

231229
# Add Game File data
232230
for i in range(1, 6):
233-
file = f'data/fileList-TR{i}.json'
234-
235231
# Load data from JSON file
236-
with open(file, 'r', encoding='utf-8') as json_file:
232+
with open(f'data/fileList-TR{i}.json', 'r', encoding='utf-8') as json_file:
237233
file_info = json.load(json_file)
238234

239235
name = file_info.get("name")
@@ -249,41 +245,55 @@ class INT,
249245

250246
if relative_path and file_md5:
251247
# Check if the file with the same md5sum already exists in File table
252-
C.execute("SELECT FileID FROM File WHERE md5sum = ? AND path = ?",
253-
(file_md5, relative_path))
248+
C.execute(
249+
"SELECT FileID FROM File WHERE md5sum = ? AND path = ?",
250+
(file_md5, relative_path)
251+
)
254252
existing_file = C.fetchone()
255253

256254
if existing_file:
257255
# File already exists, use the existing FileID
258256
file_id = existing_file[0]
259-
print("File with md5sum %s and path %s already exists. Using existing FileID: %s",
260-
file_md5, relative_path, file_id)
257+
print(
258+
"File with md5sum %s and path %s already exists. Using existing FileID: %s",
259+
file_md5,
260+
relative_path,
261+
file_id
262+
)
261263
else:
262264
# File doesn't exist, insert it into File table
263-
C.execute("INSERT INTO File (md5sum, path) VALUES (?, ?)",
264-
(file_md5, relative_path))
265+
C.execute(
266+
"INSERT INTO File (md5sum, path) VALUES (?, ?)",
267+
(file_md5, relative_path)
268+
)
265269
file_id = C.lastrowid
266270
print(f"Inserted new file with md5sum {file_md5}. New FileID: {file_id}")
267271

268-
try:
269-
# Check if the combination of fileID and gameID already exists in GameFileList
270-
C.execute("SELECT 1 FROM GameFileList WHERE fileID = ? AND gameID = ?",
271-
(file_id, game_id))
272-
existing_combination = C.fetchone()
273-
274-
if not existing_combination:
275-
# Combination doesn't exist, insert it into GameFileList
276-
C.execute("INSERT INTO GameFileList (fileID, gameID) VALUES (?, ?)",
277-
(file_id, game_id))
278-
else:
279-
# Combination already exists, print a message or handle it as needed
280-
print(f"Combination of FileID {file_id} and LevelID {game_id} "
281-
"already exists in GameFileList. Skipping insertion.")
282-
283-
except sqlite3.IntegrityError as file_list_error:
284-
# Print more details about the uniqueness violation
285-
print(f"Uniqueness violation in GameFileList: {file_list_error}")
286-
print(f"FileID: {file_id}, LevelID: {game_id}")
272+
try:
273+
# Check if the combination of fileID and gameID already exists in GameFileList
274+
C.execute(
275+
"SELECT 1 FROM GameFileList WHERE fileID = ? AND gameID = ?",
276+
(file_id, game_id)
277+
)
278+
existing_combination = C.fetchone()
279+
280+
if not existing_combination:
281+
# Combination doesn't exist, insert it into GameFileList
282+
C.execute(
283+
"INSERT INTO GameFileList (fileID, gameID) VALUES (?, ?)",
284+
(file_id, game_id)
285+
)
286+
else:
287+
# Combination already exists, print a message or handle it as needed
288+
print(
289+
f"Combination of FileID {file_id} and LevelID {game_id} "
290+
"already exists in GameFileList. Skipping insertion."
291+
)
292+
293+
except sqlite3.IntegrityError as file_list_error:
294+
# Print more details about the uniqueness violation
295+
print(f"Uniqueness violation in GameFileList: {file_list_error}")
296+
print(f"FileID: {file_id}, LevelID: {game_id}")
287297

288298
CONNECTION.commit()
289299
CONNECTION.close()

database/scrape_trle.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -310,8 +310,8 @@ def get_trle_level(soup, data):
310310
data['duration'] = scrape_common.get_trle_duration(soup)
311311
data['screen'] = scrape_common.get_trle_screen(soup)
312312
data['large_screens'] = scrape_common.get_trle_large_screens(soup)
313-
level_id = scrape_common.trle_url_to_int(soup.find('a', string='Download').get('href'))
314-
data['zip_files'] = scrape_trle_download.get_zip_file_info(level_id)
313+
data['trle_id'] = scrape_common.trle_url_to_int(soup.find('a', string='Download').get('href'))
314+
data['zip_files'] = scrape_trle_download.get_zip_file_info(data['trle_id'])
315315
data['body'] = scrape_common.get_trle_body(soup)
316316
data['walkthrough'] = get_trle_walkthrough(soup)
317317

database/tombll.db-journal

96.7 KB
Binary file not shown.

0 commit comments

Comments
 (0)