-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathFunctionsTwitterApi.R
53 lines (43 loc) · 1.67 KB
/
FunctionsTwitterApi.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
#install.packages("devtools", dependencies = TRUE)
#install.packages("rJava", depdendencies = TRUE)
library(devtools)
library(rJava)
get_twitter_data <- function (query_string, maxtweets=100){
#Start the JVM
.jinit('.')
.jaddClassPath(getoldtweets_path)
#For selecting a date range
#from_date = "2010-01-01"
#to_date = "2017-07-31"
old_wd <- getwd()
#Set the directory for creating the output file
setwd(getoldtweets_path)
command = "java -jar got.jar querysearch="
command = paste(command, query_string, sep = "", collapse = '')
#For a date range
#command = paste(command, " since=", sep = "", collapse = '')
#command = paste(command, from_date, sep = "", collapse = '')
#command = paste(command, " until=", sep = "", collapse = '')
#command = paste(command, to_date, sep = "", collapse = '')
#For testing purposes, only
command = paste(command, " maxtweets=", maxtweets, sep = "", collapse = '')
system(command)
#Get the data
csv_file = paste(getoldtweets_path, "/output_got.csv", sep = '', collapse = '')
my_data = read.csv(csv_file, sep=";", header=TRUE, quote="")
return_data_frame = data.frame()
#reset the old working directory
setwd(old_wd)
for (tweet in 1:nrow(my_data)){
temp <- data.frame(
AuthorName = my_data$username[tweet],
Title = my_data$text[tweet],
Date = my_data$date[tweet],
Cites = my_data$retweets[tweet],
Abstract = my_data$hashtags[tweet],
Id = my_data$id[tweet]
)
return_data_frame <- rbind(return_data_frame, temp)
}
return_data_frame
}