mirror of
https://github.com/bellingcat/auto-archiver.git
synced 2026-06-12 13:18:28 +03:00
Ruff format with defaults.
This commit is contained in:
@@ -16,20 +16,21 @@ def mkdir_if_not_exists(folder):
|
||||
|
||||
def expand_url(url):
|
||||
# expand short URL links
|
||||
if 'https://t.co/' in url:
|
||||
if "https://t.co/" in url:
|
||||
try:
|
||||
r = requests.get(url)
|
||||
logger.debug(f'Expanded url {url} to {r.url}')
|
||||
logger.debug(f"Expanded url {url} to {r.url}")
|
||||
return r.url
|
||||
except:
|
||||
logger.error(f'Failed to expand url {url}')
|
||||
logger.error(f"Failed to expand url {url}")
|
||||
return url
|
||||
|
||||
|
||||
def getattr_or(o: object, prop: str, default=None):
|
||||
try:
|
||||
res = getattr(o, prop)
|
||||
if res is None: raise
|
||||
if res is None:
|
||||
raise
|
||||
return res
|
||||
except:
|
||||
return default
|
||||
@@ -61,18 +62,19 @@ def random_str(length: int = 32) -> str:
|
||||
return str(uuid.uuid4()).replace("-", "")[:length]
|
||||
|
||||
|
||||
def calculate_file_hash(filename: str, hash_algo = hashlib.sha256, chunksize: int = 16000000) -> str:
|
||||
def calculate_file_hash(filename: str, hash_algo=hashlib.sha256, chunksize: int = 16000000) -> str:
|
||||
hash = hash_algo()
|
||||
with open(filename, "rb") as f:
|
||||
while True:
|
||||
buf = f.read(chunksize)
|
||||
if not buf: break
|
||||
if not buf:
|
||||
break
|
||||
hash.update(buf)
|
||||
return hash.hexdigest()
|
||||
|
||||
|
||||
def get_datetime_from_str(dt_str: str, fmt: str | None = None, dayfirst=True) -> datetime | None:
|
||||
""" parse a datetime string with option of passing a specific format
|
||||
"""parse a datetime string with option of passing a specific format
|
||||
|
||||
Args:
|
||||
dt_str: the datetime string to parse
|
||||
@@ -88,19 +90,24 @@ def get_datetime_from_str(dt_str: str, fmt: str | None = None, dayfirst=True) ->
|
||||
|
||||
|
||||
def get_timestamp(ts, utc=True, iso=True, dayfirst=True) -> str | datetime | None:
|
||||
""" Consistent parsing of timestamps.
|
||||
"""Consistent parsing of timestamps.
|
||||
Args:
|
||||
If utc=True, the timezone is set to UTC,
|
||||
if iso=True, the output is an iso string
|
||||
Use dayfirst to signify between date formats which put the date vs month first:
|
||||
e.g. DD/MM/YYYY vs MM/DD/YYYY
|
||||
"""
|
||||
if not ts: return
|
||||
"""
|
||||
if not ts:
|
||||
return
|
||||
try:
|
||||
if isinstance(ts, str): ts = parse_dt(ts, dayfirst=dayfirst)
|
||||
if isinstance(ts, (int, float)): ts = datetime.fromtimestamp(ts)
|
||||
if utc: ts = ts.replace(tzinfo=timezone.utc)
|
||||
if iso: return ts.isoformat()
|
||||
if isinstance(ts, str):
|
||||
ts = parse_dt(ts, dayfirst=dayfirst)
|
||||
if isinstance(ts, (int, float)):
|
||||
ts = datetime.fromtimestamp(ts)
|
||||
if utc:
|
||||
ts = ts.replace(tzinfo=timezone.utc)
|
||||
if iso:
|
||||
return ts.isoformat()
|
||||
return ts
|
||||
except Exception as e:
|
||||
logger.error(f"Unable to parse timestamp {ts}: {e}")
|
||||
|
||||
Reference in New Issue
Block a user