Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,9 +1,9 @@
|
|
| 1 |
import os
|
| 2 |
import time
|
| 3 |
import requests
|
| 4 |
-
from fastapi import FastAPI
|
| 5 |
-
from fastapi.responses import StreamingResponse
|
| 6 |
from datetime import datetime, timedelta
|
|
|
|
| 7 |
from fastmcp.server import FastMCP
|
| 8 |
from contextlib import asynccontextmanager
|
| 9 |
|
|
@@ -59,8 +59,13 @@ async def fetch_github_trending():
|
|
| 59 |
return cached_trending if cached_trending else []
|
| 60 |
|
| 61 |
# 定义MCP服务器和工具
|
|
|
|
| 62 |
mcp_server = FastMCP(name="GithubTrending")
|
| 63 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
@mcp_server.tool()
|
| 65 |
def get_trending_repos(num: int = 10) -> dict:
|
| 66 |
"""Tool to get top GitHub trending repositories with stars >1000"""
|
|
@@ -68,30 +73,16 @@ def get_trending_repos(num: int = 10) -> dict:
|
|
| 68 |
fetch_github_trending()
|
| 69 |
return {"trending": cached_trending[:num]}
|
| 70 |
|
| 71 |
-
app.mount("/mcp", mcp_server.http_app())
|
| 72 |
-
|
| 73 |
@app.get("/trending")
|
| 74 |
async def get_trending():
|
| 75 |
-
"""
|
|
|
|
| 76 |
if not cached_trending or (datetime.now() - last_updated) > timedelta(minutes=5):
|
| 77 |
await fetch_github_trending()
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
while True:
|
| 86 |
-
if not last_updated or (datetime.now() - last_updated) > timedelta(minutes=5):
|
| 87 |
-
await fetch_github_trending()
|
| 88 |
-
await websocket.send_json({
|
| 89 |
-
"trending": cached_trending,
|
| 90 |
-
"last_updated": last_updated.isoformat() if last_updated else None
|
| 91 |
-
})
|
| 92 |
-
await asyncio.sleep(30) # 使用 asyncio.sleep 而不是 time.sleep
|
| 93 |
-
except WebSocketDisconnect:
|
| 94 |
-
print("Client disconnected")
|
| 95 |
-
except Exception as e:
|
| 96 |
-
print(f"WebSocket error: {e}")
|
| 97 |
-
await websocket.close(code=1011)
|
|
|
|
| 1 |
import os
|
| 2 |
import time
|
| 3 |
import requests
|
| 4 |
+
from fastapi import FastAPI
|
|
|
|
| 5 |
from datetime import datetime, timedelta
|
| 6 |
+
from starlette.routing import Mount
|
| 7 |
from fastmcp.server import FastMCP
|
| 8 |
from contextlib import asynccontextmanager
|
| 9 |
|
|
|
|
| 59 |
return cached_trending if cached_trending else []
|
| 60 |
|
| 61 |
# 定义MCP服务器和工具
|
| 62 |
+
#mcp_server = FastMCP(name="GithubTrending", stateless_http=True)
|
| 63 |
mcp_server = FastMCP(name="GithubTrending")
|
| 64 |
|
| 65 |
+
app.mount("/mcp", mcp_server.http_app())
|
| 66 |
+
#mcp_app = mcp_server.http_app(path='/mcp')
|
| 67 |
+
#app.mount("/mcp", mcp_app)
|
| 68 |
+
|
| 69 |
@mcp_server.tool()
|
| 70 |
def get_trending_repos(num: int = 10) -> dict:
|
| 71 |
"""Tool to get top GitHub trending repositories with stars >1000"""
|
|
|
|
| 73 |
fetch_github_trending()
|
| 74 |
return {"trending": cached_trending[:num]}
|
| 75 |
|
|
|
|
|
|
|
| 76 |
@app.get("/trending")
|
| 77 |
async def get_trending():
|
| 78 |
+
"""获取GitHub热门项目"""
|
| 79 |
+
# 如果缓存为空或超过5分钟未更新,则重新获取
|
| 80 |
if not cached_trending or (datetime.now() - last_updated) > timedelta(minutes=5):
|
| 81 |
await fetch_github_trending()
|
| 82 |
+
|
| 83 |
+
return {
|
| 84 |
+
"trending": cached_trending,
|
| 85 |
+
"last_updated": last_updated.isoformat() if last_updated else None,
|
| 86 |
+
"cache_expires_in": 300, # 告诉客户端缓存有效期(秒)
|
| 87 |
+
"suggested_polling_interval": 60 # 建议客户端轮询间隔(秒)
|
| 88 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|