Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,6 +1,9 @@
|
|
| 1 |
import os
|
|
|
|
|
|
|
| 2 |
import httpx
|
| 3 |
from fastapi import FastAPI, HTTPException
|
|
|
|
| 4 |
from datetime import datetime, timedelta
|
| 5 |
from fastapi.middleware.cors import CORSMiddleware
|
| 6 |
from fastmcp.server import FastMCP
|
|
@@ -112,7 +115,8 @@ async def root():
|
|
| 112 |
"message": "GitHub Trending API is running",
|
| 113 |
"documentation": "/docs",
|
| 114 |
"mcp_endpoint": "/mcp",
|
| 115 |
-
"trending_endpoint": "/trending"
|
|
|
|
| 116 |
}
|
| 117 |
|
| 118 |
@app.get("/trending")
|
|
@@ -128,7 +132,6 @@ async def get_trending(num: int = 10):
|
|
| 128 |
"rate_limit_info": "Consider adding a GitHub token for higher rate limits"
|
| 129 |
}
|
| 130 |
|
| 131 |
-
|
| 132 |
@app.get("/trending/stream")
|
| 133 |
async def stream_trending(interval: int = 60, client_id: str = None):
|
| 134 |
"""增强版 SSE 端点"""
|
|
@@ -136,21 +139,19 @@ async def stream_trending(interval: int = 60, client_id: str = None):
|
|
| 136 |
try:
|
| 137 |
while True:
|
| 138 |
# 发送心跳
|
| 139 |
-
yield
|
| 140 |
|
| 141 |
# 获取数据
|
| 142 |
if not last_updated or (datetime.now() - last_updated) > timedelta(seconds=interval):
|
| 143 |
await fetch_github_trending()
|
| 144 |
|
| 145 |
-
|
| 146 |
-
"
|
| 147 |
-
"
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
"client_id": client_id,
|
| 151 |
-
"next_update": (datetime.now() + timedelta(seconds=interval)).isoformat()
|
| 152 |
-
})
|
| 153 |
}
|
|
|
|
| 154 |
|
| 155 |
await asyncio.sleep(interval)
|
| 156 |
|
|
@@ -159,13 +160,12 @@ async def stream_trending(interval: int = 60, client_id: str = None):
|
|
| 159 |
except Exception as e:
|
| 160 |
print(f"SSE 错误: {e}")
|
| 161 |
|
| 162 |
-
|
| 163 |
event_generator(),
|
| 164 |
media_type="text/event-stream",
|
| 165 |
headers={
|
| 166 |
"Cache-Control": "no-cache",
|
| 167 |
"Connection": "keep-alive",
|
| 168 |
-
"X-Accel-Buffering": "no"
|
| 169 |
}
|
| 170 |
-
)
|
| 171 |
-
return response
|
|
|
|
| 1 |
import os
|
| 2 |
+
import json
|
| 3 |
+
import asyncio
|
| 4 |
import httpx
|
| 5 |
from fastapi import FastAPI, HTTPException
|
| 6 |
+
from fastapi.responses import StreamingResponse
|
| 7 |
from datetime import datetime, timedelta
|
| 8 |
from fastapi.middleware.cors import CORSMiddleware
|
| 9 |
from fastmcp.server import FastMCP
|
|
|
|
| 115 |
"message": "GitHub Trending API is running",
|
| 116 |
"documentation": "/docs",
|
| 117 |
"mcp_endpoint": "/mcp",
|
| 118 |
+
"trending_endpoint": "/trending",
|
| 119 |
+
"sse_endpoint": "/trending/stream"
|
| 120 |
}
|
| 121 |
|
| 122 |
@app.get("/trending")
|
|
|
|
| 132 |
"rate_limit_info": "Consider adding a GitHub token for higher rate limits"
|
| 133 |
}
|
| 134 |
|
|
|
|
| 135 |
@app.get("/trending/stream")
|
| 136 |
async def stream_trending(interval: int = 60, client_id: str = None):
|
| 137 |
"""增强版 SSE 端点"""
|
|
|
|
| 139 |
try:
|
| 140 |
while True:
|
| 141 |
# 发送心跳
|
| 142 |
+
yield "event: heartbeat\ndata: \n\n"
|
| 143 |
|
| 144 |
# 获取数据
|
| 145 |
if not last_updated or (datetime.now() - last_updated) > timedelta(seconds=interval):
|
| 146 |
await fetch_github_trending()
|
| 147 |
|
| 148 |
+
data = {
|
| 149 |
+
"trending": cached_trending[:10],
|
| 150 |
+
"last_updated": last_updated.isoformat(),
|
| 151 |
+
"client_id": client_id,
|
| 152 |
+
"next_update": (datetime.now() + timedelta(seconds=interval)).isoformat()
|
|
|
|
|
|
|
|
|
|
| 153 |
}
|
| 154 |
+
yield f"event: update\ndata: {json.dumps(data)}\n\n"
|
| 155 |
|
| 156 |
await asyncio.sleep(interval)
|
| 157 |
|
|
|
|
| 160 |
except Exception as e:
|
| 161 |
print(f"SSE 错误: {e}")
|
| 162 |
|
| 163 |
+
return StreamingResponse(
|
| 164 |
event_generator(),
|
| 165 |
media_type="text/event-stream",
|
| 166 |
headers={
|
| 167 |
"Cache-Control": "no-cache",
|
| 168 |
"Connection": "keep-alive",
|
| 169 |
+
"X-Accel-Buffering": "no"
|
| 170 |
}
|
| 171 |
+
)
|
|
|