From 6f21dad393bdd8524e9a291c0debab60bf8bcc21 Mon Sep 17 00:00:00 2001 From: Evan Reichard Date: Sun, 12 Nov 2023 16:09:21 -0500 Subject: [PATCH] [fix] slight refactor --- MANIFEST.in | 2 +- vreader/api/__init__.py | 23 +++++++++++++++++++++++ vreader/api/common.py | 26 +++----------------------- vreader/api/v1.py | 29 ++++------------------------- 4 files changed, 31 insertions(+), 49 deletions(-) create mode 100644 vreader/api/__init__.py diff --git a/MANIFEST.in b/MANIFEST.in index 12c7925..55de4ad 100644 --- a/MANIFEST.in +++ b/MANIFEST.in @@ -1,3 +1,3 @@ -recursive-include vreader/api *.py recursive-include vreader/templates * recursive-include vreader/static * +recursive-include vreader/api * diff --git a/vreader/api/__init__.py b/vreader/api/__init__.py new file mode 100644 index 0000000..1e2c9cc --- /dev/null +++ b/vreader/api/__init__.py @@ -0,0 +1,23 @@ +import os +from vreader.config import Config + +def find_article(id): + 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 + + # Normalize File Info + return get_article_metadata(filename, directory) + + +def get_article_metadata(filename, directory): + return { + "date": filename[:14], + "video_id": filename[15:26], + "title": filename[27:][:-3], + "filepath": os.path.join(directory, filename) + } diff --git a/vreader/api/common.py b/vreader/api/common.py index 9ba438a..c2bc23f 100644 --- a/vreader/api/common.py +++ b/vreader/api/common.py @@ -1,9 +1,11 @@ +import os + +from . import get_article_metadata, find_article from flask import Blueprint from flask import make_response, render_template, send_from_directory from html_sanitizer import Sanitizer from markdown import markdown from vreader.config import Config -import os bp = Blueprint("common", __name__) @@ -63,25 +65,3 @@ def article_item(id): return make_response( render_template("error.html", status=404, message=e) ), 404 - - -def find_article(id): - 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 - - # Normalize File Info - return get_article_metadata(filename, directory) - - -def get_article_metadata(filename, directory): - return { - "date": filename[:14], - "video_id": filename[15:26], - "title": filename[27:][:-3], - "filepath": os.path.join(directory, filename) - } diff --git a/vreader/api/v1.py b/vreader/api/v1.py index 9de733d..2f62568 100644 --- a/vreader/api/v1.py +++ b/vreader/api/v1.py @@ -1,9 +1,10 @@ import os +import vreader + +from . import find_article from datetime import datetime -from os import path from flask import Blueprint, request from vreader.config import Config -import vreader bp = Blueprint("v1", __name__, url_prefix="/api/v1") @@ -40,7 +41,7 @@ def generate(): # Derive Filename new_title = f"{date}_{video}_{title}" - filepath = path.join(directory, f"{new_title}.md") + filepath = os.path.join(directory, f"{new_title}.md") # Write File file = open(filepath, 'w', encoding='utf-8') @@ -48,25 +49,3 @@ def generate(): file.close() return { "title": resp["title"] } - - -def find_article(id): - 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 - - # Normalize File Info - return get_article_metadata(filename, directory) - - -def get_article_metadata(filename, directory): - return { - "date": filename[:14], - "video_id": filename[15:26], - "title": filename[27:][:-3], - "filepath": os.path.join(directory, filename) - }