Scrapes chat messages and current viewer count.
抖音直播间评论实时采集插件
server.py
```python
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from pydantic import BaseModel
from typing import List, Optional
import uvicorn
app = FastAPI()
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
class CommentSchema(BaseModel):
username: str
content: str
ts: Optional[int] = None
viewers: Optional[int] = 0 # New field for viewer count
@app.post("/api/receive_comments")
async def receive_comments(comments: List[CommentSchema]):
if not comments:
return {"status": "empty"}
# Just grab the viewer count from the first item since it's the same for the batch
current_viewers = comments[0].viewers
print(f"\n--- New Batch (Online: {current_viewers}) ---")
for item in comments:
print(f"[{item.username}]: {item.content}")
return {"status": "success", "count": len(comments)}
if __name__ == "__main__":
uvicorn.run(app, host="0.0.0.0", port=8000)
```