VReader/vreader/api/v1.py

52 lines
1.2 KiB
Python
Raw Permalink Normal View History

2023-11-11 02:09:31 +00:00
import os
2023-11-12 21:09:21 +00:00
import vreader
from . import find_article
from datetime import datetime
2023-11-11 02:09:31 +00:00
from flask import Blueprint, request
from vreader.config import Config
2023-11-11 21:41:25 +00:00
bp = Blueprint("v1", __name__, url_prefix="/api/v1")
2023-11-11 02:09:31 +00:00
@bp.route("/generate", methods=["POST"])
def generate():
data = request.get_json()
if not data:
return {"error": "Missing Data"}
video = str(data.get("video"))
if video == "":
return {"error": "Missing Data"}
if len(video) != 11:
return {"error": "Invalid VideoID"}
metadata = find_article(video)
2023-11-11 02:09:31 +00:00
if metadata is not None:
return {"video": video}
context = vreader.vman.transcribe_video(video)
if context is None:
return {"error": "Unable to Extract Subtitles"}
resp = vreader.oai.query(context)
# Get Details
directory = str(Config.DATA_PATH)
title = resp.get("title")
content = resp.get("content")
date = datetime.strftime(datetime.utcnow(), "%Y%m%d%H%M%S")
2023-11-11 02:09:31 +00:00
# Derive Filename
new_title = f"{date}_{video}_{title}"
2023-11-12 21:09:21 +00:00
filepath = os.path.join(directory, f"{new_title}.md")
2023-11-11 02:09:31 +00:00
# Write File
file = open(filepath, 'w', encoding='utf-8')
2023-11-11 02:09:31 +00:00
file.write(content)
file.close()
return { "title": resp["title"] }