Spaces:
Running
Running
Upload wall_24h_api.py
Browse files- wall_24h_api.py +9 -15
wall_24h_api.py
CHANGED
|
@@ -1,27 +1,21 @@
|
|
| 1 |
"""
|
| 2 |
-
Wall 24h API endpoint
|
| 3 |
"""
|
| 4 |
import os, json, time
|
| 5 |
from fastapi import Query
|
| 6 |
from fastapi.responses import JSONResponse
|
|
|
|
| 7 |
|
| 8 |
-
|
| 9 |
|
| 10 |
def setup_wall_24h(app):
|
|
|
|
| 11 |
@app.get("/api/wall/24h")
|
| 12 |
def wall_24h():
|
| 13 |
-
"""Get wall posts from last
|
| 14 |
-
DATA_DIR = '/data' if os.path.isdir('/data') else os.path.join(os.path.dirname(os.path.abspath(__file__)), 'data')
|
| 15 |
-
WALL_FILE = os.path.join(DATA_DIR, 'wall_posts.json')
|
| 16 |
-
|
| 17 |
try:
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
with open(WALL_FILE, 'r', encoding='utf-8') as f:
|
| 22 |
-
posts = json.load(f)
|
| 23 |
-
|
| 24 |
-
if not isinstance(posts, list):
|
| 25 |
return JSONResponse({"posts": [], "count": 0})
|
| 26 |
|
| 27 |
now = time.time()
|
|
@@ -30,9 +24,9 @@ def setup_wall_24h(app):
|
|
| 30 |
created = p.get('created', 0)
|
| 31 |
if created > 1000000000000: # ms timestamp
|
| 32 |
age = now - (created / 1000)
|
| 33 |
-
else:
|
| 34 |
age = now - created
|
| 35 |
-
if age <=
|
| 36 |
filtered.append(p)
|
| 37 |
|
| 38 |
return JSONResponse({"posts": filtered[:200], "count": len(filtered)})
|
|
|
|
| 1 |
"""
|
| 2 |
+
Wall 24h API endpoint - filter wall posts to last 24 hours
|
| 3 |
"""
|
| 4 |
import os, json, time
|
| 5 |
from fastapi import Query
|
| 6 |
from fastapi.responses import JSONResponse
|
| 7 |
+
from app_v2_entry import _load_wall_posts
|
| 8 |
|
| 9 |
+
MAX_AGE_SECONDS = 86400 # 24 hours
|
| 10 |
|
| 11 |
def setup_wall_24h(app):
|
| 12 |
+
# Add to existing FastAPI app
|
| 13 |
@app.get("/api/wall/24h")
|
| 14 |
def wall_24h():
|
| 15 |
+
"""Get wall posts from last 24 hours"""
|
|
|
|
|
|
|
|
|
|
| 16 |
try:
|
| 17 |
+
posts = _load_wall_posts()
|
| 18 |
+
if not posts or not isinstance(posts, list):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
return JSONResponse({"posts": [], "count": 0})
|
| 20 |
|
| 21 |
now = time.time()
|
|
|
|
| 24 |
created = p.get('created', 0)
|
| 25 |
if created > 1000000000000: # ms timestamp
|
| 26 |
age = now - (created / 1000)
|
| 27 |
+
else: # seconds timestamp
|
| 28 |
age = now - created
|
| 29 |
+
if age <= MAX_AGE_SECONDS:
|
| 30 |
filtered.append(p)
|
| 31 |
|
| 32 |
return JSONResponse({"posts": filtered[:200], "count": len(filtered)})
|