#!/usr/bin/python3

import cgi
import cgitb
import json 
from time import strftime, time

cgitb.enable()

param = False

try:
    form = cgi.FieldStorage(encoding='utf8')
    title = form.getvalue('title')
    content = form.getvalue('content')
    tags = form.getvalue('tags')[1:].split("#")
    param = True

    file_name = "posts/" + strftime("%Y-%m-%d-%H-%M-%S")
    nicetime = strftime("%A, %d.%B %Y %H:%M Uhr")
    file_content = {
        "title": title,
        "published": nicetime, 
        "content" : content,
        "tags" : tags
    }
    file_content = json.dumps(file_content)
    f = open(file_name, "w").write(file_content)
    print('Status: 303 See Other')
    print('Location: posts-show.py')
    print()
   
except:
    print("Content-type: text/html\n")
    if not(param):
        print("Nothing to see here")
    else:
        print("Error")
