Commit 328ce5e3 authored by nanahira's avatar nanahira

readme and fix redis

parent c0fb33e2
Pipeline #36921 passed with stages
in 6 minutes and 16 seconds
# shadowban-revive
Rework of https://code.moenext.com/nanahira/shadowban-eu-backend
## Port
3000
## Environment
- `COOKIE` Twitter cookie.
- `COOKIE_FILE` Path to a file containing the Twitter cookie, one per line.
- `CHECK_COOKIE` If set, the script will check the cookie validity. It will exit after checking
\ No newline at end of file
...@@ -9,8 +9,8 @@ import random ...@@ -9,8 +9,8 @@ import random
import threading import threading
from typing import Any, Dict, List, Optional, Union from typing import Any, Dict, List, Optional, Union
import aioredis
import httpx import httpx
import redis
from fastapi import FastAPI, HTTPException from fastapi import FastAPI, HTTPException
from fastapi.middleware.cors import CORSMiddleware from fastapi.middleware.cors import CORSMiddleware
from pydantic import BaseModel, Field from pydantic import BaseModel, Field
...@@ -24,7 +24,7 @@ privateKey = os.getenv('TWITTER_AUTH_KEY') ...@@ -24,7 +24,7 @@ privateKey = os.getenv('TWITTER_AUTH_KEY')
if privateKey: if privateKey:
twitterKeys = privateKey.split(" ") twitterKeys = privateKey.split(" ")
cookies = [os.getenv('COOKIE')] cookies = os.getenv('COOKIE', "").split(",")
cookie_file = os.getenv('COOKIE_FILE') cookie_file = os.getenv('COOKIE_FILE')
if cookie_file: if cookie_file:
...@@ -73,15 +73,16 @@ _is_healthy: bool = True ...@@ -73,15 +73,16 @@ _is_healthy: bool = True
# Redis (same sync client—the API is blocking so FastAPI’s default thread‑pool # Redis (same sync client—the API is blocking so FastAPI’s default thread‑pool
# worker is fine.) # worker is fine.)
# --------------------------------------------------------------------------- # ---------------------------------------------------------------------------
redis_client: Optional[redis.Redis] = None redis_client: aioredis.Redis | None = None
if os.getenv("REDIS_HOST"): if os.getenv("REDIS_HOST"):
redis_pool = redis.ConnectionPool( redis_client = aioredis.Redis(
host=os.getenv("REDIS_HOST"), host=os.getenv("REDIS_HOST"),
port=int(os.getenv("REDIS_PORT", 6379)), port=int(os.getenv("REDIS_PORT", 6379)),
password=os.getenv("REDIS_PASSWORD", None), password=os.getenv("REDIS_PASSWORD", None),
db=int(os.getenv("REDIS_DB", 0)), db=int(os.getenv("REDIS_DB", 0)),
decode_responses=True, # 推荐加上,避免返回的是 bytes
) )
redis_client = redis.Redis(connection_pool=redis_pool)
# --------------------------------------------------------------------------- # ---------------------------------------------------------------------------
# FastAPI app & CORS # FastAPI app & CORS
...@@ -624,6 +625,7 @@ async def check_shadow_ban(screen_name: str): ...@@ -624,6 +625,7 @@ async def check_shadow_ban(screen_name: str):
if redis_client is not None: if redis_client is not None:
cached = await redis_client.get(f"shadowban:{screen_name}") cached = await redis_client.get(f"shadowban:{screen_name}")
if cached is not None: if cached is not None:
print(f"Cache hit for {screen_name} in Redis")
return json.loads(cached) return json.loads(cached)
try: try:
...@@ -632,8 +634,9 @@ async def check_shadow_ban(screen_name: str): ...@@ -632,8 +634,9 @@ async def check_shadow_ban(screen_name: str):
print("Unhandled exception:", exc) print("Unhandled exception:", exc)
raise HTTPException(status_code=500, detail="Internal error") from exc raise HTTPException(status_code=500, detail="Internal error") from exc
# Cache for 10 minutes if the run completed without failure flags # Cache for 10 mins if the run completed without failure flags
if redis_client and ['tests'] in result and not hasFailure: if redis_client and 'tests' in result and not hasFailure:
print(f"Caching result for {screen_name} in Redis")
await redis_client.set( await redis_client.set(
f"shadowban:{screen_name}", json.dumps(result), ex=600 f"shadowban:{screen_name}", json.dumps(result), ex=600
) )
......
aioredis==2.0.1
annotated-types==0.7.0
anyio==4.9.0
async-timeout==5.0.1
certifi==2025.4.26
click==8.2.1
exceptiongroup==1.3.0
fastapi==0.115.12
h11==0.16.0
httpcore==1.0.9
httpx==0.28.1
idna==3.10
pydantic==2.11.5
pydantic_core==2.33.2
redis==6.1.0
sniffio==1.3.1
starlette==0.46.2
typing-inspection==0.4.1
typing_extensions==4.13.2
uvicorn==0.34.2
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment