diff --git a/modules/archive/__init__.py b/modules/archive/__init__.py index fdd57852..b97fa403 100644 --- a/modules/archive/__init__.py +++ b/modules/archive/__init__.py @@ -16,7 +16,7 @@ from wrolpi.db import optional_session, get_db_session from wrolpi.downloader import Downloader, Download, DownloadResult from wrolpi.errors import UnrecoverableDownloadError from wrolpi.files.models import FileGroup -from wrolpi.vars import PYTEST, DOCKERIZED +from wrolpi.vars import PYTEST, DOCKERIZED, USER_AGENT from . import lib from .api import archive_bp # noqa from .errors import InvalidArchive @@ -72,7 +72,9 @@ class ArchiveDownloader(Downloader, ABC): download.url, '--browser-executable-path', CHROMIUM, '--browser-args', '["--no-sandbox"]', - '--dump-content') + '--dump-content', + '--user-agent', USER_AGENT, + ) return_code, logs, stdout = await self.process_runner( download.id, download.url, @@ -82,6 +84,15 @@ class ArchiveDownloader(Downloader, ABC): if return_code != 0: raise RuntimeError(f'Archive singlefile exited with {return_code}') + if not stdout or SINGLEFILE_HEADER.encode() not in stdout[:1000]: + if logs and (stderr := logs.get('stderr')): + e = ChildProcessError(stderr.decode()) + raise RuntimeError(f'Singlefile created was invalid: {download.url}') from e + if stdout: + e = ChildProcessError(stdout.decode()) + raise RuntimeError(f'Singlefile created was invalid: {download.url}') from e + raise RuntimeError(f'Singlefile created was invalid: {download.url}') + return stdout, logs async def do_readability(self, download: Download, html: bytes) -> dict: @@ -98,12 +109,22 @@ class ArchiveDownloader(Downloader, ABC): pathlib.Path('/home/wrolpi'), ) if return_code == 0: - readability = json.loads(stdout) + try: + readability = json.loads(stdout) + except TypeError: + # JSON was invalid. + e = ChildProcessError(stdout.decode() if stdout else 'No stdout') + if logs and (stderr := logs.get('stderr')): + e = ChildProcessError(stderr.decode()) + raise RuntimeError(f'Failed to extract readability from {download.url}') from e logger.debug(f'done readability for {download.url}') return readability else: logger.error(f'Failed to extract readability for {download.url}') - raise RuntimeError(f'Failed to extract readability for {download.url}') + e = ChildProcessError(stdout.decode() if stdout else 'No stdout') + if logs and (stderr := logs.get('stderr')): + e = ChildProcessError(stderr.decode()) + raise RuntimeError(f'Failed to extract readability for {download.url} got {return_code}') from e @staticmethod async def do_screenshot_url(download: Download) -> bytes: @@ -129,15 +150,6 @@ class ArchiveDownloader(Downloader, ABC): """ singlefile, logs = await self.do_singlefile(download) - if SINGLEFILE_HEADER.encode() not in singlefile[:1000]: - if logs and (stderr := logs.get('stderr') or ''): - e = RuntimeError(stderr) - raise RuntimeError(f'Singlefile created was invalid: {download.url}') from e - if singlefile: - e = RuntimeError(singlefile) - raise RuntimeError(f'Singlefile created was invalid: {download.url}') from e - raise RuntimeError(f'Singlefile created was invalid: {download.url}') - # Extract Readability from the Singlefile. try: readability = await self.do_readability(download, singlefile) diff --git a/wrolpi/vars.py b/wrolpi/vars.py index 261a3cf7..3e7a686e 100644 --- a/wrolpi/vars.py +++ b/wrolpi/vars.py @@ -54,7 +54,6 @@ MEDIA_DIRECTORY = Path(os.environ.get('MEDIA_DIRECTORY', DEFAULT_MEDIA_DIRECTORY CONFIG_DIR: Path = MEDIA_DIRECTORY / 'config' MODULES_DIR: Path = PROJECT_DIR / 'modules' -DEFAULT_TIMEZONE_STR = 'America/Boise' DATE_FORMAT = '%Y-%M-%d' DATETIME_FORMAT = '%Y-%m-%d %H:%M:%S' DATETIME_FORMAT_MS = '%Y-%m-%d %H:%M:%S.%f' @@ -82,8 +81,10 @@ FILE_MAX_TEXT_SIZE = int(os.environ.get('FILE_MAX_TEXT_SIZE', 100_000)) VIDEO_COMMENTS_FETCH_COUNT = int(os.environ.get('VIDEO_COMMENTS_FETCH_COUNT', 80)) YTDLP_CACHE_DIR = os.environ.get('YTDLP_CACHE_DIR', '/tmp/ytdlp_cache') +USER_AGENT = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/125.0.0.0 Safari/537.366' + DEFAULT_HTTP_HEADERS = { - 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116 Safari/537.36', + 'User-Agent': USER_AGENT, } RPI_DEVICE_PATH = pathlib.Path('/proc/device-tree/model') @@ -92,4 +93,5 @@ IS_RPI4 = 'Raspberry Pi 4' in RPI_DEVICE_MODEL_CONTENTS if RPI_DEVICE_MODEL_CONT IS_RPI5 = 'Raspberry Pi 5' in RPI_DEVICE_MODEL_CONTENTS if RPI_DEVICE_MODEL_CONTENTS else False IS_RPI = IS_RPI4 or IS_RPI5 -SIMULTANEOUS_DOWNLOAD_DOMAINS = int(os.environ.get('SIMULTANEOUS_DOWNLOAD_DOMAINS', 4)) +SIMULTANEOUS_DOWNLOAD_DOMAINS = int(os.environ.get('SIMULTANEOUS_DOWNLOAD_DOMAINS', + 2 if IS_RPI else 4))