Zmiana datetime.now na path.getmtime dla komentarza

This commit is contained in:
Wiktor 2025-05-20 17:44:32 +02:00
parent feae847950
commit e28100335d

View File

@ -72,17 +72,17 @@ class UnsupportedTargetFormatError(Exception):
pass
def convert(data: dict, target_format: str) -> str:
def convert(data: dict, last_modified: datetime, target_format: str) -> str:
match target_format:
case "adguard":
return adguard_conversion(data)
return adguard_conversion(last_modified, data)
case _:
raise UnsupportedTargetFormatError
def adguard_conversion(data: dict) -> list[str]:
def adguard_conversion(last_modified: datetime, data: dict) -> list[str]:
header_lines: list[str] = [
f"! Blocking list automatically generated at {datetime.now(timezone.utc).strftime('%Y-%m-%d %H:%M:%S %Z%z')}",
f"! Blocking list automatically generated at {last_modified.strftime('%Y-%m-%d %H:%M:%S %Z%z')}",
"! Created with ❤️ by internet-czas-dzialac.pl",
]
@ -107,6 +107,11 @@ def dump_output(data: str, output_file: str) -> None:
file.write(data)
def get_last_modified_datetime(file_path: str) -> datetime:
timestamp: float = os.path.getmtime(file_path)
return datetime.fromtimestamp(timestamp, tz=timezone.utc)
def main() -> None:
# Start measuring time
start_time: float = perf_counter()
@ -117,6 +122,7 @@ def main() -> None:
# Load data
try:
data: dict = load_data(args.inputfile)
last_modified: datetime = get_last_modified_datetime(args.inputfile)
except FileNotFoundError:
logger.error(f"File {args.inputfile} not found!")
return
@ -129,7 +135,7 @@ def main() -> None:
# Convert
try:
output = convert(data, args.targetformat)
output = convert(data, last_modified, args.targetformat)
except UnsupportedTargetFormatError:
logger.error('Unsupported format. For now only "adguard" is supported.')
return