Update app.py
Browse files
app.py
CHANGED
@@ -2,19 +2,36 @@ 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"),
|
@@ -22,7 +39,6 @@ def extract_data(request):
|
|
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"),
|
@@ -34,32 +50,34 @@ def extract_data(request):
|
|
34 |
|
35 |
@app.route("/create-md", methods=["POST"])
|
36 |
def create_markdown():
|
37 |
-
|
38 |
-
|
39 |
-
|
|
|
40 |
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
|
45 |
-
|
46 |
-
|
47 |
|
48 |
-
|
49 |
-
|
50 |
|
51 |
-
try:
|
52 |
with open(filepath, "w", encoding="utf-8") as f:
|
53 |
f.write(f"# {title}\n\n{content}")
|
54 |
-
except Exception as e:
|
55 |
-
return jsonify({"error": f"Could not write file: {str(e)}"}), 500
|
56 |
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
|
|
|
|
|
|
|
|
63 |
|
64 |
if __name__ == "__main__":
|
65 |
app.run(host="0.0.0.0", port=7860)
|
|
|
2 |
import os
|
3 |
from datetime import datetime
|
4 |
import json
|
5 |
+
import traceback
|
6 |
|
7 |
app = Flask(__name__)
|
8 |
BASE_DIR = "markdown_files"
|
9 |
+
LOG_FILE = "error.log"
|
10 |
os.makedirs(BASE_DIR, exist_ok=True)
|
11 |
|
12 |
+
def log_error(req, error_msg):
|
13 |
+
try:
|
14 |
+
with open(LOG_FILE, "a", encoding="utf-8") as f:
|
15 |
+
f.write("\n--- Error Log ---\n")
|
16 |
+
f.write(f"Time: {datetime.utcnow().isoformat()} UTC\n")
|
17 |
+
f.write(f"Remote Addr: {request.remote_addr}\n")
|
18 |
+
f.write(f"Path: {request.path}\n")
|
19 |
+
f.write(f"Headers: {dict(req.headers)}\n")
|
20 |
+
f.write(f"Args: {dict(req.args)}\n")
|
21 |
+
f.write(f"Form: {dict(req.form)}\n")
|
22 |
+
f.write(f"Raw Data: {req.data.decode('utf-8', errors='ignore')}\n")
|
23 |
+
f.write(f"Error: {error_msg}\n")
|
24 |
+
f.write(f"Traceback:\n{traceback.format_exc()}\n")
|
25 |
+
f.write("------------------\n")
|
26 |
+
except Exception as e:
|
27 |
+
print("Logging failed:", e)
|
28 |
+
|
29 |
def extract_data(request):
|
|
|
30 |
try:
|
31 |
return request.get_json(force=True)
|
32 |
except:
|
33 |
pass
|
34 |
|
|
|
35 |
if "title" in request.args and "content" in request.args:
|
36 |
return {
|
37 |
"title": request.args.get("title"),
|
|
|
39 |
"tag": request.args.get("tag", "untagged")
|
40 |
}
|
41 |
|
|
|
42 |
if "X-Title" in request.headers and "X-Content" in request.headers:
|
43 |
return {
|
44 |
"title": request.headers.get("X-Title"),
|
|
|
50 |
|
51 |
@app.route("/create-md", methods=["POST"])
|
52 |
def create_markdown():
|
53 |
+
try:
|
54 |
+
data = extract_data(request)
|
55 |
+
if not data or "title" not in data or "content" not in data:
|
56 |
+
raise ValueError("Missing 'title' or 'content' in body, query, or headers.")
|
57 |
|
58 |
+
title = data["title"].strip()
|
59 |
+
content = data["content"]
|
60 |
+
tag = data.get("tag", "untagged").strip().replace(" ", "_")
|
61 |
|
62 |
+
tag_dir = os.path.join(BASE_DIR, tag)
|
63 |
+
os.makedirs(tag_dir, exist_ok=True)
|
64 |
|
65 |
+
filename = f"{title.replace(' ', '_')}_{int(datetime.utcnow().timestamp())}.md"
|
66 |
+
filepath = os.path.join(tag_dir, filename)
|
67 |
|
|
|
68 |
with open(filepath, "w", encoding="utf-8") as f:
|
69 |
f.write(f"# {title}\n\n{content}")
|
|
|
|
|
70 |
|
71 |
+
return jsonify({
|
72 |
+
"message": "Markdown file created",
|
73 |
+
"filename": filename,
|
74 |
+
"tag": tag,
|
75 |
+
"path": filepath
|
76 |
+
}), 201
|
77 |
+
|
78 |
+
except Exception as e:
|
79 |
+
log_error(request, str(e))
|
80 |
+
return jsonify({"error": str(e)}), 400
|
81 |
|
82 |
if __name__ == "__main__":
|
83 |
app.run(host="0.0.0.0", port=7860)
|