Load env variables and fix wrong text marker usage.

1. If environment variables are not set before by docker compose, program tries to get them from .env file.
2. Fix: If text marker was not found on bs page a wrong text marker was added.
This commit is contained in:
Holger Weber 2025-07-18 08:45:20 +02:00
parent ab6d833018
commit a3f5f4a5dc
3 changed files with 13 additions and 8 deletions

View File

@ -2,6 +2,11 @@ import requests
from bs4 import BeautifulSoup
from urllib.parse import urlparse
import os
from dotenv import load_dotenv
if "BOOKSTACK_URL" not in os.environ:
print("Environment variables not read. Try reading from file.")
load_dotenv()
BOOKSTACK_URL = os.getenv("BOOKSTACK_URL","https://my-book-stack.com")
BOOKSTACK_TOKEN_ID = os.getenv("BOOKSTACK_TOKEN_ID", "TOKEN-ID")
@ -18,7 +23,6 @@ HEADERS = {
}
def is_valid_url(url):
parsed = urlparse(url)
return all([parsed.scheme in ("http", "https"), parsed.netloc])
@ -35,8 +39,6 @@ def get_page_id_by_slug(book_slug, page_slug):
raise("Book not found")
def get_page_title_from_url(url):
try:
@ -106,13 +108,12 @@ def append_link_to_bookstack_page(url):
new_entry = f"- [{title}]({url})<br>{url}"
# Füge Link unterhalb von INSERT_MARKER_LINK ein
if INSERT_MARKER_LINK in current_content:
parts = current_content.split(INSERT_MARKER_LINK)
updated_content = parts[0] + INSERT_MARKER_LINK + "\n" + new_entry + parts[1]
else:
# Falls Marker nicht vorhanden, füge ihn am Ende hinzu
updated_content = current_content + "\n\n## Unsortierte Links\n\n" + new_entry
updated_content = current_content + "\n\n" + INSERT_MARKER_LINK + "\n\n" + new_entry
# Seite aktualisieren
update_data = {
@ -150,7 +151,7 @@ def append_text_to_bockstack_page(strText):
updated_content = parts[0] + INSERT_MARKER_TEXT + "\n\n" + new_entry + "\n" + parts[1]
else:
# Falls Marker nicht vorhanden, füge ihn am Ende hinzu
updated_content = current_content + "\n\n## Textmerker\n\n" + new_entry
updated_content = current_content + "\n\n" + INSERT_MARKER_TEXT + "\n\n" + new_entry
# Seite aktualisieren
update_data = {

View File

@ -1,11 +1,16 @@
from flask import Flask, request, jsonify, abort, send_from_directory, render_template_string
import os
from dotenv import load_dotenv
from datetime import datetime
from werkzeug.utils import secure_filename
import bsupdate as bu
app = Flask(__name__)
if "API_TOKEN" not in os.environ:
print("Environment variables not read. Try reading from file.")
load_dotenv()
UPLOAD_FOLDER = "UPLOAD_FOLDER"
API_TOKEN = os.getenv("API_TOKEN", "my-secret-token")
ALLOWED_EXTENSIONS = {"png", "jpg", "jpeg", "gif", "webp"}
@ -43,8 +48,6 @@ def upload():
if text:
bu.add_link_or_text(text)
# with open(os.path.join(app.config["UPLOAD_FOLDER"], "log.txt"), "a") as f:
# f.write(f"{datetime.now()}: {text}\n")
print(f"Text erhalten: {text}")
return jsonify({"status": "OK"}), 200

View File

@ -1,3 +1,4 @@
flask
requests
beautifulsoup4
python-dotenv