From 6e844a3cb185d1a433d458a88ca7e51b2a5cd3b8 Mon Sep 17 00:00:00 2001 From: tiyn Date: Mon, 22 Apr 2024 02:06:12 +0200 Subject: [PATCH] added tts functionality --- Dockerfile | 18 ++++++++------ README.md | 1 + src/app.py | 1 + src/content.py | 54 ++++++++++++++++++++++++++++++++++++++++ src/requirements.txt | 1 + src/static/css/style.css | 6 +++++ src/static/tmp/.gitkeep | 0 7 files changed, 73 insertions(+), 8 deletions(-) create mode 100644 src/static/tmp/.gitkeep diff --git a/Dockerfile b/Dockerfile index 1dd726f..37d0de1 100644 --- a/Dockerfile +++ b/Dockerfile @@ -2,11 +2,9 @@ FROM python:3 MAINTAINER tiyn tiyn@mail-mk.eu -COPY src /blog - -WORKDIR /blog - -RUN pip3 install -r requirements.txt +ENV LANG en_US.UTF-8 +ENV LANGUAGE en_US:en +ENV LC_ALL en_US.UTF-8 RUN apt-get update && \ apt-get install -y locales && \ @@ -14,9 +12,13 @@ RUN apt-get update && \ sed -i '/en_US.UTF-8/s/^# //g' /etc/locale.gen && \ dpkg-reconfigure --frontend=noninteractive locales -ENV LANG en_US.UTF-8 -ENV LANGUAGE en_US:en -ENV LC_ALL en_US.UTF-8 +RUN apt-get install -y espeak + +COPY src /blog + +WORKDIR /blog + +RUN pip3 install -r requirements.txt VOLUME /blog/templates/entry diff --git a/README.md b/README.md index 5ccc114..9e0b362 100644 --- a/README.md +++ b/README.md @@ -22,6 +22,7 @@ via plain text files. - [x] Links to standalone article - [x] Standalone article page - [x] Links to scrolling blog page + - [x] TTS Functionality - [x] RSS feed - [x] Navigation - [x] Header diff --git a/src/app.py b/src/app.py index 8035d32..60c1495 100644 --- a/src/app.py +++ b/src/app.py @@ -137,4 +137,5 @@ def feed(): if __name__ == "__main__": + con_gen.prepare_tts() app.run(host="0.0.0.0") diff --git a/src/content.py b/src/content.py index bc92506..5ec7cb5 100644 --- a/src/content.py +++ b/src/content.py @@ -1,3 +1,4 @@ +import glob import locale import os import pathlib @@ -7,6 +8,7 @@ from os import path import markdown from bs4 import BeautifulSoup +from gtts import gTTS import config import search @@ -144,6 +146,11 @@ def gen_stand_string(path_ex): content_string += "" + curr_date + "" content_string += "

\n" + if os.path.isfile("static/tmp/" + filename_no_end + ".mp3"): + content_string += "\n" + content_string += "

\n" if filename.endswith(".html"): for line in text: content_string += line @@ -270,3 +277,50 @@ def create_preview(path, is_markdown): preview = "\n

" + first_p.text + "

\n" preview += "...
" return preview + + +def get_text_only(filename): + """ + Convert a file to text only to use in tts + + Parameters: + path (string): path to the article + + Returns: + string: unformatted string containing the contents of the file + """ + # filename = os.path.join(ENTRY_DIR, path) + clean_text = "" + if path.exists(filename): + title = open(filename).readline().rstrip("\n") + text = open(filename).readlines()[1:] + filename_no_end = filename.split(".", 1)[0] + filename_no_end = filename_no_end.split("/")[-1] + content_string = "" + if filename.endswith(".html"): + for line in text: + content_string += line + if filename.endswith(".md"): + content_string += gen_md_content(filename, 1) + content_string = absolutize_html(content_string) + soup = BeautifulSoup(content_string, "html.parser") + tag_to_remove = soup.find("figure") + if tag_to_remove: + tag_to_remove.decompose() + clean_text = soup.get_text(separator=" ") + clean_text = title + "\n\n" + clean_text + return clean_text + + +def prepare_tts(): + files = glob.glob('static/tmp/*') + for f in files: + os.remove(f) + files = glob.glob('templates/entry/*') + clean_text = "" + for f in files: + clean_text = get_text_only(f) + tts = gTTS(clean_text, lang=LANGUAGE.split("-")[0]) + _, tail = os.path.split(f) + new_filename = "static/tmp/" + os.path.splitext(tail)[0] + ".mp3" + tts.save(new_filename) diff --git a/src/requirements.txt b/src/requirements.txt index 13dc9a4..71917a2 100644 --- a/src/requirements.txt +++ b/src/requirements.txt @@ -5,3 +5,4 @@ WTForms Flask_WTF Font-Awesome-Flask BeautifulSoup4 +gTTS diff --git a/src/static/css/style.css b/src/static/css/style.css index 6cbfb52..65d5d8b 100644 --- a/src/static/css/style.css +++ b/src/static/css/style.css @@ -269,6 +269,12 @@ form.search::after { .standalone img { width: 50%; } + .entry img { width: 50%; } + +audio { + width: 50%; + height: 5vh; +} diff --git a/src/static/tmp/.gitkeep b/src/static/tmp/.gitkeep new file mode 100644 index 0000000..e69de29