Skip to content

Instantly share code, notes, and snippets.

@ElCep
Last active January 3, 2016 08:49
Show Gist options
  • Save ElCep/8438475 to your computer and use it in GitHub Desktop.
Save ElCep/8438475 to your computer and use it in GitHub Desktop.
get twittes with twitteR
#Autors F.Zottele (Fondazion E.Mach (IT)) and E.DELAY (GEOLAB, université de Limoges (FR))
#Script for download 45 twittes with twitter API for dev
rm(list=ls())
setwd("~/git/datablogvinometro/twitteR/data/")
library(twitteR)
library(lubridate)
library(ggmap)
endDay = today()
startDay = endDay - 1*weeks()
load("../cred.RData")
registerTwitterOAuth(twitCred)
get twittes from twitter
tweets = searchTwitter("geotribu", n=45, since=as.character(startDay), until=as.character(endDay))
tweets = twListToDF(tweets)
userInfo = lookupUsers(tweets$screenName)
userFrame = twListToDF(userInfo)
locatedUsers = !is.na(userFrame$location)
userFrame = userFrame[locatedUsers,]
#if a twitte is not localised try to find a localisation en geocadage from acount
dataToMap <- data.frame()
for (i in 1:length(tweets[,1])) {
if (!is.na(tweets$longitude[i])) {
dataToMap = rbind(dataToMap,data.frame(date=tweets$created[i],lon=as.numeric(tweets$longitude[i]), lat=as.numeric(tweets$latitude[i])))
} else {
user = tweets$screenName[i]
userIDX = which(userFrame$screenName==user)
if (length(userIDX) > 0) {
location = geocode(userFrame$location[userIDX],override_limit=TRUE,messaging=FALSE)
dataToMap = rbind(dataToMap,data.frame(date=tweets$created[i],lon=location$lon[1], lat=location$lat[1]))
Sys.sleep(1.5)
}
}
}
write.csv(dataToMap,paste("csv/data",Sys.time(),".csv",sep=""))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment