mirror of
https://github.com/lrnselfreliance/wrolpi.git
synced 2026-11-24 17:06:18 -05:00
Updating Zim downloading so that Zim files are indexed and modeled when downloaded with new style of modeling like Video or Archive.
This commit is contained in:
parent
233dcaa0a3
commit
0ac1e559bf
4 changed files with 50 additions and 17 deletions
|
|
@ -35,11 +35,23 @@ __all__ = [
|
|||
'get_unique_paths',
|
||||
'get_zim',
|
||||
'get_zims',
|
||||
'zim_modeler',
|
||||
'model_zim',
|
||||
'zim_download_url_to_name',
|
||||
'zim_modeler',
|
||||
]
|
||||
|
||||
|
||||
def model_zim(file_group: FileGroup, session: Session) -> Zim:
|
||||
"""Create a Zim record for the provided FileGroup."""
|
||||
zim = Zim(file_group=file_group, path=file_group.primary_path)
|
||||
session.add(zim)
|
||||
file_group.title = file_group.primary_path.name
|
||||
file_group.a_text = split_file_name_words(file_group.primary_path.name)
|
||||
file_group.indexed = True
|
||||
file_group.model = 'zim'
|
||||
return zim
|
||||
|
||||
|
||||
@register_modeler
|
||||
async def zim_modeler():
|
||||
while True:
|
||||
|
|
@ -59,13 +71,14 @@ async def zim_modeler():
|
|||
zim_id = None
|
||||
try:
|
||||
if not zim:
|
||||
zim = Zim(file_group=file_group, path=file_group.primary_path)
|
||||
session.add(zim)
|
||||
session.flush([zim])
|
||||
zim_id = zim.id
|
||||
file_group.title = file_group.primary_path.name
|
||||
file_group.a_text = split_file_name_words(file_group.primary_path.name)
|
||||
file_group.indexed = True
|
||||
zim = model_zim(file_group, session)
|
||||
zim.flush()
|
||||
else:
|
||||
zim_id = zim.id
|
||||
file_group.title = file_group.primary_path.name
|
||||
file_group.a_text = split_file_name_words(file_group.primary_path.name)
|
||||
file_group.indexed = True
|
||||
file_group.model = 'zim'
|
||||
except Exception as e:
|
||||
if PYTEST:
|
||||
raise
|
||||
|
|
|
|||
|
|
@ -12,7 +12,7 @@ from sqlalchemy.orm.exc import NoResultFound # noqa
|
|||
|
||||
from modules.zim.errors import UnknownZimEntry, UnknownZimTagEntry, UnknownZim
|
||||
from wrolpi import dates, tags
|
||||
from wrolpi.common import Base, logger, get_relative_to_media_directory
|
||||
from wrolpi.common import Base, logger, get_relative_to_media_directory, ModelHelper
|
||||
from wrolpi.dates import TZDateTime
|
||||
from wrolpi.db import optional_session, get_db_curs
|
||||
from wrolpi.downloader import Download, download_manager
|
||||
|
|
@ -60,7 +60,7 @@ class EntrySummary:
|
|||
return d
|
||||
|
||||
|
||||
class Zim(Base):
|
||||
class Zim(Base, ModelHelper):
|
||||
"""Records of Zim files.
|
||||
|
||||
Zim files stores wiki content (and much more) for offline usage. Zim files are compressed data that can be searched.
|
||||
|
|
@ -100,9 +100,6 @@ class Zim(Base):
|
|||
@optional_session
|
||||
def get_by_path(path: pathlib.Path, session: Session = None) -> Optional['Zim']:
|
||||
zim = session.query(Zim).filter_by(path=path).one_or_none()
|
||||
if not zim and path.is_file():
|
||||
zim = Zim.from_paths(session, path)
|
||||
session.commit()
|
||||
return zim
|
||||
|
||||
@staticmethod
|
||||
|
|
@ -142,6 +139,18 @@ class Zim(Base):
|
|||
self.path.unlink(missing_ok=True)
|
||||
session.delete(self)
|
||||
|
||||
@staticmethod
|
||||
def can_model(file_group: FileGroup) -> bool:
|
||||
if file_group.primary_path.suffix.lower() == '.zim':
|
||||
return True
|
||||
return False
|
||||
|
||||
@staticmethod
|
||||
def do_model(file_group: FileGroup, session: Session) -> 'Zim':
|
||||
from modules.zim.lib import model_zim
|
||||
zim = model_zim(file_group, session)
|
||||
return zim
|
||||
|
||||
@functools.cached_property
|
||||
def zim_metadata(self) -> ZimMetadata:
|
||||
zim = self.get_zim()
|
||||
|
|
|
|||
|
|
@ -200,6 +200,8 @@ async def test_zim_download(test_session, kiwix_download_zim, test_directory, te
|
|||
assert (test_directory / 'zims/wikipedia_es_all_maxi_2023-06.zim').is_file()
|
||||
assert (test_directory / 'zims/wikipedia_es_all_maxi_2023-06.zim').stat().st_size
|
||||
assert once_download.attempts == 1
|
||||
# All files downloaded were indexed and are Zim files.
|
||||
assert all(i.model == 'zim' and i.indexed is True for i in test_session.query(FileGroup))
|
||||
|
||||
# Download catalog again, should not re-download the existing file.
|
||||
recurring_download.status = 'new'
|
||||
|
|
@ -244,6 +246,9 @@ async def test_zim_download(test_session, kiwix_download_zim, test_directory, te
|
|||
# But old file is now outdated.
|
||||
assert flags.outdated_zims.is_set()
|
||||
|
||||
# All files downloaded were indexed and are Zim files.
|
||||
assert all(i.model == 'zim' and i.indexed is True for i in test_session.query(FileGroup))
|
||||
|
||||
# Delete the outdated file.
|
||||
await lib.remove_outdated_zim_files()
|
||||
assert test_session.query(FileGroup).count() == 1
|
||||
|
|
@ -383,16 +388,19 @@ async def test_zim_subscription_download_import(async_client, test_session):
|
|||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_zim_modeler(test_session, zim_factory):
|
||||
async def test_zim_modeler(async_client, test_session, zim_factory):
|
||||
"""Zim modeler sets FileGroup title and a_text."""
|
||||
zim_factory('the_zim_title')
|
||||
zim = zim_factory('the_zim_title.zim')
|
||||
assert zim.file_group.indexed is False
|
||||
test_session.commit()
|
||||
|
||||
await files_lib.refresh_files()
|
||||
|
||||
fg: FileGroup = test_session.query(FileGroup).one()
|
||||
assert fg.title == 'the_zim_title'
|
||||
assert fg.a_text == 'the zim title'
|
||||
assert fg.indexed is True
|
||||
assert fg.title == 'the_zim_title.zim'
|
||||
assert fg.a_text == 'the zim title zim'
|
||||
assert fg.model == 'zim'
|
||||
|
||||
|
||||
def test_get_custom_zims_directory(test_directory, test_wrolpi_config):
|
||||
|
|
|
|||
|
|
@ -441,6 +441,7 @@ class FileGroup(ModelHelper, Base):
|
|||
from modules.videos.models import Video
|
||||
from modules.archive.models import Archive
|
||||
from wrolpi.files.ebooks import EBook
|
||||
from modules.zim.models import Zim
|
||||
|
||||
if Video.can_model(self):
|
||||
return Video
|
||||
|
|
@ -448,6 +449,8 @@ class FileGroup(ModelHelper, Base):
|
|||
return EBook
|
||||
elif Archive.can_model(self):
|
||||
return Archive
|
||||
elif Zim.can_model(self):
|
||||
return Zim
|
||||
|
||||
def do_model(self, session: Session) -> Optional[ModelHelper]:
|
||||
"""Get/Create the Model record (Video/Archive/etc.) for this FileGroup, if any."""
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue