here's my code:
library(RCurl)
library(twitteR)
library(ROAuth)
#run the libraries ctrl+r
api_key<-"3snovyV8bNn8jVWu93UNP61rd"
api_secret<-"1U3crdPY2kS4Nx12OaxjlE2geyDFSzJXJnVSJaEvIUV3LqHdCa"
access_token<-"2457340967-8wOYs9X7m66RzywCtT6KHlow6hP8zTNpSMTid7Y"
access_token_secret<-"gTqLbHGPayMgF3FG0o0tb42yBWd3TiCZ67vhXOfnLICZT"
setup_twitter_oauth(api_key,api_secret,access_token,access_token_secret)
#ctrl+r
searchTwitter("Cristiano Ronaldo", n=25, lang="en", since=NULL,
until=NULL,
locale=NULL, geocode=NULL, sinceID=NULL, maxID=NULL,
resultType=NULL, retryOnRateLimit=120)
#ctrl+r
Rtweets(n=25, lang="en", since=NULL)
I'm looking for a command in order to specify the tweets I want to extract by account (i.e. Cristiano Ronaldo's official Twitter account).
Thanks!!!
I've used the smappR package's function getTimeline to get tweets from a single user account.
You'll need to install smappR from GitHub, if you haven't done so previously.
library(devtools)
install_github("SMAPPNYU/smappR")
You could then use the getTimeline function to get Ronaldo's tweets:
getTimeline(screen_name = "Cristiano",
filename = "ronaldo_tweets.json", # where tweets will be stored
n=500, ## number of tweets to download (max is 3,200)
oauth_folder = "~/Dropbox/credentials")
The screen_name argument refers to the user's twitter handle. Note also that smappR assumes that you have one or more OAuth objects in a folder. You can read about their setup here: https://github.com/SMAPPNYU/smappR
I hope that this helps.
Related
I have a problem in scraping filtered followers in twitter using R.
To be specific, i'd like to scrape twitter followers
who have tweets more than 1,
who mentioned certain keywords in their tweets,
who retweets certain users(people)'s tweets
among A followers(A just for an example).
Below are packages i used. Spaces are to block my private API keys.
install.packages("devtools")
library(devtools)
install_github("mkearney/rtweet")
install.packages("rtweet")
library(rtweet)
api_key <-
api_secret_key <-
access_token <-
access_token_secret <-
mytoken<-create_token(app = "", api_key,api_secret_key, access_token, access_token_secret, set_renv = FALSE)
Codes I used to scrape twitter followers who follow certain twitter users are followed.
(FoxNews is only for the purpose to illustrate an example)
FoxNews.followers<-get_followers(user="FoxNews",n=75000)
FoxNews.followers.data<-lookup_users(users=FoxNews.followers$user_id)
df.FoxNews<-data.frame(FoxNews.followers)
What i need is the list of filtered followers(conditions 1,2,3 above) among <FoxNews.followers>.
I think I can get them if i put some codes in mine but i'm not sure what to put as well as where to put.
I'd so appreciate any advice.
Thanks.
I can’t get the SpotifyR package to extract songs which have a specific title using the search_spotify command
I have read the Spotify developer page and the Spotifyr package readme.
Reproducible example below:
library(spotifyr)
Sys.setenv(SPOTIFY_CLIENT_ID = ‘myID’)
Sys.setenv(SPOTIFY_CLIENT_SECRET = ‘myCLIENTSECRET’)
access_token <- get_spotify_access_token()
searchresults <- search_spotify('Zooropa','track')
I would expect the results of this to be the Spotify tracks with the title “Zooropa”. This should be 7 results due to the presence of karaoke and tribute songs which include Zooropa in the track title. Instead the results are 16 observations, including every one of the 10 tracks on the Zooropa album, even those not called Zooropa (e.g. Lemon and Babyface).
Since I am searching in the ‘track’ field I don’t understand why I get the extra 9 results.
The spotify api returns the track id's for all of the search results. Zooropa is also an album as well as a song. If you search "Zooropa" inside of Spotify there will be more than 1 result.
Having researched this I have discovered that the spotifyr package does not allow the same search options as the underlying Spotify API. When using the search command in the Spotify API the "type" parameter allows the search to be restricted to tracks (see here). This type parameter cannot currently be used in spotifyr.
However, I have created a workaround which might be useful to others and the code for this is below. Stringdist is used because after I have extracted the data using spotifyr, I need to identify which track names are closest to "Zooropa". I use the jw method but others could be used.
library(spotifyr)
library(stringdist)
Sys.setenv(SPOTIFY_CLIENT_ID = 'yourID')
Sys.setenv(SPOTIFY_CLIENT_SECRET = 'yourclientsecret')
access_token <- get_spotify_access_token()
searchresults <- search_spotify('Zooropa','track')
artists <-searchresults$artists
artists2 <-lapply(artists, function(x) x[,3])
artists3 <-lapply(artists2, function(x) paste(x, collapse=', '))
searchresults$realartist <-as.data.frame(unlist(artists3))
usefuloutput <-cbind(searchresults$id,searchresults$name,searchresults$realartist)
colnames(usefuloutput) <- c("Spotifyid", "Spotifyname","Spotifyartist")
usefuloutput$titlecomparison <-stringdist(usefuloutput[,2],'Zooropa',method="jw")
I am using rtweet package in R to extract tweets of particular hashtag which basically needs appname,api_key,api_secret,access_token,access_token_secret.
So i have created an app in Twitter to get all the above details. And then I pass the above as follows
twitter_token <- create_token(
app = appname,
consumer_key = api_key,
consumer_secret = api_secret)
tweets <-search_tweets("#iPhone8", n=1000, include_rts = FALSE, type = "recent",lang = "en")
So this will basically extract tweets with 88 variables. Now I want to write these tweets to an excel file for further analysis.
And for that I am using the below code.
tweets.df <-twListToDF(tweets)
When I run this I am getting the following error.
Error in twListToDF(tweets) :
Not all elements of twList are of the same class
Just want to understand what could be the potential issue here.
Problem is I cannot put the exact code here as that needs my api key and all.
Any help would be really appreciated.
Regards,
Akash
You can as well use this
tweets.df <-as.data.frame(tweets)
The answer to the question located here does not seem to work anymore: Obtaining twitter screen names from a twitter list
Does anyone know if the twitteR package has been changed since it was answered in 2015. If so, is there a way to download the members of a public list in the current version?
Here's the previous answer's code updated to include a current lists. Requires a Twitter API authorization. It now returns a list of length 0 when it should have a list of the 20 Premier League club names.
library(rjson)
library(httr)
library(twitteR)
twlist <- "premier-league-clubs"
twowner <- "TwitterUK"
api.url <- paste0("https://api.twitter.com/1.1/lists/members.json?slug=",
twlist, "&owner_screen_name=", twowner, "&count=5000")
response <- POST(api.url, config(token=twitteR:::get_oauth_sig()))
response.list <- fromJSON(content(response, as = "text", encoding = "UTF-8"))
users.names <- sapply(response.list$users, function(i) i$name)
users.screennames <- sapply(response.list$users, function(i) i$screen_name)
head(users.names)
The author of the package in his github account mentions that the twitteR package is deprecated in favor or rtweet. Probably you have to take a look to the documentation of the rtweet package.
Swapping from POST to GET when making the request seems to work for lists I am retrieving.
I would like to retrieve a list of tweets from Twitter for a given hashtag using package RJSONIO in R. I think I am pretty close to the solution, but I seem to miss one step.
My code reads as follows (in this example, I use #NBA as a hashtag):
library(httr)
library(RJSONIO)
# 1. Find OAuth settings for twitter:
# https://dev.twitter.com/docs/auth/oauth
oauth_endpoints("twitter")
# Replace key and secret below
myapp <- oauth_app("twitter",
key = "XXXXXXXXXXXXXXX",
secret = "YYYYYYYYYYYYYYYYY"
)
# 3. Get OAuth credentials
twitter_token <- oauth1.0_token(oauth_endpoints("twitter"), myapp)
# 4. Use API
req=GET("https://api.twitter.com/1.1/search/tweets.json?q=%23NBA&src=typd",
config(token = twitter_token))
req <- content(req, as = "text")
response=fromJSON(req)
How can I get the list of tweets from object 'response'?
Eventually, I would like to get something like:
searchTwitter("#NBA", n=5000, lang="en")
Thanks a lot in advance!
The response object should be a list of length two: statuses and metadata. So, for example, to get the text of the first tweet, try:
response$statuses[[1]]$text
However, there are a couple of R packages designed to make just this kind of thing easier: Try streamR for the streaming API, and twitteR for the REST API. The latter has a searchTwitter function exactly as you describe.