-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.py
More file actions
166 lines (137 loc) · 4.96 KB
/
app.py
File metadata and controls
166 lines (137 loc) · 4.96 KB
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import base64
import json
import logging
import mimetypes
import os
import random
from functools import lru_cache
from typing import Any, List, Tuple, Union
from flask import (Flask, Response, abort, jsonify, render_template,
send_from_directory)
from config import Config
# Configure logging
logging.basicConfig(
level=Config.LOG_LEVEL,
format=Config.LOG_FORMAT
)
logger = logging.getLogger(__name__)
app = Flask(__name__)
app.config.from_object(Config)
def load_insults() -> List[str]:
"""Load the insults dataset from JSON."""
try:
with open(app.config['INSULTS_PATH'], 'r', encoding='utf-8') as f:
return json.load(f)
except Exception as e:
logger.error(f"Failed to load insults: {e}")
return []
insultes: List[str] = load_insults()
@lru_cache(maxsize=1)
def get_all_images() -> List[str]:
"""List all images in the static image folder. Cached for performance."""
static_img_path = app.config['STATIC_IMAGE_PATH']
if not os.path.exists(static_img_path):
logger.warning(f"Image directory not found: {static_img_path}")
return []
return sorted([f for f in os.listdir(static_img_path)
if f.lower().endswith(('.jpg', '.jpeg', '.png', '.gif'))])
@app.route("/")
def index() -> str:
insulte = random.choice(insultes) if insultes else "No insults available"
return render_template('simple.html', quote=insulte)
@app.route("/api")
def api() -> Response:
insulte = random.choice(insultes) if insultes else ""
return jsonify({"result": insulte})
@app.route("/api/v1")
def apiv1() -> Union[Response, Tuple[Response, int]]:
if not insultes:
return jsonify({"error": "No insults available"}), 500
random_index = random.randrange(len(insultes))
return jsonify({
"insult": {
"text": insultes[random_index],
"index": random_index
}
})
@app.route("/api/v1/img")
def apiv1img() -> Union[Response, Tuple[str, int]]:
try:
all_images = get_all_images()
if not all_images:
return "No images found", 404
if not insultes:
return "No insults available", 500
random_index = random.randrange(len(insultes))
random_img_index = random.randrange(len(all_images))
image_name = all_images[random_img_index]
image_path = os.path.join(app.config['STATIC_IMAGE_PATH'], image_name)
with open(image_path, 'rb') as image_file:
image_64_encode = base64.b64encode(image_file.read()).decode('utf-8')
mimetype, _ = mimetypes.guess_type(image_path)
return jsonify({
"insult": {
"text": insultes[random_index],
"index": random_index
},
"image": {
"data": image_64_encode,
"mimetype": mimetype or "image/jpeg",
"indexImg": random_img_index
}
})
except Exception as e:
logger.error(f"An error occurred in apiv1img: {str(e)}", exc_info=True)
return "Internal Server Error", 500
@app.route("/img")
def img() -> Union[str, Tuple[str, int]]:
all_images = get_all_images()
if not all_images:
return "No images found", 404
if not insultes:
return "No insults available", 500
random_index = random.randrange(len(insultes))
random_img_index = random.randrange(len(all_images))
return render_template(
'img.html',
quote=insultes[random_index],
image=app.config['IMAGE_FOLDER'] + all_images[random_img_index],
ins=random_index,
imgNumber=random_img_index
)
@app.route("/series")
def series() -> str:
all_images = get_all_images()
insulte = random.choice(insultes) if insultes else ""
image_name = random.choice(all_images) if all_images else ""
return render_template('series.html', quote=insulte, image=app.config['IMAGE_FOLDER'] + image_name)
@app.route("/img/<int:quo>/<int:img>/")
def ins(quo: int, img: int) -> Union[str, Tuple[str, int]]:
all_images = get_all_images()
try:
insulte = insultes[quo]
image_name = all_images[img]
return render_template('img_noencore.html', quote=insulte, image=app.config['IMAGE_FOLDER'] + image_name)
except IndexError:
abort(404)
return "Not found", 404
@app.route('/favicon.ico')
def favicon() -> Response:
return send_from_directory(
os.path.join(app.root_path, 'static'),
'favicon.ico',
mimetype='image/vnd.microsoft.icon'
)
@app.errorhandler(500)
def internal_error(error: Any) -> Tuple[str, int]:
return "500 error", 500
@app.errorhandler(404)
def not_found(error: Any) -> Tuple[str, int]:
return "404 error", 404
@app.errorhandler(401)
def not_authorized(error: Any) -> Tuple[str, int]:
return "401 error", 401
if __name__ == "__main__":
port = int(os.environ.get('PORT', 80))
logger.info(f"Starting application on port {port}")
app.run(host='0.0.0.0', port=port)