🎉 initial commit

This commit is contained in:
Marc Koch 2024-12-03 11:02:03 +01:00
commit 9f58e25f10
Signed by untrusted user who does not match committer: marc.koch
GPG key ID: 12406554CFB028B9
14 changed files with 459 additions and 0 deletions

7
.dockerignore Normal file
View file

@ -0,0 +1,7 @@
__pycache__/
venv/
.git/
*.pyc
.menu_image_hash
menu.txt

152
.gitignore vendored Normal file
View file

@ -0,0 +1,152 @@
# Created by https://www.toptal.com/developers/gitignore/api/flask
# Edit at https://www.toptal.com/developers/gitignore?templates=flask
### Flask ###
instance/*
!instance/.gitignore
.webassets-cache
.env
### Flask.Python Stack ###
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class
# C extensions
*.so
# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
share/python-wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST
# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec
# Installer logs
pip-log.txt
pip-delete-this-directory.txt
# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
*.py,cover
.hypothesis/
.pytest_cache/
cover/
# Translations
*.mo
*.pot
# Django stuff:
*.log
local_settings.py
db.sqlite3
db.sqlite3-journal
# Flask stuff:
instance/
# Scrapy stuff:
.scrapy
# Sphinx documentation
docs/_build/
# PyBuilder
.pybuilder/
target/
# Jupyter Notebook
.ipynb_checkpoints
# IPython
profile_default/
ipython_config.py
# pdm
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
#pdm.lock
# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
# in version control.
# https://pdm.fming.dev/#use-with-ide
.pdm.toml
# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
__pypackages__/
# Celery stuff
celerybeat-schedule
celerybeat.pid
# SageMath parsed files
*.sage.py
# Environments
.venv
env/
venv/
ENV/
env.bak/
venv.bak/
# Spyder project settings
.spyderproject
.spyproject
# Rope project settings
.ropeproject
# mkdocs documentation
/site
# mypy
.mypy_cache/
.dmypy.json
dmypy.json
# Pyre type checker
.pyre/
# pytype static type analyzer
.pytype/
# Cython debug symbols
cython_debug/
# PyCharm files
.idea/
# Generated files
.menu_image_hash
menu.txt

8
.idea/.gitignore generated vendored Normal file
View file

@ -0,0 +1,8 @@
# Default ignored files
/shelf/
/workspace.xml
# Editor-based HTTP Client requests
/httpRequests/
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml

14
.idea/bueze-menu.iml generated Normal file
View file

@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<module version="4">
<component name="Flask">
<option name="enabled" value="true" />
</component>
<component name="TemplatesService">
<option name="TEMPLATE_CONFIGURATION" value="Jinja2" />
<option name="TEMPLATE_FOLDERS">
<list>
<option value="$MODULE_DIR$/templates" />
</list>
</option>
</component>
</module>

View file

@ -0,0 +1,69 @@
<component name="InspectionProjectProfileManager">
<profile version="1.0">
<option name="myName" value="Project Default" />
<inspection_tool class="HttpUrlsUsage" enabled="true" level="WEAK WARNING" enabled_by_default="true">
<option name="ignoredUrls">
<list>
<option value="http://localhost" />
<option value="http://127.0.0.1" />
<option value="http://0.0.0.0" />
<option value="http://www.w3.org/" />
<option value="http://json-schema.org/draft" />
<option value="http://java.sun.com/" />
<option value="http://xmlns.jcp.org/" />
<option value="http://javafx.com/javafx/" />
<option value="http://javafx.com/fxml" />
<option value="http://maven.apache.org/xsd/" />
<option value="http://maven.apache.org/POM/" />
<option value="http://www.springframework.org/schema/" />
<option value="http://www.springframework.org/tags" />
<option value="http://www.springframework.org/security/tags" />
<option value="http://www.thymeleaf.org" />
<option value="http://www.jboss.org/j2ee/schema/" />
<option value="http://www.jboss.com/xml/ns/" />
<option value="http://www.ibm.com/webservices/xsd" />
<option value="http://activemq.apache.org/schema/" />
<option value="http://schema.cloudfoundry.org/spring/" />
<option value="http://schemas.xmlsoap.org/" />
<option value="http://cxf.apache.org/schemas/" />
<option value="http://primefaces.org/ui" />
<option value="http://tiles.apache.org/" />
<option value="http://" />
</list>
</option>
</inspection_tool>
<inspection_tool class="PyCompatibilityInspection" enabled="true" level="WARNING" enabled_by_default="true">
<option name="ourVersions">
<value>
<list size="3">
<item index="0" class="java.lang.String" itemvalue="3.12" />
<item index="1" class="java.lang.String" itemvalue="3.11" />
<item index="2" class="java.lang.String" itemvalue="3.10" />
</list>
</value>
</option>
</inspection_tool>
<inspection_tool class="PyPackageRequirementsInspection" enabled="true" level="WARNING" enabled_by_default="true">
<option name="ignoredPackages">
<value>
<list size="13">
<item index="0" class="java.lang.String" itemvalue="pandas" />
<item index="1" class="java.lang.String" itemvalue="PyPDF2" />
<item index="2" class="java.lang.String" itemvalue="drf-spectacular" />
<item index="3" class="java.lang.String" itemvalue="django-crispy-forms" />
<item index="4" class="java.lang.String" itemvalue="Django" />
<item index="5" class="java.lang.String" itemvalue="qrcode" />
<item index="6" class="java.lang.String" itemvalue="psycopg2" />
<item index="7" class="java.lang.String" itemvalue="django-active-link" />
<item index="8" class="java.lang.String" itemvalue="shortuuid" />
<item index="9" class="java.lang.String" itemvalue="uwsgi" />
<item index="10" class="java.lang.String" itemvalue="djangorestframework" />
<item index="11" class="java.lang.String" itemvalue="Pillow" />
<item index="12" class="java.lang.String" itemvalue="pyproject_hooks" />
</list>
</value>
</option>
</inspection_tool>
<inspection_tool class="ReassignedToPlainText" enabled="false" level="WARNING" enabled_by_default="false" />
</profile>
</component>

10
.idea/material_theme_project_new.xml generated Normal file
View file

@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="MaterialThemeProjectNewConfig">
<option name="metadata">
<MTProjectMetadataState>
<option name="userId" value="32158af6:1938925765f:-7ff5" />
</MTProjectMetadataState>
</option>
</component>
</project>

10
.idea/misc.xml generated Normal file
View file

@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Black">
<option name="sdkName" value="Python 3.12 (bueze-menu)" />
</component>
<component name="MarkdownSettingsMigration">
<option name="stateVersion" value="1" />
</component>
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.8 (tazPlease)" project-jdk-type="Python SDK" />
</project>

6
.idea/vcs.xml generated Normal file
View file

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>

19
Dockerfile Normal file
View file

@ -0,0 +1,19 @@
FROM python:3.11-slim-bookworm
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1
WORKDIR /app
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
ENV DEBIAN_FRONTEND=noninteractive
RUN apt update && apt install -y tesseract-ocr tesseract-ocr-deu && rm -rf /var/lib/apt/lists/*
COPY . .
EXPOSE 5000
CMD ["flask", "--app", "app", "run", "--host=0.0.0.0"]

1
README.md Normal file
View file

@ -0,0 +1 @@
# bueze-menu

84
app.py Normal file
View file

@ -0,0 +1,84 @@
import hashlib
import io
from pathlib import Path
from typing import Tuple
import pytesseract
import requests
from PIL import Image
from bs4 import BeautifulSoup
from flask import Flask, render_template, make_response
app = Flask(__name__)
url = 'https://bueze.de/unser-mittagstisch/'
image_hash_file = Path('.menu_image_hash')
menu_text = Path("./menu.txt")
def find_image_url():
with requests.get(url) as response:
response.raise_for_status()
soup = BeautifulSoup(response.text, 'html.parser')
image = soup.find('figure', class_='wp-block-image').find('img')
return image.get('src')
def get_image_bytes(image_url):
with requests.get(image_url, stream=True) as response:
response.raise_for_status()
return response.raw.read()
def is_same_hash(img_bytes):
if image_hash_file.exists():
previous_hash = image_hash_file.read_text()
new_hash = hashlib.md5(img_bytes).hexdigest()
return previous_hash == new_hash
return False
def write_html(text):
with menu_text.open('w') as f:
f.write(text)
def get_menu() -> Tuple[Path, str]:
"""
Get the menu text and image URL.
:return: menu_text, image_url
"""
image_url = find_image_url()
image_bytes = get_image_bytes(image_url)
# Check if the image has changed
if is_same_hash(image_bytes) and menu_text.exists():
print('No new image')
# If the image has changed, write the new hash to the file and extract the text
else:
image_hash_file.write_text(hashlib.md5(image_bytes).hexdigest())
image = Image.open(io.BytesIO(image_bytes))
text = pytesseract.image_to_string(image, lang='deu')
print('New image found')
write_html(text)
return menu_text, image_url
@app.route('/')
def root():
"""
This view function returns the menu text and image as a web page.
"""
text, image = get_menu()
lines = menu_text.read_text().splitlines()
html = render_template('base.html',
lines=lines,
img=image,
text=text.read_text())
return make_response(html, 200)
@app.route('/plain')
def plain():
"""
This view function returns the menu text as plain text.
"""
text, _ = get_menu()
response = make_response(text.read_text(), 200)
response.mimetype = 'text/plain'
return response

18
requirements.txt Normal file
View file

@ -0,0 +1,18 @@
beautifulsoup4==4.12.3
blinker==1.9.0
certifi==2024.8.30
charset-normalizer==3.4.0
click==8.1.7
Flask==3.1.0
idna==3.10
itsdangerous==2.2.0
Jinja2==3.1.4
Markdown==3.7
MarkupSafe==3.0.2
packaging==24.2
pillow==11.0.0
pytesseract==0.3.13
requests==2.32.3
soupsieve==2.6
urllib3==2.2.3
Werkzeug==3.1.3

36
static/style.css Normal file
View file

@ -0,0 +1,36 @@
body {
background-color: #f0f0f0;
font-family: Arial, sans-serif;
}
.container {
width: 80%;
margin: 6em auto;
display: flex;
justify-content: space-between;
flex-wrap: wrap;
}
.menu {
width: 60%;
flex: 1;
margin-right: 10em;
text-align: center;
}
@media (max-width: 1500px) {
.container {
flex-direction: column;
align-items: center;
}
.menu {
margin-right: 0;
margin-bottom: 3em;
}
img {
max-width: 100%;
margin-top: 3em;
}
}

25
templates/base.html Normal file
View file

@ -0,0 +1,25 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>BüzE Menü</title>
<link rel="stylesheet" href="{{ url_for('static', filename='style.css') }}">
</head>
<body>
<div class="container">
<div class="menu">
{% if lines %}
{% for line in lines %}
{% if loop.index == 1 %}<h1>{{ line }}</h1>
{% elif line == '' %}<br>
{% else %}<p>{{ line }}</p>
{% endif %}
{% endfor %}
{% else %}
<p>Leider konnte kein Menü gefunden werden.</p>
{% endif %}
</div>
<img src="{{ img }}" alt="{{ text }}">
</div>
</body>
</html>