|
|
|
@ -295,5 +295,91 @@ async def render_html(request: fastapi.Request):
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@app.get("/api/sources")
|
|
|
|
|
async def get_sources(page: int = 1, limit: int = 10):
|
|
|
|
|
try:
|
|
|
|
|
pg_pool = await init_postgres_pool()
|
|
|
|
|
async with pg_pool.acquire() as conn:
|
|
|
|
|
# 获取总数
|
|
|
|
|
total = await conn.fetchval("SELECT COUNT(*) FROM t_wechat_source")
|
|
|
|
|
# 获取分页数据
|
|
|
|
|
offset = (page - 1) * limit
|
|
|
|
|
rows = await conn.fetch(
|
|
|
|
|
"""
|
|
|
|
|
SELECT id, account_id,account_name, created_at
|
|
|
|
|
FROM t_wechat_source
|
|
|
|
|
ORDER BY created_at DESC
|
|
|
|
|
LIMIT $1 OFFSET $2
|
|
|
|
|
""",
|
|
|
|
|
limit, offset
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
sources = [
|
|
|
|
|
{
|
|
|
|
|
"id": row[0],
|
|
|
|
|
"name": row[1],
|
|
|
|
|
"type": row[2],
|
|
|
|
|
"update_time": row[3].strftime("%Y-%m-%d %H:%M:%S") if row[3] else None
|
|
|
|
|
}
|
|
|
|
|
for row in rows
|
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
"code": 0,
|
|
|
|
|
"data": {
|
|
|
|
|
"list": sources,
|
|
|
|
|
"total": total,
|
|
|
|
|
"page": page,
|
|
|
|
|
"limit": limit
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
except Exception as e:
|
|
|
|
|
return {"code": 1, "msg": str(e)}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@app.get("/api/articles")
|
|
|
|
|
async def get_articles(page: int = 1, limit: int = 10):
|
|
|
|
|
try:
|
|
|
|
|
pg_pool = await init_postgres_pool()
|
|
|
|
|
async with pg_pool.acquire() as conn:
|
|
|
|
|
# 获取总数
|
|
|
|
|
total = await conn.fetchval("SELECT COUNT(*) FROM t_wechat_articles")
|
|
|
|
|
# 获取分页数据
|
|
|
|
|
offset = (page - 1) * limit
|
|
|
|
|
rows = await conn.fetch(
|
|
|
|
|
"""
|
|
|
|
|
SELECT a.id, a.title, a.source as source as name,
|
|
|
|
|
a.publish_time, a.collection_time
|
|
|
|
|
FROM t_wechat_articles a
|
|
|
|
|
ORDER BY a.collection_time DESC
|
|
|
|
|
LIMIT $1 OFFSET $2
|
|
|
|
|
""",
|
|
|
|
|
limit, offset
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
articles = [
|
|
|
|
|
{
|
|
|
|
|
"id": row[0],
|
|
|
|
|
"title": row[1],
|
|
|
|
|
"source": row[2],
|
|
|
|
|
"publish_date": row[3].strftime("%Y-%m-%d") if row[3] else None,
|
|
|
|
|
"collect_time": row[4].strftime("%Y-%m-%d %H:%M:%S") if row[4] else None
|
|
|
|
|
}
|
|
|
|
|
for row in rows
|
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
"code": 0,
|
|
|
|
|
"data": {
|
|
|
|
|
"list": articles,
|
|
|
|
|
"total": total,
|
|
|
|
|
"page": page,
|
|
|
|
|
"limit": limit
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
except Exception as e:
|
|
|
|
|
return {"code": 1, "msg": str(e)}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
|
uvicorn.run(app, host="0.0.0.0", port=8000)
|
|
|
|
|