Update app.py
Browse files
app.py
CHANGED
|
@@ -1,26 +1,50 @@
|
|
| 1 |
from flask import Flask, request, jsonify
|
| 2 |
import os
|
| 3 |
from datetime import datetime
|
|
|
|
| 4 |
|
| 5 |
app = Flask(__name__)
|
| 6 |
BASE_DIR = "markdown_files"
|
| 7 |
os.makedirs(BASE_DIR, exist_ok=True)
|
| 8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
@app.route("/create-md", methods=["POST"])
|
| 10 |
def create_markdown():
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
|
| 19 |
-
# Create subfolder for tag
|
| 20 |
tag_dir = os.path.join(BASE_DIR, tag)
|
| 21 |
os.makedirs(tag_dir, exist_ok=True)
|
| 22 |
|
| 23 |
-
# Generate a timestamped filename
|
| 24 |
filename = f"{title.replace(' ', '_')}_{int(datetime.utcnow().timestamp())}.md"
|
| 25 |
filepath = os.path.join(tag_dir, filename)
|
| 26 |
|
|
|
|
| 1 |
from flask import Flask, request, jsonify
|
| 2 |
import os
|
| 3 |
from datetime import datetime
|
| 4 |
+
import json
|
| 5 |
|
| 6 |
app = Flask(__name__)
|
| 7 |
BASE_DIR = "markdown_files"
|
| 8 |
os.makedirs(BASE_DIR, exist_ok=True)
|
| 9 |
|
| 10 |
+
def extract_data(request):
|
| 11 |
+
# 1. Try JSON body
|
| 12 |
+
try:
|
| 13 |
+
return request.get_json(force=True)
|
| 14 |
+
except:
|
| 15 |
+
pass
|
| 16 |
+
|
| 17 |
+
# 2. Try request.args (query string or form data)
|
| 18 |
+
if "title" in request.args and "content" in request.args:
|
| 19 |
+
return {
|
| 20 |
+
"title": request.args.get("title"),
|
| 21 |
+
"content": request.args.get("content"),
|
| 22 |
+
"tag": request.args.get("tag", "untagged")
|
| 23 |
+
}
|
| 24 |
+
|
| 25 |
+
# 3. Try headers
|
| 26 |
+
if "X-Title" in request.headers and "X-Content" in request.headers:
|
| 27 |
+
return {
|
| 28 |
+
"title": request.headers.get("X-Title"),
|
| 29 |
+
"content": request.headers.get("X-Content"),
|
| 30 |
+
"tag": request.headers.get("X-Tag", "untagged")
|
| 31 |
+
}
|
| 32 |
+
|
| 33 |
+
return None
|
| 34 |
+
|
| 35 |
@app.route("/create-md", methods=["POST"])
|
| 36 |
def create_markdown():
|
| 37 |
+
data = extract_data(request)
|
| 38 |
+
if not data or "title" not in data or "content" not in data:
|
| 39 |
+
return jsonify({"error": "Missing 'title' or 'content' in body, query, or headers."}), 400
|
| 40 |
+
|
| 41 |
+
title = data["title"].strip()
|
| 42 |
+
content = data["content"]
|
| 43 |
+
tag = data.get("tag", "untagged").strip().replace(" ", "_")
|
| 44 |
|
|
|
|
| 45 |
tag_dir = os.path.join(BASE_DIR, tag)
|
| 46 |
os.makedirs(tag_dir, exist_ok=True)
|
| 47 |
|
|
|
|
| 48 |
filename = f"{title.replace(' ', '_')}_{int(datetime.utcnow().timestamp())}.md"
|
| 49 |
filepath = os.path.join(tag_dir, filename)
|
| 50 |
|