library(rtweet)
#source("../keys.r")

twitter_token <- create_token(
  app = twitter_app,
  consumer_key = twitter_api_key,
  consumer_secret = twitter_api_secret_key,
  access_token = twitter_access_token,
  access_secret = twitter_access_token_secret
)


# Retrieving a topic
library(kableExtra)
library(dplyr)

#filter just to verified users with filter:verified, non verified with -filter:verified
search_tweets(q = "datascience filter:verified", n = 1)%>%
  select(screen_name, text) %>% 
  kable(format = "html") %>% 
  kable_styling("striped")
screen_name text
SpirosMargaris Companies which aren’t innovating, are not benefiting from the full power of artificial intelligence. To inject AI into a company, the goal must be continuous improvement in business results. Link &gt;&gt; https://t.co/l7KCjNX1Ej @ITLupdates @antgrasso via @LindaGrass0 #AI #DataScience https://t.co/Fm96FCufvz
#example 1 - no filter
search_tweets(q = "datascience", n = 1)%>%
  select(screen_name, text, created_at)%>% 
  kable(format = "html") %>% 
  kable_styling("striped")
screen_name text created_at
Consider_ations We asked “What, in your view and experience, is the single biggest barrier to accelerating speed of actionable data insight for core business processes today?” #data #dataandanalytics #dataculture #datascience #businessscience #businessintelligence #busin…https://t.co/gIGp0ooerM 2021-11-23 07:21:26
#example 2 - filter to verified user
search_tweets(q = "datascience filter:verified", n = 1)%>%
  select(screen_name, text)%>% 
  kable(format = "html") %>% 
  kable_styling("striped")
screen_name text
SpirosMargaris Companies which aren’t innovating, are not benefiting from the full power of artificial intelligence. To inject AI into a company, the goal must be continuous improvement in business results. Link &gt;&gt; https://t.co/l7KCjNX1Ej @ITLupdates @antgrasso via @LindaGrass0 #AI #DataScience https://t.co/Fm96FCufvz
#example 3 - filter to unverified user
search_tweets(q = "datascience -filter:verified", n = 1)%>%
  select(screen_name, text)%>% 
  kable(format = "html") %>% 
  kable_styling("striped")
screen_name text
Consider_ations We asked “What, in your view and experience, is the single biggest barrier to accelerating speed of actionable data insight for core business processes today?” #data #dataandanalytics #dataculture #datascience #businessscience #businessintelligence #busin…https://t.co/gIGp0ooerM
#aldi example 1
aldi<-search_tweets(q = "aldiaustralia", n = 100, include_rts = F)

# Retrieving User Data
#can use @therock or therock twitter handles. Doesn't matter

get_timeline("aldiaustralia", n = 3)%>% 
  select(screen_name, text)%>% 
  kable(format = "html") %>% 
  kable_styling("striped")
screen_name text
ALDIAustralia @StAliaoftheKni1 Once a new on sale date has been confirmed it will be listed here https://t.co/7ax18DwsZK under VIC, then Products Now Available. Apologies for the inconvenience caused.
ALDIAustralia @ComradePurrski It should be available in all ALDI Liquor stores, we recommend asking the team next time you’re in store and they should be able to help. Thanks!
ALDIAustralia @roberta_chahoud We’re sorry to hear that, please return any leftover packaging or receipt to your local ALDI for a refund or replacement. Apologies for the inconvenience caused.
# EXAMPLE 1

rock = get_timeline("therock", n =100)

library(scales)
library(ggplot2)

ggplot(data = rock)+
  geom_point(aes(x = favorite_count, # how many favorited 
                 y = retweet_count,  # how many retweeted
                 color = is_quote    # is the rock quoting?
  )
  )+ 
  theme_minimal()+
  theme(legend.position = "bottom")+
  # adding the right title and legend label
  labs(title = "What's the Rock Cooking?", 
       color = "Is the Rock Quoting?")+
  # using comma from the scales package for the axis labels
  scale_y_continuous("Retweets", labels = comma)+ 
  scale_x_continuous("Favorites", labels = comma)

# EXAMPLE 2

data = search_tweets("#rstats", # include the topic you are searching for 
                     type  = "popular", # lets return the 'popular' tweets
                     include_rts = F) # no retweets pls.
data$text
## [1] "If you click on the R hex icon in @rstudio's Files pane, it brings you back to the top-level folder of your Project, if you've navigated away. I use this all the time and I want to make sure you know about it. #rstats https://t.co/7NcbXyMZzs"
## [2] "70 Free Online Courses for #DataScience to Advance Your Skills in 2021: https://t.co/h5exMoaa5s via @tut_ml \n————\n#BigData #Analytics #MachineLearning #DeepLearning #AI #DataScientists #Statistics #DataLiteracy #Python #RStats"             
## [3] "Why no gaR package in #Rstats? https://t.co/GerCU5QDFA"
text = gsub("(?:\\s*#\\w+)*", "", data$text) # remove all the hashtags at the end
text = gsub("\n|—", "", text) # remove all the line breaks
text = gsub(" ?(f|ht)(tp)(s?)(://)(.*)[.|/](.*)", "", text) # remove urls

text
## [1] "If you click on the R hex icon in @rstudio's Files pane, it brings you back to the top-level folder of your Project, if you've navigated away. I use this all the time and I want to make sure you know about it."
## [2] "70 Free Online Courses for to Advance Your Skills in 2021:"                                                                                                                                                       
## [3] "Why no gaR package in?"