File size: 3,519 Bytes
cd180fe
 
af3518b
 
cd180fe
 
6a2d7ad
cd180fe
6a2d7ad
af3518b
 
cd180fe
 
ddac79a
 
cd180fe
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
af3518b
cd180fe
 
 
 
 
 
 
6a2d7ad
 
cd180fe
 
 
 
 
 
 
6a2d7ad
cd180fe
 
 
 
 
 
 
ddac79a
cd180fe
 
 
 
 
 
af3518b
cd180fe
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6a2d7ad
cd180fe
 
 
 
 
 
 
 
 
 
 
 
 
6a2d7ad
cd180fe
 
 
 
 
 
 
 
af3518b
 
cd180fe
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
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

@app.route('/')
def index():
    # Render the HTML template with fresh image data
    image_data = fetch_image_files()
    return render_template("index.html", image_data=image_data)

@app.route('/info')
def get_info():
    info = refresh_info()
    return jsonify(info)

@app.route('/images')
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__)

@app.route('/cache/<image_name>')
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

@app.route('/statics/<image_name>')
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

@app.route('/yaml/<image_name>')
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")