Skip to content

Commit

Permalink
library: detect and update library if better identification method is…
Browse files Browse the repository at this point in the history
… available
  • Loading branch information
a1ex4 committed May 1, 2024
1 parent 60576f2 commit f090b2d
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 10 deletions.
21 changes: 13 additions & 8 deletions app/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,20 +188,25 @@ def serve_game(id):

def scan_library():
library = app_settings['library']['path']
load_titledb(app_settings)
print('Scanning library...')
if not os.path.isdir(library):
print(f'Library path {library} does not exists.')
return
_, files = getDirsAndFiles(library)
for n, filepath in enumerate(files):
if exists := db.session.query(
db.session.query(Files).filter_by(filepath=filepath).exists()
).scalar():
continue

if app_settings['valid_keys']:
current_identification = 'cnmt'
else:
print('Invalid or non existing keys.txt, title identification fallback to filename only.')
current_identification = 'filename'

all_files_with_current_identification = get_all_files_with_identification(current_identification)
files_to_identify = [f for f in files if f not in all_files_with_current_identification]
nb_to_identify = len(files_to_identify)
for n, filepath in enumerate(files_to_identify):
file = filepath.replace(library, "")
print(f'Identifiying file ({n+1}/{len(files)}): {file}')
if not app_settings['valid_keys']:
print('Invalid or non existing keys.txt, title identification fallback to filename only.')
print(f'Identifiying file ({n+1}/{nb_to_identify}): {file}')

file_info = identify_file(filepath)

Expand Down
18 changes: 16 additions & 2 deletions app/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class Files(db.Model):
version = db.Column(db.String)
extension = db.Column(db.String)
size = db.Column(db.Integer)
identification = db.Column(db.String)

class User(UserMixin, db.Model):
id = db.Column(db.Integer, primary_key=True)
Expand Down Expand Up @@ -57,7 +58,15 @@ def add_to_titles_db(library, file_info):
if exists := db.session.query(
db.session.query(Files).filter_by(filepath=filepath).exists()
).scalar():
return
existing_entry = db.session.query(Files).filter_by(filepath=filepath).all()
existing_entry_data = to_dict(existing_entry[0])
current_identification = existing_entry_data["identification"]
new_identification = file_info["identification"]
if new_identification == current_identification:
return
else:
# delete old entry and replace with updated one
db.session.query(Files).filter_by(filepath=filepath).delete()

# print(f'New file to add: {filepath}')
new_title = Files(
Expand All @@ -71,6 +80,7 @@ def add_to_titles_db(library, file_info):
version = file_info["version"],
extension = file_info["extension"],
size = file_info["size"],
identification = file_info["identification"],
)
db.session.add(new_title)

Expand All @@ -85,4 +95,8 @@ def get_all_titles_from_db():
def get_all_title_files(title_id):
title_id = title_id.upper()
results = db.session.query(Files).filter_by(title_id=title_id).all()
return [to_dict(r) for r in results]
return [to_dict(r) for r in results]

def get_all_files_with_identification(identification):
results = db.session.query(Files).filter_by(identification=identification).all()
return[to_dict(r)['filepath'] for r in results]
26 changes: 26 additions & 0 deletions app/test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import sys
from pathlib import Path
from binascii import hexlify as hx, unhexlify as uhx

sys.path.append('./NSTools/py')
from nstools.Fs import Pfs0, Nca, Type, factory, Nsp
from nstools.lib import FsTools
from nstools.nut import Keys

from titles import *

Keys.load('/home/a1ex/projects/ownfoil/app/config/keys.txt')

f = '/storage/media/games/switch/The Legend of Zelda Breath of the Wild [NSP]/The Legend of Zelda Breath of the Wild [01007EF00011E800][v786432].nsp'
nsp = Nsp.Nsp(f)
nsp.open()
# container.open(filepath, 'rb')
for nspf in nsp:
if isinstance(nspf, Nca.Nca) and nspf.header.contentType == Type.Content.META:
print(nspf.header.contentType)
for section in nspf:
if isinstance(section, Pfs0.Pfs0):
Cnmt = section.getCnmt()
# p = Path(f).resolve()
# container = factory(Path(f).resolve())
# container.open(f, 'rb')
4 changes: 4 additions & 0 deletions app/titles.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,15 +159,18 @@ def identify_file(filepath):
title_id, app_type = identify_appId(app_id)
else:
title_id = app_id
identification = 'cnmt'
except Exception as e:
print(f'Could not identify file {filepath} from metadata: {e}. Trying identification with filename...')
app_id, title_id, app_type, version = identify_file_from_filename(filename)
identification = 'filename'
if app_id is None:
print(f'Unable to extract title from filename: {filename}')
return None

else:
app_id, title_id, app_type, version = identify_file_from_filename(filename)
identification = 'filename'
if app_id is None:
print(f'Unable to extract title from filename: {filename}')
return None
Expand All @@ -182,6 +185,7 @@ def identify_file(filepath):
'version': version,
'extension': extension,
'size': get_file_size(filepath),
'identification': identification,
}


Expand Down

0 comments on commit f090b2d

Please sign in to comment.