diff --git a/lookyloo/capturecache.py b/lookyloo/capturecache.py index 96718f39..d66e5913 100644 --- a/lookyloo/capturecache.py +++ b/lookyloo/capturecache.py @@ -58,6 +58,12 @@ class CaptureCache(): self.logger = LookylooCacheLogAdapter(logger, {'uuid': self.uuid}) self.capture_dir: Path = Path(cache_entry['capture_dir']) + if not self.capture_dir.exists(): + raise MissingCaptureDirectory(f'The capture {self.uuid} does not exists in {self.capture_dir}.') + + if url := cache_entry.get('url'): + # This entry *should* be present even if there is an error. + self.url: str = url if all(key in cache_entry.keys() for key in __default_cache_keys): self.title: str = cache_entry['title'] @@ -66,14 +72,11 @@ class CaptureCache(): except ValueError: # If the microsecond is missing (0), it fails self.timestamp = datetime.strptime(cache_entry['timestamp'], '%Y-%m-%dT%H:%M:%S%z') - self.url: str = cache_entry['url'] if cache_entry.get('redirects'): self.redirects: List[str] = json.loads(cache_entry['redirects']) else: self.logger.debug('No redirects in cache') self.redirects = [] - if not self.capture_dir.exists(): - raise MissingCaptureDirectory(f'The capture {self.uuid} does not exists in {self.capture_dir}.') elif not cache_entry.get('error'): missing = set(__default_cache_keys) - set(cache_entry.keys()) raise LookylooException(f'Missing keys ({missing}), no error message. It should not happen.') diff --git a/lookyloo/lookyloo.py b/lookyloo/lookyloo.py index e3cdc7ce..91940857 100644 --- a/lookyloo/lookyloo.py +++ b/lookyloo/lookyloo.py @@ -247,13 +247,13 @@ class Lookyloo(): to_return = {'uuid': cache.uuid, 'url': cache.url if hasattr(cache, 'url') else 'Unable to get URL for the capture'} - if hasattr(cache, 'error'): + if hasattr(cache, 'error') and cache.error: to_return['error'] = cache.error if hasattr(cache, 'title'): to_return['title'] = cache.title if hasattr(cache, 'timestamp'): to_return['capture_time'] = cache.timestamp.isoformat() - if hasattr(cache, 'user_agent'): + if hasattr(cache, 'user_agent') and cache.user_agent: to_return['user_agent'] = cache.user_agent if hasattr(cache, 'referer'): to_return['referer'] = cache.referer if cache.referer else ''