Collecting, Analyzing and Presenting data about the participation in #ilovefs day
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

collecto.R 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  1. ################################################################################
  2. # collecto.R #
  3. # Collecting data from different social media sources with a specified #
  4. # searchterm/criteria #
  5. # Jan Weymeirsch janwey@fsfe.org #
  6. ################################################################################
  7. ### Loading Packages {{{ ----
  8. #### Twitter
  9. install.packages("twitteR")
  10. library("twitteR")
  11. # had to install "httr" via packagemanager
  12. #### Fediverse (eg: mastodon)
  13. install.packages("curl")
  14. library("curl")
  15. install.packages("rjson")
  16. library(rjson)
  17. ### Reddit
  18. install.packages("RedditExtractoR")
  19. library("RedditExtractoR")
  20. # }}}
  21. ## Twitter Collector {{{ ----
  22. ### Authenticate to Twitter
  23. #### Manual input (uncomment if needed)
  24. #twitter_consumerkey <- readline("[Twitter] Enter your consumer API key.")
  25. #twitter_consumerpri <- readline("[Twitter] Enter your consumer API secret.")
  26. #twitter_tokenaccess <- readline("[Twitter] Enter your Access Token.")
  27. #twitter_tokensecret <- readline("[Twitter] Enter your Token Secret.")
  28. #### Saved credentials
  29. twitter_api_cred <- read.table(file = "./twitter_api.txt", header = TRUE, sep = ";")
  30. twitter_consumerkey <- as.character(twitter_api_cred$consumer_key)
  31. twitter_consumerpri <- as.character(twitter_api_cred$consumer_private)
  32. twitter_tokenaccess <- as.character(twitter_api_cred$access_token)
  33. twitter_tokensecret <- as.character(twitter_api_cred$token_secret)
  34. setup_twitter_oauth(consumer_key = twitter_consumerkey,
  35. consumer_secret = twitter_consumerpri,
  36. access_token = twitter_tokenaccess,
  37. access_secret = twitter_tokensecret)
  38. # Note -------------------------------------------------------------------------
  39. # Please refer to the Documentation on where to receive your API credentials.
  40. # ------------------------------------------------------------------------------
  41. ### Collecting Tweets
  42. twitter_tw_dirty <- searchTwitter(search = "ilovefs",
  43. since = "2018-02-10",
  44. until = "2018-12-31",
  45. n = 9999,
  46. resultType = "recent")
  47. ### strip off retweets
  48. twitter_tw <- strip_retweets(tweets = twitter_tw_dirty,
  49. strip_manual = FALSE,
  50. strip_mt = FALSE)
  51. ### Extract relevant data from dataset
  52. twitter_timedate <- c()
  53. twitter_client <- c()
  54. twitter_name <- c()
  55. twitter_rts <- c()
  56. twitter_fav <- c()
  57. twitter_url <- c()
  58. twitter_txt <- c()
  59. twitter_usr <- c()
  60. for(i in 1:length(twitter_tw)){
  61. #### Time of tweet
  62. if(length(twitter_tw[[i]]$created) > 0){
  63. twitter_timedate[i] <- as.character(twitter_tw[[i]]$created)
  64. } else {
  65. # insert empty value, if it does not exist
  66. twitter_timedate[i] <- NA
  67. }
  68. #### Client used
  69. if(length(twitter_tw[[i]]$statusSource) > 0){
  70. twitter_client[i] <- as.character(twitter_tw[[i]]$statusSource)
  71. } else {
  72. # insert empty value, if it does not exist
  73. twitter_client[i] <- NA
  74. }
  75. #### Screen names / Twitter Handles
  76. if(length(twitter_tw[[i]]$screenName) > 0){
  77. twitter_name[i] <- as.character(twitter_tw[[i]]$screenName)
  78. } else {
  79. # insert empty value, if it does not exist
  80. twitter_name[i] <- NA
  81. }
  82. #### Number of retweets
  83. if(length(twitter_tw[[i]]$retweetCount) > 0){
  84. twitter_rts[i] <- as.character(twitter_tw[[i]]$retweetCount)
  85. } else {
  86. # insert empty value, if it does not exist
  87. twitter_rts[i] <- NA
  88. }
  89. #### Number of favorites
  90. if(length(twitter_tw[[i]]$favoriteCount) > 0){
  91. twitter_fav[i] <- as.character(twitter_tw[[i]]$favoriteCount)
  92. } else {
  93. # insert empty value, if it does not exist
  94. twitter_fav[i] <- NA
  95. }
  96. #### URLs posted about
  97. if(length(twitter_tw[[i]]$urls$expanded_url) > 0){
  98. twitter_url[i] <- as.character(twitter_tw[[i]]$urls$expanded_url)
  99. } else {
  100. # insert empty value, if it does not exist
  101. twitter_url[i] <- NA
  102. }
  103. #### actual tweet/text
  104. if(length(twitter_tw[[i]]$text) > 0){
  105. twitter_txt[i] <- as.character(twitter_tw[[i]]$text)
  106. } else {
  107. # insert empty value, if it does not exist
  108. twitter_txt[i] <- NA
  109. }
  110. #### poster of tweet
  111. if(length(twitter_tw[[i]]$screenName) > 0){
  112. twitter_usr[i] <- as.character(twitter_tw[[i]]$screenName)
  113. } else {
  114. # insert empty value, if it does not exist
  115. twitter_usr[i] <- NA
  116. }
  117. }
  118. ### Removing HTML-Tags from Client-info
  119. twitter_client <- sub(pattern = ".*\">", replace = "", x = twitter_client)
  120. twitter_client <- sub(pattern = "</a>", replace = "", x = twitter_client)
  121. ### Forming variables for dataframe
  122. time <- sub(pattern = ".* ", x = twitter_timedate, replace = "")
  123. time <- gsub(pattern = ":", x = time, replace = "")
  124. date <- sub(pattern = " .*", x = twitter_timedate, replace = "")
  125. date <- gsub(pattern = "-", x = date, replace = "")
  126. fdat <- strptime(x = paste0(date, time), format = "%Y%m%d%H%M%S", tz = "CET")
  127. retw <- as.factor(twitter_rts)
  128. favs <- as.factor(twitter_fav)
  129. link <- as.character(twitter_url)
  130. text <- as.character(twitter_txt)
  131. clnt <- as.character(twitter_client)
  132. user <- as.character(twitter_usr)
  133. ### Creating dataframe
  134. twitter <- data.frame(cbind(date, time, fdat, retw, favs, text, link, clnt, user))
  135. #### Clean-Up
  136. rm(list = c("date", "time", "fdat", "retw", "favs", "text", "link", "clnt", "user"))
  137. twitter <- within(data = twitter, expr = {
  138. date <- as.character(date);
  139. time <- as.character(time);
  140. fdat <- as.character(fdat);
  141. text <- as.character(text);
  142. link <- as.character(link);
  143. })
  144. # }}}
  145. ## Mastodon Collector with curl {{{ ----
  146. mastodon.fetchdata <- function(data){
  147. tmp_datetime <- c()
  148. tmp_lang <- c()
  149. tmp_inst <- c()
  150. tmp_link <- c()
  151. tmp_text <- c()
  152. tmp_reto <- c()
  153. tmp_favs <- c()
  154. tmp_murl <- c()
  155. tmp_acct <- c()
  156. for(i in 1:length(data)){
  157. #### Time and Date of Toot
  158. if(length(data[[i]]$created_at) > 0){
  159. tmp_datetime[i] <- data[[i]]$created_at
  160. } else {
  161. # insert empty value, if it does not exist
  162. tmp_datetime[i] <- NA
  163. }
  164. #### Language of Toot
  165. if(length(data[[i]]$language) > 0){
  166. tmp_lang[i] <- data[[i]]$language
  167. } else {
  168. # insert empty value, if it does not exist
  169. tmp_lang[i] <- NA
  170. }
  171. #### Instance of Toot
  172. if(length(data[[i]]$uri) > 0){
  173. tmp_inst[i] <- data[[i]]$uri
  174. } else {
  175. # insert empty value, if it does not exist
  176. tmp_inst[i] <- NA
  177. }
  178. #### URL of Toot
  179. if(length(data[[i]]$url) > 0){
  180. tmp_link[i] <- data[[i]]$url
  181. } else {
  182. # insert empty value, if it does not exist
  183. tmp_link[i] <- NA
  184. }
  185. #### Text/Content of Toot
  186. if(length(data[[i]]$content) > 0){
  187. tmp_text[i] <- data[[i]]$content
  188. } else {
  189. # insert empty value, if it does not exist
  190. tmp_text[i] <- NA
  191. }
  192. #### Number of Retoots
  193. if(length(data[[i]]$reblogs_count) > 0){
  194. tmp_reto[i] <- data[[i]]$reblogs_count
  195. } else {
  196. # insert empty value, if it does not exist
  197. tmp_reto[i] <- NA
  198. }
  199. #### Number of Favorites
  200. if(length(data[[i]]$favourites_count) > 0){
  201. tmp_favs[i] <- data[[i]]$favourites_count
  202. } else {
  203. # insert empty value, if it does not exist
  204. tmp_favs[i] <- NA
  205. }
  206. #### Number of Favorites
  207. if(length(data[[i]]$media_attachments) > 0){
  208. tmp_murl[i] <- data[[i]]$media_attachments[[1]]$url
  209. } else {
  210. # insert empty value, if it does not exist
  211. tmp_murl[i] <- NA
  212. }
  213. #### Account of Tooter
  214. if(length(data[[i]]$account) > 0){
  215. tmp_acct[i] <- data[[i]]$account$acct
  216. } else {
  217. # insert empty value, if it does not exist
  218. tmp_acct[i] <- NA
  219. }
  220. }
  221. return(data.frame(cbind(tmp_datetime,
  222. tmp_lang,
  223. tmp_inst,
  224. tmp_text,
  225. tmp_link,
  226. tmp_reto,
  227. tmp_favs,
  228. tmp_murl,
  229. tmp_acct)))
  230. }
  231. datetime <- c()
  232. lang <- c()
  233. inst <- c()
  234. link <- c()
  235. text <- c()
  236. reto <- c()
  237. favs <- c()
  238. murl <- c()
  239. acct <- c()
  240. for(i in 1:999){
  241. if(i == 1){
  242. mastodon_instance <- "https://mastodon.social"
  243. mastodon_hashtag <- "ilovefs"
  244. mastodon_url <- paste0(mastodon_instance,
  245. "/api/v1/timelines/tag/",
  246. mastodon_hashtag,
  247. "?limit=40")
  248. } else {
  249. mastodon_lheader <- parse_headers(mastodon_reqres$headers)[11]
  250. mastodon_next <- sub(x = mastodon_lheader, pattern = ".*link:\ <", replace = "")
  251. mastodon_url <- sub(x = mastodon_next, pattern = ">;\ rel=\"next\".*", replace = "")
  252. }
  253. mastodon_reqres <- curl_fetch_memory(mastodon_url)
  254. mastodon_rawjson <- rawToChar(mastodon_reqres$content)
  255. toots <- fromJSON(mastodon_rawjson)
  256. if(length(toots) > 0){
  257. tmp_mastodon_df <- mastodon.fetchdata(data = toots)
  258. datetime <- c(datetime, as.character(tmp_mastodon_df$tmp_datetime))
  259. lang <- c(lang, as.character(tmp_mastodon_df$tmp_lang))
  260. inst <- c(inst, as.character(tmp_mastodon_df$tmp_inst))
  261. link <- c(link, as.character(tmp_mastodon_df$tmp_link))
  262. text <- c(text, as.character(tmp_mastodon_df$tmp_text))
  263. reto <- c(reto, as.character(tmp_mastodon_df$tmp_reto))
  264. favs <- c(favs, as.character(tmp_mastodon_df$tmp_favs))
  265. murl <- c(murl, as.character(tmp_mastodon_df$tmp_murl))
  266. acct <- c(acct, as.character(tmp_mastodon_df$tmp_acct))
  267. } else {
  268. break
  269. }
  270. }
  271. ### Time of post
  272. #### date
  273. date <- sub(pattern = "T.*", x = datetime, replacement = "")
  274. date <- gsub(pattern = "-", x = date, replacement = "")
  275. #### time
  276. time <- sub(pattern = ".*T", x = datetime, replacement = "")
  277. time <- sub(pattern = "\\..*", x = time, replacement = "")
  278. time <- gsub(pattern = ":", x = time, replacement = "")
  279. #### full time
  280. fdat <- strptime(x = paste0(date, time), format = "%Y%m%d%H%M%S", tz = "CET")
  281. ### Removing HTML-Tags from Toots
  282. text <- gsub(pattern = "<.*?>", x = text, replacement = "")
  283. text <- gsub(pattern = " ", x = text, replacement = "")
  284. ### Cleaning Instance-String
  285. #### GNUsocial
  286. inst <- sub(pattern = "tag:", x = inst, replacement = "")
  287. inst <- sub(pattern = ",\\d+.*", x = inst, replacement = "")
  288. #### Mastodon
  289. inst <- sub(pattern = "https:\\/\\/", x = inst, replacement = "")
  290. inst <- sub(pattern = "\\/.*", x = inst, replacement = "")
  291. ### Only include Toots from this year
  292. mastodon_exclude <- which(as.numeric(date) < 20180210)
  293. date <- date[-mastodon_exclude]
  294. time <- time[-mastodon_exclude]
  295. fdat <- fdat[-mastodon_exclude]
  296. lang <- lang[-mastodon_exclude]
  297. inst <- inst[-mastodon_exclude]
  298. text <- text[-mastodon_exclude]
  299. link <- link[-mastodon_exclude]
  300. reto <- reto[-mastodon_exclude]
  301. favs <- favs[-mastodon_exclude]
  302. murl <- murl[-mastodon_exclude]
  303. acct <- acct[-mastodon_exclude]
  304. ### Creating dataframe
  305. mastodon <- data.frame(cbind(date, time, fdat, lang, inst, text, link, reto, favs, murl, acct))
  306. #### Clean-Up
  307. rm(list = c("date", "time", "fdat", "lang", "inst", "text", "link", "favs", "reto", "murl", "datetime", "acct"))
  308. mastodon <- within(data = mastodon, expr = {
  309. date <- as.character(date);
  310. time <- as.character(time);
  311. fdat <- as.character(fdat);
  312. text <- as.character(text);
  313. link <- as.character(link);
  314. murl <- as.character(murl);
  315. })
  316. # }}}
  317. ## Reddit Collector {{{ ----
  318. ### Authentication at Reddit
  319. # no authentication necessary, hence we can directly start scraping
  320. ### Get posts on Reddit
  321. reddit_post_dirty <- reddit_urls(search_terms = "ilovefs",
  322. #subreddit = "freesoftware linux opensource",
  323. cn_threshold = 0,
  324. page_threshold = 99999,
  325. sort_by = "new",
  326. wait_time = 5)
  327. ### Only use posts from the current year
  328. reddit_searchinyear <- 18 # has to have format "YY", eg "18" for "2018"
  329. reddit_post_year <- gsub(x = reddit_post_dirty$date,
  330. pattern = "\\d.-\\d.-",
  331. replace = "")
  332. reddit_post <- reddit_post_dirty[which(reddit_post_year == reddit_searchinyear),]
  333. ### Extracting relevant variables
  334. comt <- c() # Comments / Replies
  335. subr <- c() # Subreddit
  336. ptns <- c() # Points / Score
  337. ttle <- c() # Title
  338. text <- c() # Text / Content
  339. link <- c() # Linked to Website
  340. date <- c() # Date
  341. rurl <- c() # Reddit-URL of post
  342. acct <- c() # Author of Post
  343. for(i in c(1:length(reddit_post$URL))){
  344. comt[i] <- reddit_post$num_comments[i]
  345. ttle[i] <- reddit_post$title[i]
  346. rurl[i] <- reddit_post$URL[i]
  347. date[i] <- gsub(x = reddit_post$date[i], pattern = "-", replace = "")
  348. subr[i] <- reddit_post$subreddit[i]
  349. Sys.sleep(2)
  350. reddit_content <- reddit_content(URL = reddit_post$URL[i], wait_time = 0)
  351. ptns[i] <- reddit_content$post_score[1]
  352. text[i] <- reddit_content$post_text[1]
  353. link[i] <- reddit_content$link[1]
  354. acct[i] <- reddit_content$author[1]
  355. }
  356. ### Creating dataframe
  357. reddit <- data.frame(cbind(date, rurl, link, text, ttle, ptns, subr, comt, acct))
  358. #### Clean-Up
  359. rm(list = c("date", "rurl", "link", "text", "ttle", "ptns", "subr", "comt", "acct"))
  360. reddit <- within(data = reddit, expr = {
  361. date <- as.character(date);
  362. rurl <- as.character(rurl);
  363. link <- as.character(link);
  364. text <- as.character(text);
  365. ttle <- as.character(ttle);
  366. ptns <- as.numeric(as.character(ptns));
  367. subr <- as.character(subr);
  368. comt <- as.numeric(as.character(comt));
  369. })
  370. # }}}
  371. ### Exporting data {{{ ----
  372. time_of_saving <- sub(x = Sys.time(), pattern = " CET", replace = "")
  373. time_of_saving <- sub(x = time_of_saving, pattern = " ", replace = "_")
  374. time_of_saving <- gsub(x = time_of_saving, pattern = ":", replace = "-")
  375. #### RData
  376. save_path <- paste0("./data/ilovefs-all_", time_of_saving, ".RData")
  377. save(list = c("twitter", "mastodon", "reddit"), file = save_path)
  378. #### Text
  379. ##### Fediverse
  380. save_path_fed_t <- paste0("./data/ilovefs-fediverse_", time_of_saving, ".txt")
  381. write.table(mastodon, file = save_path_fed_t)
  382. ##### Twitter
  383. save_path_twitter_t <- paste0("./data/ilovefs-twitter_", time_of_saving, ".txt")
  384. write.table(twitter, file = save_path_twitter_t)
  385. ##### Reddit
  386. save_path_reddit_t <- paste0("./data/ilovefs-reddit_", time_of_saving, ".txt")
  387. write.table(reddit, file = save_path_reddit_t)
  388. #### CSV
  389. ##### Fediverse
  390. save_path_fed_c <- paste0("./data/ilovefs-fediverse_", time_of_saving, ".csv")
  391. write.csv(mastodon, file = save_path_fed_c)
  392. ##### Twitter
  393. save_path_twitter_c <- paste0("./data/ilovefs-twitter_", time_of_saving, ".csv")
  394. write.csv(twitter, file = save_path_twitter_c)
  395. ##### Reddit
  396. save_path_reddit_c <- paste0("./data/ilovefs-reddit_", time_of_saving, ".csv")
  397. write.csv(reddit, file = save_path_reddit_c)
  398. # }}}