-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
106 lines (81 loc) · 4.7 KB
/
Copy pathmain.py
File metadata and controls
106 lines (81 loc) · 4.7 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
import datetime
import json
import random
from fastapi import FastAPI, HTTPException
from fastapi.responses import FileResponse
app = FastAPI()
# Currently the "/" has to be removed locally
images_dir = "/sources"
@app.get("/endpoints")
@app.get("/endpunkte")
async def get_endpoints():
available_endpoints = read_endpoints()
return {"message": f"Endpoints: {available_endpoints}"}
@app.get("/endpoints/{endpoint}")
@app.get("/endpunkte/{endpoint}")
async def get_image(endpoint: str):
images = read_endpoint(endpoint).get("images")
if images is None:
raise HTTPException(status_code=404, detail="No images found")
possible_images = list(filter((lambda image: image.get("file_name") is not None), images))
importance_first_images = list(filter((lambda image: image.get("importance") == "first"), possible_images))
if len(importance_first_images) > 0:
return FileResponse(f"{images_dir}/{random.choice(importance_first_images)["file_name"]}")
specific_date_and_time_range = list(
filter((lambda image: is_today_in_specific_date_range(image) and is_now_in_time_range(image)), possible_images))
if len(specific_date_and_time_range) > 0:
return FileResponse(f"{images_dir}/{random.choice(specific_date_and_time_range)["file_name"]}")
specific_date_range = list(filter((lambda image: is_today_in_specific_date_range(image)), possible_images))
if len(specific_date_range) > 0:
return FileResponse(f"{images_dir}/{random.choice(specific_date_range)["file_name"]}")
possible_images = list(filter(lambda image: image.get("dateRange") is None, possible_images))
general_date_and_time_range = list(
filter((lambda image: is_today_in_general_date_range(image) and is_now_in_time_range(image)), possible_images))
if len(general_date_and_time_range) > 0:
return FileResponse(f"{images_dir}/{random.choice(general_date_and_time_range)["file_name"]}")
general_date_range = list(filter((lambda image: is_today_in_general_date_range(image)), possible_images))
if len(general_date_range) > 0:
return FileResponse(f"{images_dir}/{random.choice(general_date_range)["file_name"]}")
possible_images = list(filter(lambda image: image.get("annualDateRange") is None, possible_images))
time_range = list(filter((lambda image: is_now_in_time_range(image)), possible_images))
if len(time_range) > 0:
return FileResponse(f"{images_dir}/{random.choice(time_range)["file_name"]}")
possible_images = list(filter(lambda image: image.get("timeRange") is None, possible_images))
if len(possible_images) == 0:
raise HTTPException(status_code=404, detail="No valid images found")
return FileResponse(f"{images_dir}/{random.choice(possible_images)["file_name"]}")
def read_endpoints():
with open(f"{images_dir}/configuration.json") as config_reader:
config = json.load(config_reader)
endpoints = map((lambda endpoint: endpoint["name"]), config["endpoints"])
return ", ".join(endpoints)
def read_endpoint(name: str):
with open(f"{images_dir}/configuration.json") as config_reader:
config = json.load(config_reader)
return next(endpoint for endpoint in config["endpoints"] if endpoint["name"] == name)
def is_today_in_specific_date_range(image):
date_range_object = image.get("dateRange")
if date_range_object is None:
return False
current_date = datetime.datetime.now()
specific_date_start = datetime.datetime.strptime(image.get("dateRange").get("from"), "%d.%m.%Y")
specific_date_end = datetime.datetime.strptime(image.get("dateRange").get("to"), "%d.%m.%Y")
return specific_date_start <= current_date <= specific_date_end
def is_today_in_general_date_range(image):
date_range_object = image.get("annualDateRange")
if date_range_object is None:
return False
current_date = datetime.datetime.now()
date_start = datetime.datetime.strptime(image.get("annualDateRange").get("from"), "%d.%m.").replace(year=current_date.year)
date_end = datetime.datetime.strptime(image.get("annualDateRange").get("to"), "%d.%m.").replace(year=current_date.year)
if date_end < date_start:
return date_start <= current_date <= date_end.replace(year=current_date.year+1) or date_start.replace(year=current_date.year-1) == date_end
return date_start <= current_date <= date_end
def is_now_in_time_range(image):
time_range_object = image.get("timeRange")
if time_range_object is None:
return False
current_time = datetime.datetime.now().time()
time_start = datetime.time(image.get("timeRange").get("from"), 0, 0)
time_end = datetime.time(image.get("timeRange").get("to"), 0, 0)
return time_start <= current_time <= time_end