library(twitteR)
## Warning: package 'twitteR' was built under R version 3.2.5
library(dplyr)
## Warning: package 'dplyr' was built under R version 3.2.5
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:twitteR':
##
## id, location
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
library(lubridate)
## Warning: package 'lubridate' was built under R version 3.2.5
##
## Attaching package: 'lubridate'
## The following object is masked from 'package:base':
##
## date
library(ggplot2)
## Warning: package 'ggplot2' was built under R version 3.2.5
# dostęp do API
consumer_key <- "xxxxxxxx"
consumer_secret <- "xxxxxxxx"
access_token <- "xxxxxxxx"
access_secret <- "xxxxxxxx"
setup_twitter_oauth(consumer_key, consumer_secret, access_token, access_secret)
## [1] "Using direct authentication"
## top 5 trendów - pobranie wszysckich tweetów
woeid <- availableTrendLocations() %>% filter(country=="Poland")
rownames(woeid) <- woeid$name
# trendy, ale tylko hashtagi
trends <- getTrends(woeid["Poland",3]) %>% filter(substr(name, 1, 1) == "#")
trend_tweets <- data.frame()
for(i in 1:5) {
trend <- trends[i,1] # najpopularniejszy temat
print(trend)
trend.tweets <- searchTwitter(trend, n=1000, resultType="recent")
trend.tweets.df <- twListToDF(trend.tweets) %>% select(screenName, text, created, retweetCount, favoriteCount, isRetweet, id)
trend.tweets.df$trend <- trend
trend_tweets <- rbind(trend_tweets, trend.tweets.df)
}
## [1] "#blackfriday"
## [1] "#ThankYou2NE1"
## [1] "#RZECZoPOLITYCE"
## Warning in doRppAPICall("search/tweets", n, params = params,
## retryOnRateLimit = retryOnRateLimit, : 1000 tweets were requested but the
## API can only return 232
## [1] "#mgła"
## Warning in doRppAPICall("search/tweets", n, params = params,
## retryOnRateLimit = retryOnRateLimit, : 1000 tweets were requested but the
## API can only return 146
## [1] "#dubchallenge"
rm(woeid, trend, trend.tweets, trend.tweets.df)
trend_tweets <- unique(trend_tweets)
trend_tweets <- trend_tweets %>%
filter(substr(trend_tweets$text, 1, 3) != "RT ") %>% # bez RT
filter(screenName != "trendinaliaPL") %>% # bez tego bota
mutate(value=retweetCount+favoriteCount)
trend_tweets <- trend_tweets %>%
filter(created >= Sys.time() - dhours(6)) # ostatnie 6h
# najpopulatniejszew twity per tag:
for(i in 1:5) {
trend_h <- trends[i,1] # najpopularniejszy temat
t <- trend_tweets %>%
filter(trend==trend_h) %>%
arrange(desc(value)) %>%
top_n(1, value) %>%
select(screenName, id, text, retweetCount, favoriteCount)
twit <- paste0("Top-tweet (RTs=", t[1,4], ", FAVs=", t[1,5], ", TOT=", t[1,4]+t[1,5], ") w ostatnich 6h z top-trendu ", trend_h, "\nhttps://twitter.com/", t[1,1], "/status/", t[1,2])
# tweet(twit)
cat(twit)
cat(paste0("\nZnaków: ", nchar(twit)))
cat("\n\n")
cat(paste("Kto:", t[1,1], "\n"))
cat(paste("Co: ", t[1,3], "\n"))
cat(paste0("RTs=", t[1,4], ", FAVs=", t[1,5], ", TOT=", t[1,4]+t[1,5], "\n"))
cat("\n==============\n\n")
}
## Top-tweet (RTs=9, FAVs=23, TOT=32) w ostatnich 6h z top-trendu #blackfriday
## https://twitter.com/MichelleMone/status/802098969010642945
## Znaków: 134
##
## Kto: MichelleMone
## Co: Off to #Telford for my speech @SocialSuperStr is going crazy with #BlackFriday deals. Don't miss out… https://t.co/rcD2tK0G94
## RTs=9, FAVs=23, TOT=32
##
## ==============
##
## Top-tweet (RTs=1, FAVs=2, TOT=3) w ostatnich 6h z top-trendu #ThankYou2NE1
## https://twitter.com/Unreliable_liar/status/802098991869595648
## Znaków: 136
##
## Kto: Unreliable_liar
## Co: Preach this. I'm so tired of these fluffy rainbows AND pastel concepts #ThankYou2NE1 https://t.co/2GUEPXdrd9
## RTs=1, FAVs=2, TOT=3
##
## ==============
##
## Top-tweet (RTs=4, FAVs=5, TOT=9) w ostatnich 6h z top-trendu #RZECZoPOLITYCE
## https://twitter.com/rzeczpospolita/status/802076716927688704
## Znaków: 137
##
## Kto: rzeczpospolita
## Co: Brejza: Wassermann nie mówiła prawdy
## #RZECZoPOLITYCE
## https://t.co/owHU9CH7sX
## RTs=4, FAVs=5, TOT=9
##
## ==============
##
## Top-tweet (RTs=3, FAVs=9, TOT=12) w ostatnich 6h z top-trendu #mgła
## https://twitter.com/leskiewicz/status/802014535993458688
## Znaków: 124
##
## Kto: leskiewicz
## Co: #mgła w Warszawie - słabo się jeździ. Wyjdźcie wcześniej, żeby nie gnać na wariata, bo mało co widać
## RTs=3, FAVs=9, TOT=12
##
## ==============
##
## Top-tweet (RTs=1, FAVs=2, TOT=3) w ostatnich 6h z top-trendu #dubchallenge
## https://twitter.com/EGGSAVIERS/status/802098970885431296
## Znaków: 131
##
## Kto: EGGSAVIERS
## Co: #dubchallenge the challenge for me is to get prettier ;-; https://t.co/66g3OssgAu
## RTs=1, FAVs=2, TOT=3
##
## ==============
# kto pisze najwięcej w trendach?
trend_tweets %>%
group_by(trend, screenName) %>%
summarise(n=n()) %>%
top_n(2, n) %>%
ungroup() %>%
arrange(trend, desc(n)) %>%
mutate(r=row_number()) %>%
mutate(screenName = factor(screenName, levels=rev(screenName))) %>%
ggplot() +
geom_bar(aes(screenName, n, fill=trend), stat="identity") +
coord_flip() +
theme_minimal()

# który trend najpopularniejszy?
trend_tweets %>%
group_by(trend) %>%
summarise(n=n()) %>%
ungroup() %>%
arrange(desc(n)) %>%
mutate(trend=factor(trend, levels = trend)) %>%
ggplot() +
geom_bar(aes(trend, n, fill=trend), stat="identity") +
theme_minimal()

trend_tweets %>%
mutate(hour=hour(created)) %>%
group_by(trend, hour) %>%
summarise(n=n()) %>%
ungroup() %>%
ggplot(aes(hour, n, color=trend)) +
geom_line() +
theme_minimal()
