mjestecko/tools/article_utils.py
veclav talica 0398828c01 init
2023-05-21 19:24:54 +05:00

42 lines
973 B
Python

import time, subprocess
MONTHS = {
1: "January",
2: "February",
3: "March",
4: "April",
5: "May",
6: "June",
7: "July",
8: "August",
9: "September",
10: "October",
11: "November",
12: "December"
}
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))
elif key == "Tags":
result["tags"] = val.split(",")
else:
result[key.lower()] = val
result["last_edit"] = time.gmtime(int(subprocess.getoutput(r"stat -c %Y " + filepath)))
return result