2021-01-14 17:12:16 +01:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
from datetime import datetime
|
|
|
|
import json
|
|
|
|
from pathlib import Path
|
|
|
|
from typing import Any, Dict, List, Optional, Tuple
|
|
|
|
|
2021-01-14 17:28:59 +01:00
|
|
|
from .exceptions import LookylooException
|
|
|
|
|
2021-01-14 17:12:16 +01:00
|
|
|
|
|
|
|
class CaptureCache():
|
2021-06-16 00:16:56 +02:00
|
|
|
__slots__ = ('uuid', 'title', 'timestamp', 'url', 'redirects', 'capture_dir',
|
|
|
|
'error', 'incomplete_redirects', 'no_index', 'categories', 'parent')
|
2021-01-14 17:12:16 +01:00
|
|
|
|
|
|
|
def __init__(self, cache_entry: Dict[str, Any]):
|
2021-06-16 00:16:56 +02:00
|
|
|
__default_cache_keys: Tuple[str, str, str, str, str, str] = ('uuid', 'title', 'timestamp', 'url', 'redirects', 'capture_dir')
|
|
|
|
if all(key in cache_entry.keys() for key in __default_cache_keys):
|
2021-01-14 17:12:16 +01:00
|
|
|
self.uuid: str = cache_entry['uuid']
|
|
|
|
self.title: str = cache_entry['title']
|
|
|
|
self.timestamp: datetime = datetime.strptime(cache_entry['timestamp'], '%Y-%m-%dT%H:%M:%S.%f%z')
|
|
|
|
self.url: str = cache_entry['url']
|
|
|
|
self.redirects: List[str] = json.loads(cache_entry['redirects'])
|
2021-03-20 01:18:28 +01:00
|
|
|
self.capture_dir: Path = Path(cache_entry['capture_dir'])
|
2021-01-14 17:12:16 +01:00
|
|
|
elif not cache_entry.get('error'):
|
2021-06-16 00:16:56 +02:00
|
|
|
missing = set(__default_cache_keys) - set(cache_entry.keys())
|
2021-01-14 17:28:59 +01:00
|
|
|
raise LookylooException(f'Missing keys ({missing}), no error message. It should not happen.')
|
2021-01-14 17:12:16 +01:00
|
|
|
|
2021-01-14 17:28:59 +01:00
|
|
|
# Error without all the keys in __default_cache_keys was fatal.
|
|
|
|
# if the keys in __default_cache_keys are present, it was an HTTP error
|
2021-01-14 17:12:16 +01:00
|
|
|
self.error: Optional[str] = cache_entry.get('error')
|
2021-01-18 13:26:02 +01:00
|
|
|
self.incomplete_redirects: bool = True if cache_entry.get('incomplete_redirects') in [1, '1'] else False
|
|
|
|
self.no_index: bool = True if cache_entry.get('no_index') in [1, '1'] else False
|
2021-01-14 17:12:16 +01:00
|
|
|
self.categories: List[str] = json.loads(cache_entry['categories']) if cache_entry.get('categories') else []
|
2021-05-12 22:30:04 +02:00
|
|
|
self.parent: Optional[str] = cache_entry.get('parent')
|