mjestecko/tools/article_utils.py

27 lines
764 B
Python
Raw Normal View History

2023-05-21 14:24:54 +00:00
import time, subprocess
def the_line_after_metadata(lines: []) -> int:
i = 0
while lines[i].strip():
i += 1
return i
def parse_metadata(filepath: str) -> {}:
result = {}
with open(filepath, "r") as f:
content = f.readlines()
i = the_line_after_metadata(content)
for line in content[:i]:
delim = line.find(":")
key, val = (line[:delim].strip(), line[delim+1:].strip())
if key == "Date":
result["Date"] = time.gmtime(int(val))
2023-05-21 14:24:54 +00:00
elif key == "Tags":
2024-02-16 13:29:50 +00:00
result["Tags"] = [v.strip() for v in val.split(",")]
2023-05-21 14:24:54 +00:00
else:
result[key] = val
result["Last Edit"] = time.gmtime(int(subprocess.getoutput(r"stat -c %Y " + filepath)))
2023-05-21 14:24:54 +00:00
return result