-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
175 lines (137 loc) · 5.61 KB
/
app.py
File metadata and controls
175 lines (137 loc) · 5.61 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
167
168
169
170
171
172
173
174
175
from nasa_scraper import create_folder, scrappe_nasa, resize_image, get_apod_metadata
from flask import Flask, jsonify, send_file, request
import datetime, random
app = Flask(__name__)
PORT = 3400
BASE_IMAGE_DIR = "public"
BASE_URL = f"http://localhost:{PORT}"
def get_current_utc_date() -> str:
return (datetime.datetime.now(datetime.timezone(datetime.timedelta(hours=-5)))).strftime("%y%m%d")
def process_image(date: str, w: int, h: int, crop: bool, minW: int = 0, minH: int = 0):
try:
image_path = scrappe_nasa(date, minW, minH)
if not(image_path):
image_path, date = "default.jpg", "default"
image_name = f"{date}.jpg"
if ((w > 0) and (h > 0)):
output_folder = f"{BASE_IMAGE_DIR}/{w}x{h}"
resized_path = f"{output_folder}/{date}.jpg"
resized_name = image_name.replace(".jpg", f"_{w}x{h}.jpg")
create_folder(output_folder)
resize_image(image_path, resized_path, w, h, crop=crop)
return resized_path, resized_name
else:
return image_path, image_name
except Exception as e:
print(f"Error: {str(e)}")
return "default.jpg", "default.jpg"
@app.route("/", methods=["GET"])
def read_root():
return jsonify({
"message": (
"Welcome to the NASA Daily Picture API! "
"Docs: https://github.com/Leogendra/NASA-Daily-API"
)
})
@app.route("/daily/", methods=["GET"])
def get_daily_nasa():
try:
w = int(request.args.get("w", 0))
h = int(request.args.get("h", 0))
crop = request.args.get("crop", "true").lower() == "true"
download = request.args.get("download", "false").lower() in ["true", "1", "yes"]
todayUTC = get_current_utc_date()
image_path, image_name = process_image(todayUTC, w, h, crop)
if (image_name == "default.jpg"):
return get_random_image()
if (image_path):
return send_file(
image_path,
mimetype="image/jpeg",
as_attachment=download,
download_name=image_name
)
else:
return jsonify({"error": "Image not found"}), 404
except Exception as e:
return jsonify({"error": str(e)}), 500
@app.route("/date/<date>/", methods=["GET"])
def get_image_by_date(date: str):
try:
w = int(request.args.get("w", 0))
h = int(request.args.get("h", 0))
crop = request.args.get("crop", "true").lower() == "true"
download = request.args.get("download", "false").lower() in ["true", "1", "yes"]
if not date.isdigit() or len(date) != 6:
return jsonify({"error": "Invalid date format. Use YYMMDD."}), 400
image_path, image_name = process_image(date, w, h, crop)
if (image_path):
return send_file(
image_path,
mimetype="image/jpeg",
as_attachment=download,
download_name=image_name
)
else:
return jsonify({"error": "Image not found"}), 404
except Exception as e:
return jsonify({"error": str(e)}), 500
@app.route("/random/", methods=["GET"])
def get_random_image():
try:
w = int(request.args.get("w", 0))
h = int(request.args.get("h", 0))
crop = request.args.get("crop", "true").lower() == "true"
minW = int(request.args.get("minW", 0))
minH = int(request.args.get("minH", 0))
download = request.args.get("download", "false").lower() in ["true", "1", "yes"]
start_date = datetime.datetime(1996, 1, 1)
end_date = datetime.datetime.now()
nbRetries = 5
while (nbRetries):
print(f"Searching for image... {nbRetries} retries left")
random_days = random.randint(0, (end_date - start_date).days)
random_date = start_date + datetime.timedelta(days=random_days)
random_date_str = random_date.strftime("%y%m%d")
image_path, image_name = process_image(random_date_str, w, h, crop, minW, minH)
if ("default" not in image_name):
break
else:
nbRetries -= 1
if (image_path):
return send_file(
image_path,
mimetype="image/jpeg",
as_attachment=download,
download_name=image_name
)
else:
return jsonify({"error": "No image found with the specified parameters"}), 404
except Exception as e:
return jsonify({"error": str(e)}), 500
@app.route("/metadata/daily/", methods=["GET"])
def get_daily_metadata():
try:
todayUTC = get_current_utc_date()
metadata = get_apod_metadata(todayUTC)
if metadata:
return jsonify(metadata)
else:
return jsonify({"error": "Metadata not found"}), 404
except Exception as e:
return jsonify({"error": str(e)}), 500
@app.route("/metadata/date/<date>/", methods=["GET"])
def get_metadata_by_date(date: str):
try:
if (not(date.isdigit()) or len(date) != 6):
return jsonify({"error": "Invalid date format. Use YYMMDD."}), 400
metadata = get_apod_metadata(date)
if metadata:
return jsonify(metadata)
else:
return jsonify({"error": "Metadata not found"}), 404
except Exception as e:
return jsonify({"error": str(e)}), 500
if __name__ == "__main__":
app.run(debug=True, port=PORT)
print(f"Server is running on http://localhost:{PORT}")