fix: Properly cache URL, fix mypy issue

Related https://github.com/Lookyloo/PyLookyloo/issues/32
pull/746/head
Raphaël Vinot 2023-07-24 13:15:10 +02:00
parent 158d52fb42
commit 8cd6f5490a
2 changed files with 8 additions and 5 deletions

View File

@ -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.')

View File

@ -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 ''