eolas-db/src/services/parse_file_service.py

40 lines
1 KiB
Python
Raw Normal View History

2024-11-01 14:54:50 +00:00
import os
from datetime import datetime
from typing import List, TypedDict
from hurry.filesize import size
from services.parse_markdown_service import ParseMarkdownService
class Entry(TypedDict):
title: str
2024-11-01 16:11:16 +00:00
last_modified: str
size: str
2024-11-01 14:54:50 +00:00
tags: List[str]
2024-11-01 16:11:16 +00:00
links: List[str]
2024-11-01 14:54:50 +00:00
body: str
class ParseFileService:
def __init__(self, file):
self.eolas_file = file
self.info = os.stat(file)
self.parse_markdown_service = ParseMarkdownService(file)
def __get_title(self):
return os.path.basename(self.eolas_file)
def parse(self) -> Entry:
markdown_data = self.parse_markdown_service.parse()
return {
"title": self.__get_title(),
"last_modified": datetime.fromtimestamp(self.info.st_mtime).strftime(
"%Y-%m-%d %H:%M:%S"
),
"size": size(self.info.st_size),
"tags": markdown_data.get("tags", []),
2024-11-01 16:11:16 +00:00
"links": markdown_data.get("links", []),
2024-11-01 14:54:50 +00:00
"body": markdown_data.get("body", []),
}