-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
339 lines (279 loc) · 11 KB
/
app.py
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
from flask import (
Flask,
render_template,
request,
jsonify,
redirect,
url_for,
send_from_directory,
)
import os
from datetime import datetime, timedelta
import pytz
from mutagen.mp3 import MP3
from mutagen.id3 import ID3
from mutagen.id3._frames import COMM
from mutagen.id3._frames import TYER
from mutagen.id3._frames import TCON
from mutagen.id3._frames import TALB
from mutagen.id3._frames import TPE1
from mutagen.id3._frames import TIT2
from recorder import (
load_config,
save_config,
get_next_7_days_schedule,
)
import psutil
import shutil
from recorder import load_config, get_next_7_days_schedule
import humanize
app = Flask(__name__)
config = load_config()
BASE_DIR = config.get("base_dir", os.path.dirname(os.path.abspath(__file__)))
OUTPUT_DIR = config.get("output_dir", os.path.join(BASE_DIR, "recordings"))
def get_recordings():
recordings_dir = OUTPUT_DIR
recordings = []
for file in os.listdir(recordings_dir):
if file.endswith(".mp3"):
recordings.append(file)
return sorted(recordings, reverse=True)
@app.route("/")
def index():
config = load_config()
shows = config["shows"]
# Add the config index to each show
for i, show in enumerate(shows):
show["config_index"] = i
# Get current time in Chicago (America/Chicago)
chicago_tz = pytz.timezone("America/Chicago")
now = datetime.now(chicago_tz)
# Function to get the next occurrence of a show
def get_next_occurrence(show):
show_time = datetime.strptime(show["time"], "%I:%M %p").time()
show_day = [
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday",
"Sunday",
].index(show["day"])
show_tz = pytz.timezone(show["timezone"])
# Convert show time to Chicago time
show_datetime = datetime.combine(now.date(), show_time)
show_datetime = show_tz.localize(show_datetime).astimezone(chicago_tz)
# Adjust the date to the next occurrence
days_ahead = show_day - now.weekday()
if days_ahead < 0 or (days_ahead == 0 and now.time() > show_datetime.time()):
days_ahead += 7
next_show = show_datetime + timedelta(days=days_ahead)
return next_show
# Sort shows based on their next occurrence
sorted_shows = sorted(shows, key=get_next_occurrence)
return render_template("index.html", shows=sorted_shows, current_time=now)
@app.route("/recordings/<path:filename>")
def serve_recording(filename):
recordings_dir = OUTPUT_DIR
return send_from_directory(recordings_dir, filename)
def get_mp3_tags(file_path):
audio = MP3(file_path, ID3=ID3)
tags = {}
if audio.tags:
tags["title"] = str(audio.tags.get("TIT2", [""])[0])
tags["artist"] = str(audio.tags.get("TPE1", [""])[0])
tags["album"] = str(audio.tags.get("TALB", [""])[0])
tags["genre"] = str(audio.tags.get("TCON", [""])[0])
tags["year"] = str(audio.tags.get("TYER", [""])[0])
tags["comment"] = str(audio.tags.get("COMM", [""])[0])
return tags
@app.route("/recordings")
def recordings():
recordings_dir = OUTPUT_DIR
recordings = []
for file in os.listdir(recordings_dir):
if file.endswith(".mp3"):
file_path = os.path.join(recordings_dir, file)
tags = get_mp3_tags(file_path)
date = datetime.fromtimestamp(os.path.getmtime(file_path)).strftime(
"%Y-%m-%d %H:%M:%S"
)
recordings.append({"filename": file, "tags": tags, "date": date})
# Sort recordings by date (newest first)
recordings.sort(key=lambda x: x["date"], reverse=True)
return render_template("recordings.html", recordings=recordings)
@app.route("/edit_tags/<path:filename>", methods=["GET", "POST"])
def edit_tags(filename):
recordings_dir = OUTPUT_DIR
file_path = os.path.join(recordings_dir, filename)
if request.method == "POST":
audio = MP3(file_path, ID3=ID3)
if audio.tags is None:
audio.tags = ID3()
audio.tags["TIT2"] = TIT2(encoding=3, text=request.form["title"])
audio.tags["TPE1"] = TPE1(encoding=3, text=request.form["artist"])
audio.tags["TALB"] = TALB(encoding=3, text=request.form["album"])
audio.tags["TCON"] = TCON(encoding=3, text=request.form["genre"])
audio.tags["TYER"] = TYER(encoding=3, text=request.form["year"])
audio.tags["COMM"] = COMM(
encoding=3, lang="eng", desc="comment", text=request.form["comment"]
)
audio.save()
return redirect(url_for("recordings"))
tags = get_mp3_tags(file_path)
return render_template("edit_tags.html", filename=filename, tags=tags)
@app.route("/delete_recording/<path:filename>", methods=["POST"])
def delete_recording(filename):
recordings_dir = OUTPUT_DIR
file_path = os.path.join(recordings_dir, filename)
if os.path.exists(file_path):
os.remove(file_path)
return jsonify({"success": True, "message": "Recording deleted successfully"})
else:
return jsonify({"success": False, "message": "Recording not found"}), 404
# Add a new route for exporting all recordings
@app.route("/export_recordings", methods=["GET"])
def export_recordings():
recordings_dir = OUTPUT_DIR
recordings = []
for file in os.listdir(recordings_dir):
if file.endswith(".mp3"):
file_path = os.path.join(recordings_dir, file)
tags = get_mp3_tags(file_path)
date = datetime.fromtimestamp(os.path.getmtime(file_path)).strftime(
"%Y-%m-%d %H:%M:%S"
)
recordings.append(
{
"filename": file,
"title": tags.get("title", ""),
"artist": tags.get("artist", ""),
"album": tags.get("album", ""),
"genre": tags.get("genre", ""),
"year": tags.get("year", ""),
"date": date,
}
)
return jsonify(recordings)
@app.route("/add_show", methods=["GET", "POST"])
def add_show():
if request.method == "POST":
config = load_config()
new_show = {
"name": request.form["name"],
"url": request.form["url"],
"day": request.form["day"],
"time": request.form["time"],
"timezone": request.form["timezone"],
"duration": int(request.form["duration"]),
"artist": request.form["artist"],
"album": request.form["album"],
"genre": request.form["genre"],
}
config["shows"].append(new_show)
save_config(config)
return redirect(url_for("index"))
return render_template("add_show.html")
@app.route("/edit_show/<int:index>", methods=["GET", "POST"])
def edit_show(index):
config = load_config()
if index < 0 or index >= len(config["shows"]):
return "Show not found", 404
show = config["shows"][index]
if request.method == "POST":
if "delete" in request.form:
del config["shows"][index]
save_config(config)
return redirect(url_for("index"))
else:
show["name"] = request.form["name"]
show["url"] = request.form["url"]
show["day"] = request.form["day"]
# Convert start and end times from HH:MM format to HH:MM AM/PM format
start_time = datetime.strptime(request.form["start_time"], "%H:%M")
end_time = datetime.strptime(request.form["end_time"], "%H:%M")
show["time"] = start_time.strftime("%I:%M %p")
# Calculate duration in seconds
duration = end_time - start_time
if duration.days < 0: # If end time is on the next day
duration += timedelta(days=1)
show["duration"] = int(duration.total_seconds())
show["timezone"] = request.form["timezone"]
show["artist"] = request.form["artist"]
show["album"] = request.form["album"]
show["genre"] = request.form["genre"]
save_config(config)
return redirect(url_for("index"))
# Convert time from HH:MM AM/PM format to HH:MM format for the form
time_obj = datetime.strptime(show["time"], "%I:%M %p")
show["start_time"] = time_obj.strftime("%H:%M")
# Calculate end time based on duration
end_time = time_obj + timedelta(seconds=show["duration"])
show["end_time"] = end_time.strftime("%H:%M")
return render_template("edit_show.html", show=show, index=index)
@app.route("/status")
def status():
config = load_config()
recordings_dir = OUTPUT_DIR
# Get disk usage
total, used, free = shutil.disk_usage(recordings_dir)
disk_usage = {
"total": f"{total // (2**30)} GB",
"used": f"{used // (2**30)} GB",
"free": f"{free // (2**30)} GB",
"percent": f"{used * 100 // total}%",
}
# Get CPU and memory usage
cpu_usage = f"{psutil.cpu_percent()}%"
memory = psutil.virtual_memory()
memory_usage = f"{memory.percent}%"
# Get the last recorded file
recordings = [f for f in os.listdir(recordings_dir) if f.endswith(".mp3")]
last_recording = (
max(recordings, key=lambda f: os.path.getmtime(os.path.join(recordings_dir, f)))
if recordings
else None
)
if last_recording:
last_recording_time = datetime.fromtimestamp(
os.path.getmtime(os.path.join(recordings_dir, last_recording))
)
last_recording_time = pytz.timezone("America/Chicago").localize(
last_recording_time
)
last_recording = f"{
last_recording} - {last_recording_time.strftime('%Y-%m-%d %H:%M:%S %Z')}"
# Get the next scheduled recording
schedule = get_next_7_days_schedule(config)
next_recording = None
next_recording_time = None
now = pytz.timezone("America/Chicago").localize(datetime.now())
for day, shows in schedule:
for show in shows:
show_time = pytz.timezone("America/Chicago").localize(
show["time"].replace(tzinfo=None)
)
if show_time > now:
next_recording_time = show_time
next_recording = f"{
show['name']} - {show_time.strftime('%Y-%m-%d %H:%M:%S %Z')}"
break
if next_recording:
break
# Calculate relative time for next recording
next_recording_relative = None
if next_recording_time:
next_recording_relative = humanize.naturaltime(next_recording_time, when=now)
status_info = {
"disk_usage": disk_usage,
"cpu_usage": cpu_usage,
"memory_usage": memory_usage,
"last_recording": last_recording or "No recordings yet",
"next_recording": next_recording or "No upcoming recordings",
"next_recording_relative": next_recording_relative or "N/A",
"total_recordings": len(recordings),
}
return render_template("status.html", status=status_info)
if __name__ == "__main__":
app.run(debug=False)