Compare commits
No commits in common. "6e49fadbabc1440047c26d6b69c5b2a687b5f550" and "73f419f1aab112a13e44dd04bfa1d3f5b8e42e6e" have entirely different histories.
6e49fadbab
...
73f419f1aa
59
app.py
59
app.py
@ -1,13 +1,11 @@
|
|||||||
from fastapi import FastAPI, Request, HTTPException
|
from fastapi import FastAPI, Request, HTTPException
|
||||||
from fastapi.responses import HTMLResponse, FileResponse, Response
|
from fastapi.responses import HTMLResponse
|
||||||
import markdown
|
import markdown
|
||||||
from fastapi.staticfiles import StaticFiles
|
from fastapi.staticfiles import StaticFiles
|
||||||
from fastapi.templating import Jinja2Templates
|
from fastapi.templating import Jinja2Templates
|
||||||
import json, yaml
|
import json, yaml
|
||||||
import os
|
import os
|
||||||
import uvicorn
|
import uvicorn
|
||||||
from datetime import datetime
|
|
||||||
from xml.etree import ElementTree as ET
|
|
||||||
|
|
||||||
app = FastAPI()
|
app = FastAPI()
|
||||||
templates = Jinja2Templates(directory="templates")
|
templates = Jinja2Templates(directory="templates")
|
||||||
@ -106,61 +104,6 @@ async def result(request: Request, scale_id: str):
|
|||||||
})
|
})
|
||||||
raise HTTPException(status_code=404, detail="问卷未找到")
|
raise HTTPException(status_code=404, detail="问卷未找到")
|
||||||
|
|
||||||
def generate_sitemap():
|
|
||||||
# Create the root element
|
|
||||||
urlset = ET.Element("urlset", xmlns="http://www.sitemaps.org/schemas/sitemap/0.9")
|
|
||||||
|
|
||||||
latest_mtime = max(
|
|
||||||
os.path.getmtime(os.path.join(root, f))
|
|
||||||
for root, _, files in os.walk('scales')
|
|
||||||
for f in files if f.endswith(('.yaml', '.yml'))
|
|
||||||
)
|
|
||||||
|
|
||||||
# Add static routes
|
|
||||||
static_routes = ["/"] # Add your static routes here
|
|
||||||
for route in static_routes:
|
|
||||||
url = ET.SubElement(urlset, "url")
|
|
||||||
ET.SubElement(url, "loc").text = f"https://psychoscales.org{route}"
|
|
||||||
ET.SubElement(url, "lastmod").text = datetime.fromtimestamp(latest_mtime).strftime("%Y-%m-%d")
|
|
||||||
ET.SubElement(url, "changefreq").text = "monthly"
|
|
||||||
ET.SubElement(url, "priority").text = "0.8"
|
|
||||||
|
|
||||||
# Add dynamic tag routes
|
|
||||||
tags, scales = load_all_scales()
|
|
||||||
for tag in tags:
|
|
||||||
url = ET.SubElement(urlset, "url")
|
|
||||||
ET.SubElement(url, "loc").text = f"https://psychoscales.org/tag/{tag}"
|
|
||||||
ET.SubElement(url, "lastmod").text = datetime.fromtimestamp(latest_mtime).strftime("%Y-%m-%d")
|
|
||||||
ET.SubElement(url, "changefreq").text = "weekly"
|
|
||||||
ET.SubElement(url, "priority").text = "0.6"
|
|
||||||
|
|
||||||
# Add dynamic scale routes
|
|
||||||
for scale_id in scales.keys():
|
|
||||||
url = ET.SubElement(urlset, "url")
|
|
||||||
ET.SubElement(url, "loc").text = f"https://psychoscales.org/scales/{scale_id}"
|
|
||||||
# For individual scale pages, use the actual file modification time
|
|
||||||
scale_file = os.path.join('scales', f"{scale_id}.yaml")
|
|
||||||
if os.path.exists(scale_file):
|
|
||||||
mtime = os.path.getmtime(scale_file)
|
|
||||||
ET.SubElement(url, "lastmod").text = datetime.fromtimestamp(mtime).strftime("%Y-%m-%d")
|
|
||||||
ET.SubElement(url, "changefreq").text = "monthly"
|
|
||||||
ET.SubElement(url, "priority").text = "0.6"
|
|
||||||
|
|
||||||
# Convert to string
|
|
||||||
return ET.tostring(urlset, encoding='unicode', method='xml')
|
|
||||||
|
|
||||||
# Mount all files from public directory to root
|
|
||||||
@app.get("/{filename}")
|
|
||||||
async def get_public_file(filename: str):
|
|
||||||
public_path = os.path.join("public", filename)
|
|
||||||
if filename == "sitemap.xml":
|
|
||||||
return Response(
|
|
||||||
content=generate_sitemap(),
|
|
||||||
media_type="application/xml"
|
|
||||||
)
|
|
||||||
if os.path.isfile(public_path):
|
|
||||||
return FileResponse(public_path)
|
|
||||||
raise HTTPException(status_code=404, detail="File not found")
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
uvicorn.run(app,host='0.0.0.0',port=8000)
|
uvicorn.run(app,host='0.0.0.0',port=8000)
|
@ -1,4 +0,0 @@
|
|||||||
User-agent: *
|
|
||||||
Allow: /
|
|
||||||
|
|
||||||
# Sitemap: /sitemap.xml
|
|
Binary file not shown.
Before Width: | Height: | Size: 200 KiB |
@ -8,7 +8,6 @@
|
|||||||
<meta name="keywords" content="心尺, PsychoScales, 心理学量表,专业心理测评,抑郁症测试,焦虑症自评,在线心理测评系统,心理咨询师工具, 心理测验, 报告解读, 测试题">
|
<meta name="keywords" content="心尺, PsychoScales, 心理学量表,专业心理测评,抑郁症测试,焦虑症自评,在线心理测评系统,心理咨询师工具, 心理测验, 报告解读, 测试题">
|
||||||
<meta name="baidu-site-verification" content="codeva-mPOBUr0rLS" />
|
<meta name="baidu-site-verification" content="codeva-mPOBUr0rLS" />
|
||||||
<link rel="stylesheet" href="/static/styles.css">
|
<link rel="stylesheet" href="/static/styles.css">
|
||||||
<link rel="icon" type="image/x-icon" href="/static/favicon.ico">
|
|
||||||
{% block head_extra %}{% endblock %}
|
{% block head_extra %}{% endblock %}
|
||||||
</head>
|
</head>
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user