Created
March 24, 2014 16:31
-
-
Save tcash21/9743845 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(twitteR) | |
library(RMySQL) | |
mydb = dbConnect(MySQL(), user='username', password='password', dbname='tweets', host='hostname', port=3306) | |
load("OAuth.dat") | |
registerTwitterOAuth(my_oauth) | |
## Capture tweets within 150 mile radius of Boston that mention 'Patriots' | |
p<-tryCatch(searchTwitter("Patriots", geocode=c("42.368750,-71.055279,150mi"), n=750), error=function(e) print(e$message)) | |
ids<-unlist(lapply(p, function(x) x$id)) | |
sns<-unlist(lapply(p, function(x) x$screenName)) | |
tweets<-unlist(lapply(p, function(x) x$text)) | |
replyToSN<-as.character(lapply(p, function(x) x$replyToSN)) | |
replyToSN[which(replyToSN == "character(0)")] <- "" | |
created<- unlist(lapply(lapply(p, function(x) x$created), as.character)) | |
retweetCount<-unlist(lapply(p, function(x) x$retweetCount)) | |
pdf<-data.frame(id=ids, text=tweets, screenName=sns, replyToSN=replyToSN, created=created, retweetCount=retweetCount, team="Patriots") | |
## Capture tweets within 150 mile radius of Denver that mention 'Broncos' | |
b<-tryCatch(searchTwitter("Broncos", geocode=c("39.733842,-104.984665,150mi"), n=750), error=function(e) print(e$message)) | |
ids<-unlist(lapply(b, function(x) x$id)) | |
sns<-unlist(lapply(b, function(x) x$screenName)) | |
tweets<-unlist(lapply(b, function(x) x$text)) | |
replyToSN<-as.character(lapply(b, function(x) x$replyToSN)) | |
replyToSN[which(replyToSN == "character(0)")] <- "" | |
created<- unlist(lapply(lapply(b, function(x) x$created), as.character)) | |
retweetCount<-unlist(lapply(b, function(x) x$retweetCount)) | |
bdf<-data.frame(id=ids, text=tweets, screenName=sns, replyToSN=replyToSN, created=created, retweetCount=retweetCount, team="Broncos") | |
alldata <- rbind(pdf, bdf) | |
alldata$id<-as.character(alldata$id) | |
alldata$text <- as.character(alldata$text) | |
alldata$screenName <- as.character(alldata$screenName) | |
alldata$replyToSN <- as.character(alldata$replyToSN) | |
alldata$text <- gsub("\n", "", alldata$text) | |
alldata$text <- gsub("[^[:alnum:] ]", "", alldata$text) | |
alldata$screenName <- gsub("[^[:alnum:] ]", "", alldata$screenName) | |
## write tweets to MySQL database | |
dbWriteTable(mydb, "TCTest", alldata, append = T, row.names= F) | |
dbDisconnect(mydb) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment