VReader/vreader/api/v1.py

73 lines
1.7 KiB
Python
Raw Normal View History

2023-11-11 02:09:31 +00:00
import os
from datetime import datetime
2023-11-11 02:09:31 +00:00
from os import path
from flask import Blueprint, request
from vreader.config import Config
import vreader
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}"
filepath = 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"] }
def find_article(id):
2023-11-11 02:09:31 +00:00
directory = str(Config.DATA_PATH)
files = os.listdir(directory)
# Find Filename
filename = next((x for x in files if x[15:26] == id and x.endswith(".md")), None)
if filename is None:
return None
2023-11-11 02:09:31 +00:00
# Normalize File Info
return get_article_metadata(filename, directory)
2023-11-11 02:09:31 +00:00
def get_article_metadata(filename, directory):
2023-11-11 02:09:31 +00:00
return {
"date": filename[:14],
"video_id": filename[15:26],
"title": filename[27:][:-3],
"filepath": os.path.join(directory, filename)
2023-11-11 02:09:31 +00:00
}