-
Notifications
You must be signed in to change notification settings - Fork 0
/
twitter_realtime_api.R
152 lines (111 loc) · 3.84 KB
/
twitter_realtime_api.R
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# Twitter API
library(rtweet)
# Interactive Maps
library(tmaptools)
library(leaflet)
# Core
library(tidyverse)
source("scripts/geocode_for_free.R")
token <- create_token(
# Replace with your Twitter app name
app = "Covid19twitteranalysis",
# Replace with your App API Key
consumer_key = "some_key",
# Replace with your App Secret Key
consumer_secret = "some_secret"
)
#search_tweets
search_tweets()
tweets_covid19 = rtweet::search_tweets(
q = "#COVID19", # Search query
n = 2000, # Number of results
lang = "en", # Language
include_rts = FALSE # Don't include retweets if want unique tweets
)
glimpse(tweets_covid19)
tweets_covid19 %>% write_rds(path = "tweets_covid19.rds")
#"data/frontline/tweets_covid19.rds")
# 1994 Tweets related to COVID, Downloaded on May 12, 2020
tweets_covid19 <- read_rds("tweets_covid19.rds")
# "data/frontline/tweets_covid19.rds")
#"C:\Users\adeol\Desktop\data\tweets_covid19.rds"
# 2.2 Results
tweets_covid19 %>% glimpse()
# User info
tweets_covid19 %>% slice(1:5) %>% select(screen_name, location, description)
# Tweet info
tweets_covid19 %>% slice(1:5) %>% select(text, url)
# Hashtags info
tweets_covid19 %>% slice(1:5) %>% select(hashtags) %>% unnest_wider(hashtags)
# URL's in the Tweet
tweets_covid19 %>% slice(1:5) %>% select(urls_expanded_url) %>% unnest(urls_expanded_url)
# - Real-time twitter action of what people are talking about
rt <- stream_tweets(timeout = 5)
rt %>% glimpse()
# Geocoding Coordinates
lookup_coords("london, uk") # Requires Google Maps API (Costs)
lookup_coords("usa") # Pre-programmed
# Geocoding Function
geocode_for_free("london, uk")
geocode_for_free("usa")
# Apply to streaming tweets
#rt <- stream_tweets(geocode_for_free("usa"), timeout = 5)
#rt
#rt %>% glimpse()
rt <- stream_tweets(geocode_for_free("london, uk"), timeout = 5)
rt
rt %>% glimpse()
# Apply to search tweets london
st <- search_tweets(
q = "#COVID19",
n = 500,
include_rts = FALSE,
lang = "en",
geocode = geocode_for_free("london, uk") %>% near_geocode(100)
)
st %>% glimpse()
st %>%
select(contains("coords")) %>%
unnest_wider(geo_coords) %>%
filter(!is.na(...1))
#Apply to search tweets usa
st_usa <- search_tweets(
q = "#COVID19",
n = 300,
include_rts = FALSE,
lang = "en",
geocode = geocode_for_free("usa") %>% near_geocode(100)
)
st_usa %>% glimpse()
st_usa %>%
select(contains("coords")) %>%
unnest_wider(geo_coords) %>%
filter(!is.na(...1))
# MAP
quakes[1:20,] %>%
leaflet() %>%
addTiles() %>%
addMarkers(~long, ~lat, popup = ~as.character(mag), label = ~as.character(mag))
quakes[1:20,]
# Mapping our Tweets
st %>%
select(screen_name, text, coords_coords) %>%
unnest_wider(coords_coords) %>%
filter(!is.na(...1)) %>%
set_names(c("screen_name", "text", "lon", "lat")) %>%
leaflet() %>%
addTiles() %>%
addMarkers(~lon, ~lat, popup = ~as.character(text), label = ~as.character(screen_name))
# Use a Circle to indicate location of tweets
data_prepared <- tibble(
location = geocode_for_free("london, uk") %>% near_geocode(100)
) %>%
separate(location, into = c("lat", "lon", "distance"), sep = ",", remove = FALSE) %>%
mutate(distance = distance %>% str_remove_all("[^0-9.-]")) %>%
mutate_at(.vars = vars(-location), as.numeric)
data_prepared %>%
leaflet() %>%
setView(data_prepared$lon, data_prepared$lat, zoom = 3) %>%
addTiles() %>%
addMarkers(~lon, ~lat, popup = ~as.character(location), label = ~as.character(location)) %>%
addCircles(lng = ~lon, lat = ~lat, weight = 1, radius = ~distance/0.000621371)