frank0957 commited on
Commit
76b67a9
Β·
verified Β·
1 Parent(s): 4eec969

Update external_data_daemon.py

Browse files
Files changed (1) hide show
  1. external_data_daemon.py +6 -92
external_data_daemon.py CHANGED
@@ -1,9 +1,8 @@
1
  # external_data_daemon.py
2
  # Runs once every 24 hours.
3
- # 1. Fetches high‑impact economic events for today & tomorrow from
4
- # Financial Modeling Prep (free tier, requires FMP_API_KEY).
5
- # Saves the list to /app/economic_calendar_data.json for the dashboard.
6
- # Also creates wiki entries tagged "economic-calendar" (no notes.txt clutter).
7
  # 2. Resolves open flags:
8
  # a. Polymarket‑based – using /app/flag_mappings.json
9
  # b. Price‑based – using yfinance
@@ -15,10 +14,8 @@ import yfinance as yf
15
 
16
  FLAGS_FILE = "/app/flags.json"
17
  MAPPINGS_FILE = "/app/flag_mappings.json"
18
- ECON_LAST_RUN_FILE = "/app/econ_last_run.txt"
19
- ECON_DATA_FILE = "/app/economic_calendar_data.json"
20
 
21
- # ── Telegram helpers ─────────────────────────────────────────────
22
  TELEGRAM_TOKEN = os.getenv("TELEGRAM_BOT_TOKEN")
23
  TELEGRAM_CHAT_ID = os.getenv("TELEGRAM_CHAT_ID")
24
 
@@ -47,89 +44,6 @@ def write_json(path, data):
47
  with open(path, "w") as f:
48
  json.dump(data, f, indent=2)
49
 
50
- # ── Economic calendar (Financial Modeling Prep) ───────────────────
51
- FMP_API_KEY = os.getenv("FMP_API_KEY")
52
- FMP_CALENDAR_URL = "https://financialmodelingprep.com/api/v3/economic_calendar"
53
-
54
- def fetch_economic_events():
55
- """
56
- Fetch today's and tomorrow's economic events from FMP.
57
- Returns a list of dicts with keys: title, date, time, country, importance.
58
- """
59
- if not FMP_API_KEY:
60
- print("[ExternalData] FMP_API_KEY not set. Skipping economic calendar.", flush=True)
61
- return []
62
-
63
- today = datetime.now(timezone.utc)
64
- tomorrow = today + timedelta(days=1)
65
- date_from = today.strftime("%Y-%m-%d")
66
- date_to = tomorrow.strftime("%Y-%m-%d")
67
-
68
- params = {
69
- "apikey": FMP_API_KEY,
70
- "from": date_from,
71
- "to": date_to,
72
- }
73
- query_string = urllib.parse.urlencode(params)
74
- full_url = f"{FMP_CALENDAR_URL}?{query_string}"
75
-
76
- try:
77
- req = urllib.request.Request(full_url)
78
- with urllib.request.urlopen(req, timeout=20) as resp:
79
- data = json.loads(resp.read().decode())
80
- events = []
81
- if isinstance(data, list):
82
- for item in data:
83
- impact = item.get("impact", "") or "Low"
84
- events.append({
85
- "title": item.get("event", "Unknown event"),
86
- "date": item.get("date", ""),
87
- "time": item.get("time", ""),
88
- "country": item.get("country", ""),
89
- "importance": impact.capitalize() if impact else "Low"
90
- })
91
- return events
92
- except Exception as e:
93
- print(f"[ExternalData] FMP calendar fetch failed: {e}", flush=True)
94
- return []
95
-
96
- def create_economic_notes():
97
- last_run = ""
98
- try:
99
- if os.path.exists(ECON_LAST_RUN_FILE):
100
- with open(ECON_LAST_RUN_FILE, "r") as f:
101
- last_run = f.read().strip()
102
- except:
103
- pass
104
- today_str = datetime.now(timezone.utc).strftime("%Y-%m-%d")
105
- if last_run == today_str:
106
- print("[ExternalData] Economic notes already created today. Skipping.", flush=True)
107
- return
108
-
109
- events = fetch_economic_events()
110
- if not events:
111
- write_json(ECON_DATA_FILE, []) # empty β†’ dashboard shows "No events"
112
- print("[ExternalData] No economic events fetched today.", flush=True)
113
- return
114
-
115
- write_json(ECON_DATA_FILE, events)
116
- print(f"[ExternalData] Saved {len(events)} economic events for dashboard.", flush=True)
117
-
118
- # Wiki entries (tagged "economic-calendar")
119
- for ev in events:
120
- note_text = f"90% YES Economic event: {ev['title']} ({ev.get('country','')}) {ev['date']} {ev['time']}. {ev['importance']} impact."
121
- try:
122
- from llm_wiki import save_entry
123
- safe_title = ev['title'][:30].replace('/', '_').replace(' ', '_')
124
- wiki_content = f"# {ev['title']}\n\n**Time:** {datetime.now(timezone.utc).isoformat()}\n**Content:** {note_text}\n"
125
- save_entry(topic=f"Econ-{safe_title}", content=wiki_content, tags=["economic-calendar"])
126
- except:
127
- pass
128
-
129
- with open(ECON_LAST_RUN_FILE, "w") as f:
130
- f.write(today_str)
131
- print(f"[ExternalData] Created wiki entries for {len(events)} economic events.", flush=True)
132
-
133
  # ── Flag resolution (unchanged) ──────────────────────────────────
