bep40 commited on
Commit
0e66c09
·
verified ·
1 Parent(s): 21efac9

Upload wall_24h_api.py

Browse files
Files changed (1) hide show
  1. wall_24h_api.py +16 -5
wall_24h_api.py CHANGED
@@ -2,14 +2,25 @@
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"""
@@ -24,7 +35,7 @@ def setup_wall_24h(app):
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)
 
2
  Wall 24h API endpoint - filter wall posts to last 24 hours
3
  """
4
  import os, json, time
 
5
  from fastapi.responses import JSONResponse
 
6
 
7
+ MAX_AGE_SECONDS = 86400 # 24 hours in seconds
8
+
9
+ # Try to import _load_wall_posts from app_v2_entry, fallback to own implementation
10
+ try:
11
+ from app_v2_entry import _load_wall_posts
12
+ except ImportError:
13
+ def _load_wall_posts():
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
+ try:
17
+ if os.path.exists(WALL_FILE):
18
+ with open(WALL_FILE, 'r', encoding='utf-8') as f:
19
+ return json.load(f)
20
+ except: pass
21
+ return []
22
 
23
  def setup_wall_24h(app):
 
24
  @app.get("/api/wall/24h")
25
  def wall_24h():
26
  """Get wall posts from last 24 hours"""
 
35
  created = p.get('created', 0)
36
  if created > 1000000000000: # ms timestamp
37
  age = now - (created / 1000)
38
+ else:
39
  age = now - created
40
  if age <= MAX_AGE_SECONDS:
41
  filtered.append(p)