Files
screw-bardo/app.py
2024-10-15 14:26:59 -04:00

64 lines
2.4 KiB
Python

from flask import Flask, render_template, Response, request
from main import get_auto_transcript, get_video_id, create_and_stream, output_stream, fake_stream, awaiter
from asyncio import sleep
from datetime import datetime
import threading
import pytz
import time
app = Flask(__name__, static_folder="website/static", template_folder="website")
@app.route('/')
def home():
return render_template('index.html')
@app.route('/process_url', methods=['POST'])
def process_url():
# Opens a file to log the video id and the assistants respone to see if I can further improve instructions:
#log = open("log.txt", "at", 1)
url = request.form['url']
if url == "test":
global thread
thread = threading.Thread(name="test_thread", target=fake_stream)
return Response("teehee", status=200)
# Extract the video ID from the URL
video_id = get_video_id(url) # Modify this function to accept the URL
if not video_id:
return "Couldn't parse video ID from URL. (Are you sure you entered a valid YouTube.com or YouTu.be URL?)"
# Get the transcript for that video ID
transcript = get_auto_transcript(video_id)
if (not transcript):
return "Successfully parsed video ID from URL, however the ID was either invalid, the transcript was disabled by the video owner, or some other error was raised because of YouTube."
thread = threading.Thread(name="create_stream", target=create_and_stream, args=(transcript,)) # The comma here is very intentional, it's so that it iterates it as a tuple rather than iterateing the string.
return Response("Processing started. Check /stream_output for updates.", status=200) # Add more detailed output if needed
@app.route('/stream_output')
def stream_output():
def yoink():
print("Starting stream thread.")
thread.start()
# Start streaming output from output_stream
print("Starting to stream output...")
most_recent = ""
while not output_stream.done:
if output_stream.buffer != []:
delta = output_stream.buffer.pop(0)
yield bytes(delta, encoding="utf-8")
else:
awaiter(sleep(0.05))
output_stream.reset()
thread.join()
return
return Response(yoink(), content_type='text/plain', status=200)
if __name__ == '__main__':
app.run(debug=True)