Learn R Programming

tweet2r (version 1.1)

tweet2r: Set up parameters to file streaming and store tweets in a JSON file using Twitter Streaming API .

Description

Configuration of the start stop for retrieving tweets, also set up the search by bounding box or by key words, number of tweets retrieved (stored in a collection of JSON files) and set up of the file prefix to store the JSON files created during the connection to the to Twitter Streaming API.tweet2r Don't opens a connection to Twitter's Streaming API by himself but you can configure the tweeter connection as is described in streamR that will return public statuses that match your keywords or bounding box definition. It is very useful to set up start time and end time of the connection. Note that the end time will be delayed depending on the time needed to close the file for the number of tweets set up.

Usage

tweet2r(t_start,t_end, ntweets=NULL,keywords=NULL, bbox=NULL, fileprefix,
          consumerKey,consumerSecret,requestURL,accessURL,authURL)

Arguments

t_start

Configuration of the start time to retrieve tweets. It use the strptime function

t_end

Configuration of the end time to stop retrieving tweets. It use the strptime function

ntweets

Number of tweets retrieved per file. The stream connection remains open, but the tweets are being stared in the files each time arrives to number of tweets configured. Nevertheless the number of tweets retrieved could be less, that depends on twitter API)

keywords

Vector with the keywords used to retrieve tweets. More information in twitter developers web https://dev.twitter.com/streaming/overview/request-parameters

bbox

Vector with the bounding box coordinates from which you want to retrieve tweets. You will retrieve geotagged tweets as well as tweets from users that has in his profile this area as a residence https://dev.twitter.com/streaming/overview/request-parameters

fileprefix

Fileprefix for the JSON file where the tweets will be stored. Avoid number at the begging this will be confusing for SQL database

consumerKey

Twitter API consumer key https://dev.twitter.com/oauth

consumerSecret

Twitter API consumer secret https://dev.twitter.com/oauth

requestURL

Twitter API url to request data https://dev.twitter.com

accessURL

Twitter API url to access https://dev.twitter.com

authURL

Twitter API authentification https://dev.twitter.com

See Also

tweet2r,parseTweets,streamR

Examples

Run this code
# NOT RUN {
# }
# NOT RUN {
	#Configuration fo twitter API connection
  #this could be avoid and use the default configuration 
	requestURL <- "https://api.twitter.com/oauth/request_token"
	accessURL <- "https://api.twitter.com/oauth/access_token"
	authURL <- "https://api.twitter.com/oauth/authorize"
	consumerKey <- sys.getenv("consumer_key")
	consumerSecret <- sys.getenv("consumer_secret")

	#definition of the start time and end time
	t_start<-"2015-09-11 9:45:00"
	t_end<-"2015-09-11 23:59:59"

	#definition of the file prefix
	fileprefix="tweets"
	key=c("keyword1", "keyword2")
  
  	#definition number of tweets per file
 	 number=3000

	#running the function
	tweet2r(t_start=t_start,t_end=t_end,ntweets=number,keywords=key,fileprefix = fileprefix,
		requestURL,accessURL,authURL,consumerKey,consumerSecret)

	#running the function using bbox
	#set up a bbox
	bbox=c(-0.1644,39.8485,0.6916,40.0034)

	tweet2r(t_start=t_start,t_end=t_end,ntweets=number,bbox=bbox,fileprefix = fileprefix,
		requestURL,accessURL,authURL,consumerKey,consumerSecret)
	
# }

Run the code above in your browser using DataLab