134
  POLYMARKET_API = "https://data-api.polymarket.com/markets"
135
 
@@ -320,10 +234,10 @@ def resolve_all_flags():
320
  print(f"[ExternalData] Flag resolution: {len(total_closed)} flags closed.", flush=True)
321
 
322
  def run():
323
- print("[ExternalData] Daemon started.", flush=True)
324
  while True:
325
  try:
326
- create_economic_notes()
327
  resolve_all_flags()
328
  except Exception as e:
329
  print(f"[ExternalData] Daily run failed: {e}", flush=True)
 
1
  # external_data_daemon.py
2
  # Runs once every 24 hours.
3
+ # 1. Economic calendar data is provided by the backup GitHub repo
4
+ # (the file /app/economic_calendar_data.json is pulled by the backup daemon).
5
+ # This daemon does NOT attempt any live fetch.
 
6
  # 2. Resolves open flags:
7
  # a. Polymarket‑based – using /app/flag_mappings.json
8
  # b. Price‑based – using yfinance
 
14
 
15
  FLAGS_FILE = "/app/flags.json"
16
  MAPPINGS_FILE = "/app/flag_mappings.json"
17
+ ECON_DATA_FILE = "/app/economic_calendar_data.json" # read‑only, updated by backup
 
18
 
 
19
  TELEGRAM_TOKEN = os.getenv("TELEGRAM_BOT_TOKEN")
20
  TELEGRAM_CHAT_ID = os.getenv("TELEGRAM_CHAT_ID")
21
 
 
44
  with open(path, "w") as f:
45
  json.dump(data, f, indent=2)
46
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
47
  # ── Flag resolution (unchanged) ──────────────────────────────────
48
  POLYMARKET_API = "https://data-api.polymarket.com/markets"
49
 
 
234
  print(f"[ExternalData] Flag resolution: {len(total_closed)} flags closed.", flush=True)
235
 
236
  def run():
237
+ print("[ExternalData] Daemon started (calendar provided by backup sync).", flush=True)
238
  while True:
239
  try:
240
+ # Calendar is read from /app/economic_calendar_data.json (pulled by backup)
241
  resolve_all_flags()
242
  except Exception as e:
243
  print(f"[ExternalData] Daily run failed: {e}", flush=True)