forked from brainupgrade-in/gh-actions-python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
74 lines (65 loc) · 2.39 KB
/
app.py
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
from flask import Flask
import os
import requests
import json
app = Flask(__name__)
@app.route('/')
def get_env():
return list_env_vars()
@app.route('/env')
def list_env_vars():
env_vars = os.environ
return '\n'.join([f'{key}: {value}' for key, value in env_vars.items()])
@app.route('/env-file')
def list_properties_file():
properties_file_path = '/etc/app/application.properties'
try:
with open(properties_file_path, 'r') as file:
content = file.read()
# return content.replace('\n', '<br>')
return content
except FileNotFoundError:
return "Properties file not found."
except Exception as e:
return f"An error occurred: {e}"
# http://localhost:5000/place/berlin
@app.route('/place/<name>')
def get_location_details(name):
API_URL_GEOCODING = os.environ.get('API_URL_GEOCODING')
url = f'{API_URL_GEOCODING}/search?name={name}&count=1&language=en&format=json'
# url = f'{url_params}'
try:
response = requests.get(url)
response.raise_for_status()
data = response.json()
results = data.get('results', [])
if results:
first_result = results[0]
return {
'name': f'{name}',
'country': first_result.get('country'),
'latitude': first_result.get('latitude'),
'longitude': first_result.get('longitude'),
'temperature (C)': get_current_temperature(first_result.get('latitude'), first_result.get('longitude'))
}
else:
return None
except requests.RequestException as e:
print(f"An error occurred while fetching location details: {e}")
return None
@app.route('/temperature/<latitude>/<longitude>')
def get_current_temperature(latitude, longitude):
API_URL_WEATHER = os.environ.get('API_URL_WEATHER')
url = f'{API_URL_WEATHER}/forecast?latitude={latitude}&longitude={longitude}¤t=temperature'
# url = f'{url_params}'
try:
response = requests.get(url)
response.raise_for_status()
data = response.json()
current_weather = data.get('current', {})
return current_weather.get('temperature')
except requests.RequestException as e:
print(f"An error occurred while fetching temperature: {e}")
return jsonify(error=str(e)), 500
if __name__ == '__main__':
app.run(debug=True)