mirror of
https://github.com/lrnselfreliance/wrolpi.git
synced 2026-11-25 17:36:18 -05:00
Reworking modelers to only fetch matching models once to avoid making many queries per group.
This commit is contained in:
parent
ff292b6e3f
commit
a149cc8f69
5 changed files with 85 additions and 57 deletions
|
|
@ -55,18 +55,30 @@ class ArchiveDownloader(Downloader, ABC):
|
|||
archive_downloader = ArchiveDownloader(priority=100)
|
||||
|
||||
|
||||
def find_archive_file_in_group(group: List[File]):
|
||||
for file in group:
|
||||
if lib.is_singlefile_file(file.path):
|
||||
return file
|
||||
|
||||
|
||||
@register_modeler
|
||||
def archive_modeler(groups: Dict[str, List[File]], session: Session):
|
||||
archive_groups = {}
|
||||
for stem, group in groups.items():
|
||||
if any(lib.is_singlefile_file(i.path) for i in group):
|
||||
archive_groups[stem] = group
|
||||
archive_files = {stem: file for stem, group in groups.items() if (file := find_archive_file_in_group(group))}
|
||||
if not archive_files:
|
||||
# No archives in these groups.
|
||||
return
|
||||
# Get all matching Archive records (if any) in one query.
|
||||
archive_paths = [i.path for i in archive_files.values()]
|
||||
archive_records = {i.singlefile_path: i for i in
|
||||
session.query(Archive).filter(Archive.singlefile_path.in_(archive_paths))}
|
||||
|
||||
for stem, archive_file in archive_files.items():
|
||||
group = groups[stem]
|
||||
|
||||
for stem, group in archive_groups.items():
|
||||
readability_file, singlefile_file, readability_json_file, readability_txt_file, screenshot_file = \
|
||||
match_archive_files(group)
|
||||
|
||||
archive: Archive = Archive.find_by_path(singlefile_file.path, session)
|
||||
archive: Archive = archive_records.get(archive_file.path)
|
||||
if not archive:
|
||||
archive = Archive(singlefile_file=singlefile_file)
|
||||
session.add(archive)
|
||||
|
|
|
|||
|
|
@ -20,22 +20,39 @@ logger = logger.getChild(__name__)
|
|||
__all__ = ['video_modeler', 'VideoIndexer']
|
||||
|
||||
|
||||
def find_video_file_in_group(group: List[File]):
|
||||
return next((i for i in group if i.mimetype.startswith('video/') and i.path.suffix != '.part'), None)
|
||||
|
||||
|
||||
@register_modeler
|
||||
def video_modeler(groups: Dict[str, List[File]], session: Session):
|
||||
local_groups = groups.copy()
|
||||
videos = []
|
||||
for stem, group in local_groups.items():
|
||||
video_file = next((i for i in group if i.mimetype.startswith('video/') and i.path.suffix != '.part'), None)
|
||||
if not video_file:
|
||||
# Not a video group.
|
||||
continue
|
||||
new_videos = []
|
||||
|
||||
# Search all groups for video files.
|
||||
video_files = {stem: video_file for stem, group in groups.items() if
|
||||
(video_file := find_video_file_in_group(group))}
|
||||
if not video_files:
|
||||
# No videos in these groups.
|
||||
return
|
||||
# Get all matching Video records (if any) in one query.
|
||||
video_paths = [i.path for i in video_files.values()]
|
||||
video_records = {i.video_path: i for i in session.query(Video).filter(Video.video_path.in_(video_paths))}
|
||||
|
||||
for stem, video_file in video_files.items():
|
||||
group = groups[stem]
|
||||
|
||||
session.flush(group)
|
||||
poster_file = next((i for i in group if i.mimetype.split('/')[0] == 'image'), None)
|
||||
info_json_file = next((i for i in group if i.path.name.endswith('.info.json')), None)
|
||||
# Prefer WebVTT over SRT. (SRT cannot be displayed for HTML video).
|
||||
caption_file = next((i for i in group if i.path.name.endswith('.en.vtt')), None)
|
||||
caption_file = caption_file or next((i for i in group if i.path.name.endswith('.en.srt')), None)
|
||||
poster_file = info_json_file = caption_file = None
|
||||
for file in group:
|
||||
if file.mimetype.startswith('image/'):
|
||||
poster_file = file
|
||||
elif file.path.name.endswith('.info.json'):
|
||||
info_json_file = file
|
||||
# Prefer WebVTT over SRT. (SRT cannot be displayed for HTML video).
|
||||
elif file.path.name.endswith('.en.vtt'):
|
||||
caption_file = file
|
||||
elif file.path.name.endswith('.en.srt'):
|
||||
caption_file = caption_file or file
|
||||
|
||||
if poster_file:
|
||||
poster_file.associated = True
|
||||
|
|
@ -47,7 +64,7 @@ def video_modeler(groups: Dict[str, List[File]], session: Session):
|
|||
info_json_file.associated = True
|
||||
info_json_file.do_stats()
|
||||
|
||||
video = session.query(Video).filter_by(video_file=video_file).one_or_none()
|
||||
video: Video = video_records.get(video_file.path)
|
||||
if not video:
|
||||
video = Video(video_file=video_file)
|
||||
session.add(video)
|
||||
|
|
@ -67,11 +84,11 @@ def video_modeler(groups: Dict[str, List[File]], session: Session):
|
|||
video.size = size
|
||||
video_file.model = Video.__tablename__
|
||||
|
||||
videos.append(video)
|
||||
new_videos.append(video)
|
||||
|
||||
if videos:
|
||||
session.flush(videos)
|
||||
for video in videos:
|
||||
if new_videos:
|
||||
session.flush(new_videos)
|
||||
for video in new_videos:
|
||||
video.video_file.do_index()
|
||||
video.validate(session)
|
||||
|
||||
|
|
|
|||
|
|
@ -197,12 +197,11 @@ class EBook(ModelHelper, Base):
|
|||
return session.query(EBook).filter(EBook.ebook_path == path).one_or_none()
|
||||
|
||||
|
||||
def model_ebook(session: Session, ebook_file: File, files: List[File]) -> EBook:
|
||||
def model_ebook(session: Session, ebook_file: File, ebook: EBook, files: List[File]) -> EBook:
|
||||
"""Creates an EBook model based off a File. Searches for it's cover in the provided `files`."""
|
||||
# Multiple formats may share this cover.
|
||||
cover_file = next((i for i in files if i.mimetype.split('/')[0] == 'image'), None)
|
||||
|
||||
ebook = session.query(EBook).filter_by(ebook_file=ebook_file).one_or_none()
|
||||
if not ebook:
|
||||
ebook = EBook(ebook_file=ebook_file)
|
||||
session.add(ebook)
|
||||
|
|
@ -244,25 +243,31 @@ def model_ebook(session: Session, ebook_file: File, files: List[File]) -> EBook:
|
|||
|
||||
@register_modeler
|
||||
def ebook_modeler(groups: Dict[str, List[File]], session: Session):
|
||||
"""Searches for ebook files and models them into the ebook table.
|
||||
|
||||
May generate cover files for EPUBs."""
|
||||
local_groups = groups.copy()
|
||||
|
||||
for stem, group in local_groups.items():
|
||||
found_ebook = False
|
||||
"""Searches for ebook files and models them into the ebook table."""
|
||||
ebook_files = dict()
|
||||
for stem, group in groups.items():
|
||||
for file in group:
|
||||
if not mimetype_is_ebook(file.mimetype):
|
||||
continue
|
||||
if mimetype_is_ebook(file.mimetype):
|
||||
try:
|
||||
ebook_files[stem].append(file)
|
||||
except KeyError:
|
||||
ebook_files[stem] = [file, ]
|
||||
if not ebook_files:
|
||||
# No ebooks in these groups.
|
||||
return
|
||||
# Get all ebooks (if any) in one query.
|
||||
ebook_paths = [j.path for i in ebook_files.values() for j in i]
|
||||
ebook_records = {i.ebook_path: i for i in session.query(EBook).filter(EBook.ebook_path.in_(ebook_paths))}
|
||||
|
||||
for stem, ebook_files in ebook_files.items():
|
||||
group = groups[stem]
|
||||
for ebook_file in ebook_files:
|
||||
session.flush(group)
|
||||
model_ebook(session, file, group)
|
||||
ebook = ebook_records.get(ebook_file.path)
|
||||
model_ebook(session, ebook_file, ebook, group)
|
||||
|
||||
found_ebook = True
|
||||
|
||||
if found_ebook:
|
||||
# Claim this group for this ebook.
|
||||
del groups[stem]
|
||||
# Claim this group for this ebook.
|
||||
del groups[stem]
|
||||
|
||||
|
||||
@register_after_refresh
|
||||
|
|
|
|||
|
|
@ -23,7 +23,6 @@ class File(ModelHelper, Base):
|
|||
path: pathlib.Path = Column(MediaPathType, primary_key=True)
|
||||
|
||||
associated = Column(Boolean, default=False)
|
||||
directory: pathlib.Path = Column(MediaPathType) # aka the parent.
|
||||
full_stem = Column(String) # f'{directory}/{stem}'
|
||||
idempotency = Column(TZDateTime, default=lambda: now())
|
||||
indexed = Column(Boolean, default=False)
|
||||
|
|
@ -31,7 +30,6 @@ class File(ModelHelper, Base):
|
|||
model = Column(String) # Filled out by the modeler. Will be the table name (video, archive, etc.).
|
||||
modification_datetime = Column(TZDateTime)
|
||||
size = Column(Integer)
|
||||
stem = Column(String) # The file name, but without the suffix (aka extension). See: split_path_stem_and_suffix
|
||||
suffix = Column(String) # aka the extension.
|
||||
title = Column(String) # Provided by the modeler, or it will be the entire file name (with suffix).
|
||||
|
||||
|
|
@ -57,7 +55,6 @@ class File(ModelHelper, Base):
|
|||
path = self.path.relative_to(get_media_directory())
|
||||
d = dict(
|
||||
associated=self.associated,
|
||||
directory=self.directory,
|
||||
full_stem=self.full_stem,
|
||||
key=path, # React browser expects this.
|
||||
mimetype=self.mimetype,
|
||||
|
|
@ -65,7 +62,6 @@ class File(ModelHelper, Base):
|
|||
modified=self.modification_datetime, # React browser expects this.
|
||||
path=path,
|
||||
size=self.size,
|
||||
stem=self.stem,
|
||||
suffix=self.suffix,
|
||||
title=self.title,
|
||||
)
|
||||
|
|
@ -134,9 +130,8 @@ class File(ModelHelper, Base):
|
|||
if not self.mimetype:
|
||||
from wrolpi.files.lib import get_mimetype
|
||||
self.mimetype = get_mimetype(self.path)
|
||||
self.stem, self.suffix = split_path_stem_and_suffix(self.path)
|
||||
self.directory = self.path.parent
|
||||
self.full_stem = f'{self.directory}/{self.stem}'
|
||||
stem, self.suffix = split_path_stem_and_suffix(self.path)
|
||||
self.full_stem = f'{self.path.parent}/{stem}'
|
||||
self.title = self.title or self.path.name
|
||||
stat = self.path.stat()
|
||||
self.size = stat.st_size
|
||||
|
|
@ -149,7 +144,7 @@ class File(ModelHelper, Base):
|
|||
if changed:
|
||||
self.indexed = False
|
||||
# Use title as the a_text temporarily, this may be overwritten by indexer.
|
||||
self.a_text = self.a_text or self.stem
|
||||
self.a_text = self.a_text or stem
|
||||
return changed
|
||||
|
||||
@validates('path')
|
||||
|
|
|
|||
|
|
@ -350,16 +350,15 @@ async def test_files_indexer(test_session, make_files_structure, test_directory)
|
|||
assert info_json_file.path.suffix == '.json' and info_json_file.indexer == indexers.DefaultIndexer
|
||||
assert video_file.path.suffix == '.mp4' and video_file.indexer == videos.VideoIndexer
|
||||
|
||||
# Suffix, directory and name are correct.
|
||||
def assert_file_properties(file: File, suffix, directory, stem):
|
||||
assert file.suffix == suffix and file.directory == directory and file.stem == stem
|
||||
def assert_file_properties(file: File, suffix):
|
||||
assert file.suffix == suffix
|
||||
|
||||
assert_file_properties(text_file, '.txt', test_directory, 'a text file')
|
||||
assert_file_properties(zip_file, '.zip', test_directory, 'a zip file')
|
||||
assert_file_properties(image_file, '.jpeg', test_directory / 'images', 'an image file')
|
||||
assert_file_properties(unknown_file, '', test_directory, 'unknown file')
|
||||
assert_file_properties(info_json_file, '.info.json', test_directory / 'videos', 'a video file')
|
||||
assert_file_properties(video_file, '.mp4', test_directory / 'videos', 'a video file')
|
||||
assert_file_properties(text_file, '.txt')
|
||||
assert_file_properties(zip_file, '.zip')
|
||||
assert_file_properties(image_file, '.jpeg')
|
||||
assert_file_properties(unknown_file, '')
|
||||
assert_file_properties(info_json_file, '.info.json')
|
||||
assert_file_properties(video_file, '.mp4')
|
||||
|
||||
# File are indexed by their titles and contents.
|
||||
files, total = lib.search_files('file', 10, 0)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue