Using aria2c to download files. Removing custom file download functions.

This commit is contained in:
Roland 2025-01-10 11:32:37 -07:00
parent 95a524ba33
commit 220cb8010f
No known key found for this signature in database
GPG key ID: 1BA620CE6C9A4680
6 changed files with 185 additions and 59 deletions

View file

@ -1,7 +1,6 @@
import asyncio
import json
import pathlib
from http import HTTPStatus
from itertools import zip_longest
from typing import List, Dict
from uuid import uuid4
@ -28,33 +27,18 @@ def kiwix_download_manager(test_download_manager) -> DownloadManager:
@pytest.fixture
def kiwix_download_zim(test_directory, kiwix_download_manager, test_zim_bytes):
def kiwix_download_zim(test_directory, kiwix_download_manager, test_zim_bytes, mock_downloader_download_file):
async def do_download(download_info: DownloadFileInfo = None, expected_url: str = None,
download_file_side_effect=None, hrefs: List[str] = None):
async def _download_file(url: str, output_path: pathlib.Path = None, info: DownloadFileInfo = None):
if expected_url:
assert url == expected_url
if download_info:
assert info == download_info
output_path.write_bytes(test_zim_bytes)
with mock.patch('modules.zim.downloader.fetch_hrefs') as mock_fetch_hrefs, \
mock.patch('modules.zim.downloader.get_download_info') as mock_get_download_info, \
mock.patch('modules.zim.downloader.download_file') as mock_download_file:
with mock.patch('modules.zim.downloader.fetch_hrefs') as mock_fetch_hrefs:
# Fake the download, but make sure it is called correctly. Also creates a Zim file at the output_path.
mock_download_file.side_effect = download_file_side_effect or _download_file
# These are the links in the parent directory of the url.
mock_fetch_hrefs.return_value = hrefs or ['?C=N;O=D', '?C=M;O=A', '?C=S;O=A', '?C=D;O=A', '/zim/',
'wikipedia_es_all_maxi_2023-05.zim',
'wikipedia_es_all_maxi_2023-06.zim',
'wikipedia_es_all_nopic_2023-05.zim',
'wikipedia_es_all_nopic_2023-06.zim']
# This is the DownloadInfo for the latest Zim file that was selected.
mock_get_download_info.return_value = download_info or DownloadFileInfo(
status=HTTPStatus.OK,
name='wikipedia_es_all_maxi_2023-06.zim',
size=len(test_zim_bytes),
)
mock_downloader_download_file(test_zim_bytes)
await kiwix_download_manager.wait_for_all_downloads()
# Async download are hard to test, sleep to let tasks finish.

View file

@ -2,8 +2,7 @@ from http import HTTPStatus
from typing import List
from modules.zim import lib
from wrolpi.common import logger, aiohttp_get, get_download_info, download_file, background_task, get_html_soup, \
trim_file_name
from wrolpi.common import logger, aiohttp_get, background_task, get_html_soup
from wrolpi.downloader import Downloader, Download, DownloadResult
__all__ = ['KiwixCatalogDownloader', 'KiwixZimDownloader', 'kiwix_zim_downloader', 'kiwix_catalog_downloader']
@ -72,33 +71,14 @@ class KiwixZimDownloader(Downloader):
listable = False
async def do_download(self, download: Download) -> DownloadResult:
url = download.url
download_info = await get_download_info(url)
zim_directory = lib.get_zim_directory()
zim_directory.mkdir(parents=True, exist_ok=True)
name = trim_file_name(download_info.name)
output_path = zim_directory / name
if output_path.is_file():
output_size = output_path.stat().st_size
if download_info.size and download_info.size == output_size:
# File is already downloaded.
return DownloadResult(success=True)
logger.info(f'Downloading Zim {url} to {output_path} of size {download_info.size}')
await download_file(url, output_path=output_path, info=download_info)
output_path = await self.download_file(download.id, download.url, zim_directory)
# Notify the maintainer if outdated Zim files are lying around.
lib.flag_outdated_zim_files()
output_size = output_path.stat().st_size
if download_info.size and output_size != download_info.size:
return DownloadResult(
success=False,
error=f'Download size does not match: {output_size} != {download_info.size}')
return_code = await lib.check_zim(output_path)
if return_code == 127:
logger.warning(f'Not validating {output_path} because zimcheck is not installed')