AWS Polly to read current weather and news
seboo1995
Posted on November 15, 2019
Introduction
We all know that Cloud technologies and Machine Learning are extremely advancing, therefore, we need to utilize this.
AWS(Amazon Web Services) has a service called Polly reads a text, now this service uses Machine Learning in the background, but that is not the scope of this post.
A bit about the project
I extracted weather data by using the OWM(Open Weather Map) API for two cities in Hungary: Budapest and Miskolc.
My goal with this project was that everyone has a fast lifestyle, that we do not have the time to check the weather. SO that is why I created this so, for example, you can do your work while listening the weather data. This also can be enriched with news data, so I tried to use news API and enrich this application. Polly will read the top-news headlines.
The code was written in R (using R studio) and below I will show my code.
Importing libraries and setting global variables
Here I am using the owmr and tidyverse libraries. The owmr library is the interface that R has with OWM. I saved my API key somewhere safe in my directory and externally load it.
# SETTING THE ENVIRONMENT
#-----------------------------------------
library(owmr)
library(tidyverse)
#I will get my API key that is stored in my project directory,
#and make it global
#If you want to use this code,
#then name the file API_KEY and
#put the API key in the file.
API_KEY <- read_file("API_KEY")
Sys.setenv(OWM_API_KEY = API_KEY)
Specifying the location that I want to get weather data
Here I am trying to get the weather data with the API. In order to do that I need to get the id of the cities, that are defined by the API.
So the tibble owm_cities contains the id and coordinates of the
cities.
# ASSIGNING CITIES
#-----------------------------------------
#The city that I am interested in is Budapest and Deprecen(Hungarian Cities)
desired_city <- c("Budapest","Miskolc")
#In the package there is a tibble with the cities"
head(owm_cities)
# I am getting the id of the cities
id_of_cities <- owm_cities %>%
filter(nm %in% desired_city) %>%
select(id,nm)
Getting the weather data
Here I am getting the data from the API, but the data is in format that is hard to work on, however owmr package has a nice function called owmr_as_tibble that transforms the API response from a complicated object to a tibble.
# GETTING CURRENT WEATHER DATA
# -------------------------------------------------
#Getting the current weather data for this cities
response_from_API <- get_current_for_group(id_of_cities$id)
class(response_from_API)
#Because the format that response_from_API has it is not formated well enough
#I will use a function that turns the list and owmr_group in tibble
response_from_API
response_tibble <- owmr_as_tibble(response_from_API)
Here I am trying to get the forecast
# FORECAST WEATHER DATA
# -------------------------------------------------
#getting the forecast
forecast_Budapest <- get_forecast("Budapest")
forecast_Miskolc <- get_forecast("Miskolc")
#making them a tibble
forecast_Budapest <- owmr_as_tibble(forecast_Budapest)
forecast_Budapest
forecast_Miskolc <- owmr_as_tibble(forecast_Miskolc)
#Well I am intereste in getting the first 9 hours of the data, or the first 3 hours to
# the third 3 hour interval.
forecast_Budapest <-forecast_Budapest %>% select(weather_main, temp,humidity,weather_description,wind_speed)
forecast_Miskolc <-forecast_Miskolc %>% select(weather_main, temp,humidity,weather_description,wind_speed)
Saving data to AWS S3
Because data is so important, I will try to store it in S3 as well, just like back-up.
# SAVING FILES TO UPLOAD TO S3
#--------------------------------------
#For Budapest
write_csv(forecast_Budapest,path = "Budapest_forecast.csv")
#For Miskolc
write_csv(forecast_Miskolc,path = "Miskolc_forecast.csv")
# GETTING READY TO UPLOAD THE DATA IN S3
#--------------------------------------
#getting the keys
keyTable <- read.csv("credentials.csv", header = T)
AWS_ACCESS_KEY_ID <- as.character(keyTable$Access.key.ID)
AWS_SECRET_ACCESS_KEY <- as.character(keyTable$Secret.access.key)
#making the
Sys.setenv("AWS_ACCESS_KEY_ID" = "AWS_ACCESS_KEY_ID",
"AWS_SECRET_ACCESS_KEY" = "AWS_SECRET_ACCESS_KEY",
"AWS_DEFAULT_REGION" = "eu-west-1")
keyTable
# UPLOADING DATA TO S3
#--------------------------------------
#importing library
library(aws.s3)
#name of my bucket
bucket_name <- "sebair-selmani-1803425"
#putting the forecast-data.Rdata in the bucket
put_object("forecast-data.RData", bucket = bucket_name)
#putting the current data
put_object("current-data.RData", bucket = bucket_name)
The interesting part where I use Polly
# USING POLLY TO READ DATA
#--------------------------------------
#READING THE CURRENT WEATHER
library(aws.polly)
library(aws.signature)
response_tibble$
welcome<- synthesize("Welcome to my app user", voice = "Joanna")
# the temp is in kelvins
current_weather_Budapest <- synthesize(paste0("The current temprature in",as.character(desired_city[1]),"is",
as.character((response_tibble$temp[1])-273.15),"and the humidity is"
,as.character(response_tibble$humidity[1],"and the weather description",
response_tibble$weather_description[1],response_tibble$weather_main[1])),voice = "Ivy")
current_weather_Miskolc <- synthesize(paste0("The current temprature in",as.character(desired_city[2]),"is",
as.character((response_tibble$temp[2])-273.15),"and the humidity is"
,as.character(response_tibble$humidity[2],"and the weather description",
response_tibble$weather_description[2],response_tibble$weather_main[2])),voice = "Ivy")
#READING THE FORECAST FOR THE NEXT 3 HOURS, BUT I HAVE DATA FOR THE NEXT 9 HOURS
forecast_weather_Budapest <- synthesize(paste0("The weather in the next 3 hours in",
desirec_city[1],
" The tempature will be ",forecast_Budapest$temp[1],
"The humidity will be",forecast_Budapest$humidity[1],
"and the overall description will be"
,forecast_Budapest$weather_description),voice = "Ivy")
forecast_weather_Miskolc <- synthesize(paste0("The weather in the next 3 hours in",
desirec_city[2],
" The tempature will be ",forecast_Miskolc$temp[1],
"The humidity will be",forecast_Miskolc$humidity[1],
"and the overall description will be"
,forecast_Miskolc$weather_description),voice = "Ivy")
# PLAYING WITH AUDACIOUS PLAYER
library("tuneR")
setWavPlayer("audacious")
play(current_weather_Budapest)
play(current_weather_Miskolc)
play(forecast_weather_Budapest)
play(forecast_weather_Miskolc)
Getting news from Google news API and playing only the headlines
# GETTING NEWS DATA FROM GOOGLE NEWS API
#--------------------------------------
library(rvest)
library(jsonlite)
#my google news api key
API_KEY_NEWS <- read_file("API_NEWS")
# for getting the top news
url <- paste0("https://newsapi.org/v2/top-headlines?country=hu&apiKey=",API_KEY_NEWS)
data <- fromJSON(url_fake)
data$articles$title
#READING ONLY THE HEADLINES
lapply(data$articles$title
, function(inp){
title <- synthesize(inp,voice = "Ivy")
play(title)
})
goodbye <- synthesize("It was a pleasure to work with you. I wish you a happy day",voice = "Ivy")
play(goodbye)
Well we came to the end of the project. This is only a mini project that can be developed to a full mobile application, but I will leave that for the future.
Posted on November 15, 2019
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.