-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.py
More file actions
48 lines (35 loc) · 1.24 KB
/
Copy pathapp.py
File metadata and controls
48 lines (35 loc) · 1.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
"""Boot script — initializes the DB, starts the cron, serves the dashboard.
Run with: python app.py
"""
from __future__ import annotations
import os
import threading
import webbrowser
from dotenv import load_dotenv
from flask import Flask
load_dotenv()
from monitor import db
from monitor.api import bp
from monitor.scheduler import start_scheduler
def create_app() -> Flask:
db.init_db()
# Backfill: convert any pre-existing 'May 30, 2023' / '1 week ago' style
# dates to ISO YYYY-MM-DD so SQL filtering and chart bucketing work.
fixed = db.normalize_existing_dates()
if fixed:
print(f"[startup] normalized {fixed} article date(s) to ISO format")
app = Flask(__name__)
app.register_blueprint(bp)
return app
def main() -> None:
app = create_app()
port = int(os.environ.get("PORT", "5000"))
# Background daily cron — only when running the dev server directly.
if not os.environ.get("WERKZEUG_RUN_MAIN"):
start_scheduler()
url = f"http://127.0.0.1:{port}/"
threading.Timer(1.2, lambda: webbrowser.open(url)).start()
print(f"\nGoogle News API Monitor — open {url}\n")
app.run(host="127.0.0.1", port=port, debug=False, use_reloader=False)
if __name__ == "__main__":
main()