erick.is/_unpublish.py

60 lines
1.2 KiB
Python
Raw Normal View History

import glob
import os
import sys
from datetime import datetime
import _utils
articles = glob.glob("_posts/*.gmi")
articles.sort(reverse=True)
titles = []
if len(articles) == 0:
print("No articles found")
sys.exit(1)
for article in articles:
fm = _utils.load(article)
titles.append(fm.metadata["title"])
print("\nAvailable articles:\n")
for index, title in enumerate(titles):
print(f"\t[{index + 1}] {title}")
print("")
article = ""
selection = ""
index = -1
while index < 0 or index > len(articles):
try:
index = int(selection) - 1
if index == -1:
sys.exit(0)
article = articles[index]
except (ValueError, IndexError):
selection = input("Enter a number (0 to exit): ")
fm = _utils.load(article)
file_date = datetime.fromisoformat(fm.metadata["date"]).strftime("%Y-%m-%d")
del fm.metadata["date"]
with open(
article.replace(f"_posts/{file_date}-", "_drafts/"),
mode="w",
encoding="utf8",
) as file:
file.write(_utils.dumps(fm))
try:
os.remove(article)
except PermissionError:
print(f"Permission denied to delete '{article}'.")
except Exception as e:
print(f"An error occurred: {e}")