2023-11-11 02:09:31 +00:00
|
|
|
import os
|
2023-11-12 21:09:21 +00:00
|
|
|
import vreader
|
|
|
|
|
|
|
|
from . import find_article
|
2023-11-12 21:04:31 +00:00
|
|
|
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"}
|
|
|
|
|
2023-11-12 21:04:31 +00:00
|
|
|
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")
|
2023-11-12 21:04:31 +00:00
|
|
|
date = datetime.strftime(datetime.utcnow(), "%Y%m%d%H%M%S")
|
2023-11-11 02:09:31 +00:00
|
|
|
|
|
|
|
# Derive Filename
|
2023-11-12 21:04:31 +00:00
|
|
|
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
|
2023-11-12 21:04:31 +00:00
|
|
|
file = open(filepath, 'w', encoding='utf-8')
|
2023-11-11 02:09:31 +00:00
|
|
|
file.write(content)
|
|
|
|
file.close()
|
|
|
|
|
|
|
|
return { "title": resp["title"] }
|