-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathWeatherVane.R
More file actions
125 lines (107 loc) · 3.09 KB
/
WeatherVane.R
File metadata and controls
125 lines (107 loc) · 3.09 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
# Load required packages
library(rtweet)
library(tidytext)
library(httpuv)
library(dplyr)
library(tibble)
library(stringi)
# Set weather word mappings
keywords <- c('sunny',
'sunshine',
'partly',
'cloudy',
'overcast',
'rainy',
'rain',
'raining',
'showers',
'snow',
'snowing',
'snowy',
'windy',
'foggy',
'thunder and lighting')
types <- c('Sunny',
'Sunny',
'Partly Cloudy',
'Cloudy',
'Overcast',
'Rainy',
'Rainy',
'Rainy',
'Showers',
'Snowy',
'Snowy',
'Snowy',
'Windy',
'Foggy',
'Thunder and Lighting'
)
weatherkey <- tibble(keywords, types)
# Set up access to bot account
auth <- rtweet_bot(api_key = Sys.getenv('WEATHER_VANE_BOT_API_KEY'),
api_secret = Sys.getenv('WEATHER_VANE_BOT_API_SECRET'),
access_token = Sys.getenv('WEATHER_VANE_BOT_ACCESS_KEY'),
access_secret = Sys.getenv('WEATHER_VANE_BOT_ACCESS_SECRET'))
auth_as(auth)
# Pull tweets related to weather and are from Sacramento, CA area
t <- search_tweets(q = 'weather', n = 50, type = 'recent', include_rts = F, geocode = '38.575764,-121.478851,25mi')
# Get today's date and format
today <- Sys.Date()
textdate <- format(today, '%A, %B%e') %>% tolower() %>% stri_replace( '', fixed = ',')
# Pull tweets and analyze to find the weather
weather <- t %>%
tibble() %>%
unnest_tweets(word, full_text, drop = FALSE) %>%
unnest_ngrams(grams, full_text, drop = FALSE) %>%
group_by(id_str) %>%
filter(any(word == 'today') | any(grams == textdate)) %>%
filter(strftime(created_at, format = '%Y-%m-%d') == Sys.Date()) %>%
ungroup() %>%
mutate(word = ifelse('partly' %in% word & word == 'cloudy',
'partly',
word)) %>%
inner_join(weatherkey, by = c('word' = 'keywords'), keep = T)
# Find and format high temps
highs <- weather %>%
filter(grepl('^high of', grams)) %>%
select(grams) %>%
unnest_tokens(char, grams) %>%
filter(grepl('^[0-9]', char)) %>%
deframe() %>%
stri_replace_all_regex('f', '') %>%
unique() %>%
as.numeric() %>%
sort() %>%
as.character() %>%
stri_c('f') %>%
stri_c(collapse = ' or ')
# Find and format low temps
lows <- weather %>%
filter(grepl('^low of', grams)) %>%
select(grams) %>%
unnest_tokens(char, grams) %>%
filter(grepl('^[0-9]', char)) %>%
deframe() %>%
stri_replace_all_regex('f', '') %>%
unique() %>%
as.numeric() %>%
sort() %>%
as.character() %>%
stri_c('f') %>%
stri_c(collapse = ' or ')
# Generate message
message <- weather %>%
select(types) %>%
unique() %>%
deframe() %>%
stri_c( collapse = ' or ') %>%
paste0('Twitter says Sacramento area\'s weather today is ',
.,
', with possible highs of ',
highs,
' and possible lows of ',
lows,
'.')
# Post tweet
post_tweet(status = message)