bep40 commited on
Commit
4ffeb5a
·
verified ·
1 Parent(s): 580bd3b

Upload wall_24h_api.py

Browse files
Files changed (1) hide show
  1. wall_24h_api.py +40 -0
wall_24h_api.py ADDED
@@ -0,0 +1,40 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ MAX_AGE = 86400 # 24h seconds
9
+
10
+ def setup_wall_24h(app):
11
+ @app.get("/api/wall/24h")
12
+ def wall_24h():
13
+ """Get wall posts from last 24h"""
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
+ if not os.path.exists(WALL_FILE):
19
+ return JSONResponse({"posts": [], "count": 0})
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()
28
+ filtered = []
29
+ for p in posts:
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 <= MAX_AGE:
36
+ filtered.append(p)
37
+
38
+ return JSONResponse({"posts": filtered[:200], "count": len(filtered)})
39
+ except Exception as e:
40
+ return JSONResponse({"posts": [], "count": 0, "error": str(e)})