66 lines
1.7 KiB
Python
66 lines
1.7 KiB
Python
from flask import Flask, render_template, request, jsonify
|
|
from threading import Thread
|
|
import time
|
|
from yt_dlp import YoutubeDL
|
|
from yt_dlp.utils import DownloadError
|
|
import pathlib
|
|
import os
|
|
import shutil
|
|
|
|
|
|
def rip_worker(rip_queue: list[str]):
|
|
project_root_path = pathlib.Path(__file__).parent.resolve()
|
|
tmp_path = f"{project_root_path}/tmp"
|
|
|
|
while True:
|
|
queue_is_empty = len(rip_queue) == 0
|
|
if queue_is_empty:
|
|
continue
|
|
|
|
# Creates tmp directory, if it already exists, it clears it, and makes a new one.
|
|
if not os.path.isdir(tmp_path):
|
|
os.mkdir(tmp_path)
|
|
else:
|
|
shutil.rmtree(tmp_path)
|
|
os.mkdir(tmp_path)
|
|
|
|
current_url = rip_queue[0]
|
|
|
|
try:
|
|
with YoutubeDL(
|
|
{
|
|
"format": "m4a/bestaudio/best",
|
|
"postprocessors": [
|
|
{
|
|
"key": "FFmpegExtractAudio",
|
|
"preferredcodec": "m4a",
|
|
}
|
|
],
|
|
"outtmpl": f"{tmp_path}/%(title)s.%(ext)s",
|
|
}
|
|
) as ydl:
|
|
error_code = ydl.download([current_url])
|
|
|
|
except DownloadError:
|
|
print("Download Error")
|
|
|
|
rip_queue.pop(0)
|
|
time.sleep(1)
|
|
|
|
|
|
app = Flask(__name__)
|
|
rip_queue = []
|
|
ripping_thread = Thread(target=rip_worker, args=(rip_queue,), daemon=True).start()
|
|
|
|
|
|
@app.route("/")
|
|
def index():
|
|
return render_template("index.html")
|
|
|
|
|
|
@app.route("/rip", methods=["POST"])
|
|
def rip():
|
|
url = request.form.get("url")
|
|
rip_queue.append(url)
|
|
return jsonify({"success": True, "message": f"Added, {url}!"})
|