-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
149 lines (114 loc) · 4.62 KB
/
app.py
File metadata and controls
149 lines (114 loc) · 4.62 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
from flask import Flask, render_template, request, redirect, url_for, jsonify
import json
import os
from datetime import datetime
import subprocess
from collections import Counter
app = Flask(__name__)
# Load settings.json
SETTINGS_PATH = "settings.json"
with open(SETTINGS_PATH, "r") as f:
CONFIG = json.load(f)
STREAM_URLS = CONFIG["modules"]["stream_ear_bowr"].get("STREAM_URLS", [])
REPORTS_DIR = os.path.join(CONFIG["BASE_DIR"], CONFIG["REPORT_SUBDIR"])
SEED_INDEX_PATH = os.path.join(CONFIG["BASE_DIR"], "static", "seed_index.json")
# In-memory status tracker
status = {
"last_run": None,
"last_stream": None,
"last_result": None,
"last_keywords": []
}
@app.route("/chirp")
def chirp():
try:
with open("static/last_chirp.json", "r", encoding="utf-8") as f:
return jsonify(json.load(f))
except Exception as e:
return jsonify({"error": str(e)})
@app.route("/")
def index():
try:
with open(os.path.join(CONFIG["BASE_DIR"], "static", "status.json")) as f:
latest_status = json.load(f)
status.update(latest_status)
except:
pass
# Ensure keywords always reflect current config if not explicitly set
if not status.get("last_keywords"):
with open(SETTINGS_PATH, "r") as f:
cfg = json.load(f)
status["last_keywords"] = cfg["modules"]["stream_ear_bowr"].get("KEYWORDS", [])
return render_template("dashboard.html", stream_urls=STREAM_URLS, status=status)
@app.route("/trigger", methods=["POST"])
def trigger_listen():
stream_url = request.form.get("stream_url")
print(f"[Cockpit] Listening triggered for: {stream_url}")
# Update config with selected stream URL
CONFIG["modules"]["stream_ear_bowr"]["STREAM_URLS"] = [stream_url]
with open(SETTINGS_PATH, "w") as f:
json.dump(CONFIG, f, indent=2)
try:
subprocess.Popen(["python", "stream_ear_bowrv2.py"])
status["last_run"] = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
status["last_stream"] = stream_url
status["last_result"] = "Triggered via UI"
return redirect(url_for("index"))
except Exception as e:
print(f"Trigger error: {e}")
return "Error launching listener", 500
@app.route("/seeds")
def view_seeds():
status_filter = request.args.get("status") # from dropdown
seeds = []
if os.path.exists(SEED_INDEX_PATH):
with open(SEED_INDEX_PATH, "r", encoding="utf-8") as f:
raw_seeds = json.load(f)
# 🔽 Normalize all statuses first
for s in raw_seeds:
status = s.get("status", "unknown").lower()
s["status"] = status # update the record itself
# 🧮 Count statuses
status_counts = Counter(s["status"] for s in raw_seeds)
status_counts["all"] = len(raw_seeds)
print("🧪 Statuses found:", set(status_counts.keys()))
# 🔍 Filter seeds based on selected status
unique = {}
for s in raw_seeds:
ts = s.get("timestamp")
text_snippet = s.get("text", "")[:50]
if ts:
if not status_filter or s["status"] == status_filter:
key = (ts, text_snippet)
unique[key] = s
seeds = list(unique.values())
return render_template("seeds.html", seeds=seeds, current_status=status_filter, status_counts=status_counts)
@app.route("/status")
def get_status():
return jsonify(status)
@app.route("/add_keyword", methods=["POST"])
def add_keyword():
new_kw = request.form.get("new_keyword", "").strip().lower()
if not new_kw:
return redirect(url_for("index"))
with open(SETTINGS_PATH, "r") as f:
config = json.load(f)
keywords = config["modules"]["stream_ear_bowr"].get("KEYWORDS", [])
if new_kw not in keywords:
keywords.append(new_kw)
config["modules"]["stream_ear_bowr"]["KEYWORDS"] = keywords
with open(SETTINGS_PATH, "w") as f:
json.dump(config, f, indent=2)
print(f"[+] Keyword added to settings: {new_kw}")
status["last_keywords"] = keywords
return redirect(url_for("index"))
@app.route("/germinate")
def run_germinator():
try:
subprocess.run(["python", "germinator.py"], check=True)
status["last_result"] = "Germinator executed"
except Exception as e:
status["last_result"] = f"Germinator error: {e}"
return redirect(url_for("index"))
if __name__ == "__main__":
app.run(debug=True, port=5000)