Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
W
woRkflow
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Deploy
Releases
Model registry
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Tom Rushby
woRkflow
Commits
555fad1c
Commit
555fad1c
authored
4 years ago
by
Tom Rushby
Browse files
Options
Downloads
Patches
Plain Diff
open weather API implementation (WIP)
parent
06c054a7
No related branches found
No related tags found
No related merge requests found
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
howTo/APIkeys/owmAPIkey.R
+2
-0
2 additions, 0 deletions
howTo/APIkeys/owmAPIkey.R
howTo/apiOpenWeather.R
+57
-0
57 additions, 0 deletions
howTo/apiOpenWeather.R
howTo/weatherAPI/Icons/10d.png
+0
-0
0 additions, 0 deletions
howTo/weatherAPI/Icons/10d.png
with
59 additions
and
0 deletions
howTo/APIkeys/owmAPIkey.R
0 → 100644
+
2
−
0
View file @
555fad1c
# OpenWeatherMapAPI key
owmAPIkey
<-
"bda9e4fdac62847c129d8ed3227b7325"
\ No newline at end of file
This diff is collapsed.
Click to expand it.
howTo/apiOpenWeather.R
0 → 100644
+
57
−
0
View file @
555fad1c
# Open Weather API
library
(
woRkflow
)
# load workflow package
woRkflow
::
setup
()
# load env.R set up the default paths etc
reqLibs
<-
c
(
"httr"
,
"jsonlite"
,
"knitr"
,
"tidyr"
,
"data.table"
,
# data munching
"here"
,
# here
"lubridate"
,
# dates and times
"ggplot2"
,
# plots
"bookdown"
# for making reports (should also install knittr etc)
)
# load them
woRkflow
::
loadLibraries
(
reqLibs
)
# Load API key from external file - so can restrict access if required
source
(
paste0
(
here
::
here
(),
'/howTo/APIkeys/owmAPIkey.R'
))
# OpenWeatherMapAPI key
# API request ----
# Get hourly weather forecast from Open Weather (use '&units=metric' for celsius)
getURL
<-
paste0
(
"https://api.openweathermap.org/data/2.5/onecall?lat=50.937337&lon=-1.404147&units=metric&exclude=minutely,daily&appid="
,
owmAPIkey
)
response
<-
httr
::
GET
(
getURL
)
openWeatherData
<-
fromJSON
(
rawToChar
(
response
$
content
))
str
(
openWeatherData
)
# forecast - current
currentWeather
<-
openWeatherData
$
current
# into own data.frame
# forecast - 48 hours ahead
hourlyWeather
<-
openWeatherData
$
hourly
# into own data.frame
# process data ----
procTime
<-
function
(
x
){
as.POSIXct
(
x
,
origin
=
"1970-01-01"
)
# convert unix time
}
currentWeather
[
1
:
3
]
<-
lapply
(
currentWeather
[
1
:
3
],
procTime
)
hourlyWeather
[,
1
]
<-
lapply
(
hourlyWeather
[
1
],
procTime
)
hourlyWeather
<-
unnest
(
hourlyWeather
,
cols
=
weather
)
# Outputs and util ----
# What to do with the data & presentation info
# ..$pop = probablility of precipitation
# Get icons - URLs
icon
<-
"10d"
iconURL
<-
paste0
(
"http://openweathermap.org/img/wn/"
,
icon
,
"@2x.png"
)
download.file
(
iconURL
,
paste0
(
here
::
here
(),
'/howTo/weatherAPI/icons/'
,
icon
,
'.png'
),
mode
=
'wb'
)
message
(
paste0
(
"The weather now is: "
,
currentWeather
$
weather
$
main
,
" and the temperature is "
,
round
(
currentWeather
$
temp
),
" degrees."
))
This diff is collapsed.
Click to expand it.
howTo/weatherAPI/Icons/10d.png
0 → 100644
+
0
−
0
View file @
555fad1c
2.52 KiB
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