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