Spaces:
Paused
Paused
from flask import Flask, request, jsonify, send_file | |
from flask.templating import render_template | |
import os | |
import threading | |
import yaml | |
from image_payload_reader import read_payload_from_image | |
#from telegram import start_telegram_bot | |
from utils import read_config | |
from imgbb import extract_original_image_urls | |
app = Flask(__name__) | |
templates_dir = "templates" | |
CACHE_DIR = '/tmp/cache' | |
# Model for response | |
class ImageFile: | |
def __init__(self, image, yaml_data): | |
self.image = image | |
self.yaml = yaml_data | |
class ImageResponse: | |
def __init__(self, image_files): | |
self.image_files = image_files | |
def refresh_info(): | |
config = read_config() | |
app_version = config['app']['version'] | |
whatsapp_bot_enabled = config['app']['whatsapp_bot_enabled'] | |
telegram_bot_enabled = config['app']['telegram_bot_enabled'] | |
info = { | |
'version': app_version, | |
'whatsapp_bot_enabled': whatsapp_bot_enabled, | |
'telegram_bot_enabled': telegram_bot_enabled, | |
} | |
return info | |
def start_bots(): | |
info = refresh_info() | |
telegram_bot_enabled = info['telegram_bot_enabled'] | |
if telegram_bot_enabled: | |
#start_telegram_bot() | |
pass | |
# Note: WhatsApp bot functionality is not implemented in this Flask version | |
# Start the Telegram bot in a separate thread when the application starts | |
threading.Thread(target=start_bots).start() | |
# Function to fetch image files and corresponding YAML data from 'cache' directory | |
def fetch_image_files(): | |
files = extract_original_image_urls() | |
image_files = [f for f in files if f.endswith(('.jpg', '.jpeg', '.png'))] | |
response_data = [] | |
for image_file in image_files: | |
yaml_file = image_file.rsplit('.', 1)[0] + '.yaml' | |
yaml_data = {} | |
if yaml_file in files: | |
with open(os.path.join(CACHE_DIR, yaml_file), 'r') as file: | |
yaml_data = yaml.safe_load(file) | |
response_data.append(ImageFile(image=image_file, yaml_data=yaml_data)) | |
return response_data | |
def index(): | |
# Render the HTML template with fresh image data | |
image_data = fetch_image_files() | |
return render_template("index.html", image_data=image_data) | |
def get_info(): | |
info = refresh_info() | |
return jsonify(info) | |
def get_images(): | |
# Fetch fresh image data | |
image_data = fetch_image_files() | |
response_data = [img.__dict__ for img in image_data] | |
return jsonify(ImageResponse(response_data).__dict__) | |
def get_image(image_name): | |
file_path = image_name | |
if os.path.exists(file_path): | |
return send_file(file_path) | |
return jsonify({"error": "File not found"}), 404 | |
def get_static_image(image_name): | |
file_path = os.path.join('statics', image_name) | |
if os.path.exists(file_path): | |
return send_file(file_path) | |
return jsonify({"error": "File not found"}), 404 | |
def get_embedded_yaml(image_name): | |
file_path = image_name | |
if not os.path.exists(file_path): | |
return jsonify({"error": "Image file not found"}), 404 | |
# Use the function to read YAML payload from image metadata | |
yaml_payload = read_payload_from_image(file_path) | |
if yaml_payload: | |
return jsonify(yaml_payload) | |
return jsonify({"error": "No YAML payload found in the image metadata"}), 404 | |
if __name__ == '__main__': | |
app.run(debug=True, port=7860, host="0.0.0.0") | |