Skip to content

Commit 1b660dd

Browse files
committed
feat: Allow for manual generation of videos
1 parent 2c7cc9b commit 1b660dd

File tree

2 files changed

+81
-0
lines changed

2 files changed

+81
-0
lines changed

templates/video_list.html

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,38 @@
44
{% block type_name_singular %}movie{% endblock %}
55

66
{% block table_listing %}
7+
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
8+
<script>
9+
function runProcess() {
10+
// Disable the button while processing
11+
$('#run-btn').prop('disabled', true);
12+
$('#status').text('Processing...');
13+
14+
// Start the process
15+
$.post('/thegateway/generate', function(data) {
16+
if (data.in_progress) {
17+
alert("Generation is already in progress by another user.");
18+
} else {
19+
// Do the check
20+
checkStatus();
21+
}
22+
});
23+
}
24+
25+
function checkStatus() {
26+
$.get('/thegateway/check_status', function(data) {
27+
if (data.completed) {
28+
$('#status').text(data.message);
29+
alert(data.message); // Show popup
30+
$('#run-btn').prop('disabled', false);
31+
} else {
32+
$('#status').text('Processing... ' + data.message);
33+
// Check again after a delay
34+
setTimeout(checkStatus, 1000);
35+
}
36+
});
37+
}
38+
</script>
739
<br>
840
<a href="{{ url_for('thegateway.admin') }}">Return to the Main Menu</a>
941
<table class="table is-fullwidth is-hoverable is-striped">
@@ -79,4 +111,8 @@
79111
<span>Add a Video</span>
80112
</button>
81113
</a>
114+
115+
<button id="run-btn" class="button is-info" onclick="runProcess()">
116+
<span>Generate Videos</span>
117+
</button>
82118
{% endblock %}

thegateway/videos.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from time import sleep
2+
13
from models import db, Videos
24
from flask import (
35
url_for,
@@ -6,12 +8,22 @@
68
redirect,
79
request,
810
send_from_directory,
11+
jsonify,
912
)
1013
from thegateway import thegateway_blueprint
1114
from thegateway.mobiclip import validate_mobiclip, save_video_data, get_mobiclip_length
1215
from thegateway.form import VideoForm
1316
from thegateway.admin import oidc
1417
from werkzeug.utils import redirect
18+
import threading
19+
import subprocess
20+
21+
22+
generate_status = {
23+
"completed": False,
24+
'message': "",
25+
"in_progress": False,
26+
}
1527

1628

1729
@thegateway_blueprint.route("/thegateway/videos/")
@@ -77,3 +89,36 @@ def add_video():
7789
@oidc.require_login
7890
def get_video_thumbnail(movie_id):
7991
return send_from_directory("./assets/videos/", f"{movie_id}.img")
92+
93+
94+
@thegateway_blueprint.post("/thegateway/generate")
95+
@oidc.require_login
96+
def generate_videos():
97+
def actually_generate_videos():
98+
message = "Successfully generated thumbnails and videos!"
99+
# Sleep for a second to give the web app time to process the fact that another user isn't generating.
100+
sleep(1)
101+
generate_status["in_progress"] = True
102+
103+
# Generate videos first
104+
if subprocess.run(["./cli", "2"]).returncode != 0:
105+
message = "Error generating videos."
106+
else:
107+
# Now thumbnails
108+
if subprocess.run(["./cli", "3"]).returncode != 0:
109+
message = "Error generating thumbnails."
110+
111+
generate_status["completed"] = True
112+
generate_status["message"] = message
113+
generate_status["in_progress"] = False
114+
115+
if not generate_status["in_progress"]:
116+
threading.Thread(target=actually_generate_videos, daemon=True).start()
117+
118+
return jsonify(generate_status)
119+
120+
121+
@thegateway_blueprint.route("/thegateway/check_status")
122+
def check_status():
123+
"""Endpoint to check the current process status"""
124+
return jsonify(generate_status)

0 commit comments

Comments
 (0)