Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
P
PROG2900
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Sara Savanovic Djordjevic
PROG2900
Commits
c76ed6da
Commit
c76ed6da
authored
1 year ago
by
Sara Savanovic Djordjevic
Browse files
Options
Downloads
Patches
Plain Diff
add: get_weather()
parent
b51335e7
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
server/APIs/get_weather.py
+41
-0
41 additions, 0 deletions
server/APIs/get_weather.py
with
41 additions
and
0 deletions
server/APIs/get_weather.py
0 → 100644
+
41
−
0
View file @
c76ed6da
from
flask
import
request
import
requests
import
datetime
import
json
# get_weather retrieves weather data for a given set of coordinates
def
get_weather
():
# Extract url parameters
latitude
=
request
.
args
.
get
(
'
lat
'
,
''
)
longitude
=
request
.
args
.
get
(
'
lon
'
,
''
)
# Form timestamp string with correct format
current_time
=
datetime
.
datetime
.
utcnow
().
strftime
(
"
%Y-%m-%dT%H:%M:%SZ
"
)
# Construct request string and execute get request to Yr API
url
=
f
"
https://api.met.no/weatherapi/locationforecast/2.0/compact?lat=
{
latitude
}
&lon=
{
longitude
}
"
response
=
requests
.
get
(
url
)
if
response
.
status_code
==
200
:
# Extract data from response
data
=
response
.
json
()
timeseries
=
data
[
'
properties
'
][
'
timeseries
'
]
# Get data for current time
weather_data
=
None
for
entry
in
timeseries
:
if
entry
[
'
time
'
]
==
current_time
:
weather_data
=
entry
[
'
data
'
]
break
if
weather_data
is
not
None
:
resp_code
=
200
else
:
resp_code
=
404
# Data for current time not found
weather_data
=
{}
else
:
# Write empty data object and return status code 500
resp_code
=
500
weather_data
=
{}
return
json
.
dumps
(
weather_data
),
resp_code
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment