2022-11-02 14:37:57 -06:00
|
|
|
import copy
|
2021-12-30 22:23:32 -07:00
|
|
|
import datetime
|
|
|
|
|
import json
|
|
|
|
|
import pathlib
|
|
|
|
|
from uuid import uuid4
|
|
|
|
|
|
|
|
|
|
import pytest
|
2022-12-23 16:04:09 -07:00
|
|
|
import pytz
|
2021-12-30 22:23:32 -07:00
|
|
|
|
2022-02-02 20:43:11 -07:00
|
|
|
from modules.archive.lib import archive_strftime
|
2022-01-03 15:13:02 -07:00
|
|
|
from modules.archive.models import Archive, Domain
|
2022-08-06 05:22:07 +00:00
|
|
|
from wrolpi.files.models import File
|
2021-12-30 22:23:32 -07:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
|
def archive_directory(test_directory) -> pathlib.Path:
|
|
|
|
|
path = test_directory / 'archive'
|
|
|
|
|
path.mkdir()
|
|
|
|
|
return path
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
2022-08-06 05:22:07 +00:00
|
|
|
def archive_factory(test_session, archive_directory, make_files_structure):
|
2021-12-30 22:23:32 -07:00
|
|
|
def time_generator():
|
2022-12-23 16:04:09 -07:00
|
|
|
timestamp = datetime.datetime(2000, 1, 1, 0, 0, 0).astimezone(pytz.UTC)
|
2021-12-30 22:23:32 -07:00
|
|
|
while True:
|
|
|
|
|
timestamp += datetime.timedelta(seconds=1)
|
|
|
|
|
yield timestamp
|
|
|
|
|
|
|
|
|
|
now = time_generator()
|
|
|
|
|
|
2022-08-06 05:22:07 +00:00
|
|
|
def _(domain: str = None, url: str = None, title: str = 'NA', contents: str = None,
|
|
|
|
|
singlefile_contents: str = None) -> Archive:
|
2021-12-31 22:35:23 -07:00
|
|
|
if domain:
|
|
|
|
|
assert '/' not in domain
|
|
|
|
|
|
2021-12-31 20:23:49 -07:00
|
|
|
title = title or str(uuid4())
|
2021-12-30 22:23:32 -07:00
|
|
|
domain_dir = domain or title
|
|
|
|
|
domain_dir = archive_directory / domain_dir
|
|
|
|
|
domain_dir.mkdir(exist_ok=True)
|
|
|
|
|
|
2022-12-23 16:04:09 -07:00
|
|
|
archive_datetime = next(now)
|
2022-02-02 20:43:11 -07:00
|
|
|
timestamp = archive_strftime(archive_datetime)
|
2021-12-30 22:23:32 -07:00
|
|
|
|
2022-08-06 05:22:07 +00:00
|
|
|
json_contents = {}
|
|
|
|
|
if title != 'NA':
|
|
|
|
|
json_contents['title'] = title
|
|
|
|
|
if url:
|
|
|
|
|
json_contents['url'] = url
|
|
|
|
|
json_contents = json.dumps(json_contents)
|
|
|
|
|
singlefile_path, screenshot_path, readability_path, readability_txt_path, readability_json_path = \
|
|
|
|
|
make_files_structure({
|
|
|
|
|
str(domain_dir / f'{timestamp}_{title}.html'): singlefile_contents or '<html></html>',
|
|
|
|
|
str(domain_dir / f'{timestamp}_{title}.png'): None,
|
|
|
|
|
str(domain_dir / f'{timestamp}_{title}.readability.html'): '<html></html>',
|
|
|
|
|
str(domain_dir / f'{timestamp}_{title}.readability.txt'): contents,
|
|
|
|
|
str(domain_dir / f'{timestamp}_{title}.readability.json'): json_contents,
|
|
|
|
|
})
|
2021-12-30 22:23:32 -07:00
|
|
|
|
|
|
|
|
if domain:
|
|
|
|
|
domain = test_session.query(Domain).filter_by(domain=domain).one_or_none()
|
|
|
|
|
|
2022-08-06 05:22:07 +00:00
|
|
|
if not domain and domain_dir.name != 'NA':
|
2021-12-30 22:23:32 -07:00
|
|
|
domain = Domain(
|
2021-12-31 20:14:14 -07:00
|
|
|
domain=domain_dir.name,
|
2021-12-30 22:23:32 -07:00
|
|
|
directory=domain_dir,
|
|
|
|
|
)
|
|
|
|
|
test_session.add(domain)
|
2021-12-31 22:35:23 -07:00
|
|
|
test_session.flush([domain])
|
2021-12-30 22:23:32 -07:00
|
|
|
|
|
|
|
|
archive = Archive(
|
2022-08-06 05:22:07 +00:00
|
|
|
readability_json_file=File(path=readability_json_path) if readability_json_path else None,
|
|
|
|
|
readability_file=File(path=readability_path) if readability_path else None,
|
|
|
|
|
readability_txt_file=File(path=readability_txt_path) if readability_txt_path else None,
|
|
|
|
|
screenshot_file=File(path=screenshot_path) if screenshot_path else None,
|
|
|
|
|
singlefile_file=File(path=singlefile_path, model='archive'),
|
2021-12-30 22:23:32 -07:00
|
|
|
title=title,
|
2022-01-03 15:13:02 -07:00
|
|
|
url=url,
|
2022-08-06 05:22:07 +00:00
|
|
|
domain_id=domain.id if domain else None,
|
2022-02-02 20:43:11 -07:00
|
|
|
archive_datetime=archive_datetime,
|
2021-12-30 22:23:32 -07:00
|
|
|
)
|
|
|
|
|
test_session.add(archive)
|
2022-08-06 05:22:07 +00:00
|
|
|
archive.singlefile_file.do_index()
|
2021-12-30 22:23:32 -07:00
|
|
|
|
|
|
|
|
return archive
|
|
|
|
|
|
|
|
|
|
return _
|
2022-11-02 14:37:57 -06:00
|
|
|
|
|
|
|
|
|
|
|
|
|
SINGLEFILE_CONTENTS = '''<html><!--
|
|
|
|
|
Page saved with SingleFile
|
|
|
|
|
url: https://example.com
|
|
|
|
|
saved date: Thu May 12 2022 00:38:02 GMT+0000 (Coordinated Universal Time)
|
|
|
|
|
--><head><meta charset="utf-8">
|
|
|
|
|
|
|
|
|
|
<meta name="msapplication-TileColor" content="#2d89ef">
|
|
|
|
|
<meta name="theme-color" content="#000000">
|
|
|
|
|
|
|
|
|
|
<title>the title</title>
|
|
|
|
|
|
|
|
|
|
<body>
|
|
|
|
|
some body contents
|
|
|
|
|
</body>
|
|
|
|
|
|
|
|
|
|
</html>
|
|
|
|
|
'''
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
|
def singlefile_contents() -> str:
|
|
|
|
|
"""Return a short HTML string that contains an example Singlefile file."""
|
|
|
|
|
return copy.deepcopy(SINGLEFILE_CONTENTS)
|