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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  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. retw <- as.factor(twitter_rts)
  127. favs <- as.factor(twitter_fav)
  128. link <- as.character(twitter_url)
  129. text <- as.character(twitter_txt)
  130. clnt <- as.character(twitter_client)
  131. user <- as.character(twitter_usr)
  132. ### Creating dataframe
  133. twitter <- data.frame(cbind(date, time, retw, favs, text, link, clnt, user))
  134. #### Clean-Up
  135. rm(list = c("date", "time", "retw", "favs", "text", "link", "clnt", "user"))
  136. twitter <- within(data = twitter, expr = {
  137. date <- as.character(date);
  138. time <- as.character(time);
  139. text <- as.character(text);
  140. link <- as.character(link);
  141. })
  142. # }}}
  143. ## Mastodon Collector with curl {{{ ----
  144. mastodon.fetchdata <- function(data){
  145. tmp_datetime <- c()
  146. tmp_lang <- c()
  147. tmp_inst <- c()
  148. tmp_link <- c()
  149. tmp_text <- c()
  150. tmp_reto <- c()
  151. tmp_favs <- c()
  152. tmp_murl <- c()
  153. tmp_acct <- c()
  154. for(i in 1:length(data)){
  155. #### Time and Date of Toot
  156. if(length(data[[i]]$created_at) > 0){
  157. tmp_datetime[i] <- data[[i]]$created_at
  158. } else {
  159. # insert empty value, if it does not exist
  160. tmp_datetime[i] <- NA
  161. }
  162. #### Language of Toot
  163. if(length(data[[i]]$language) > 0){
  164. tmp_lang[i] <- data[[i]]$language
  165. } else {
  166. # insert empty value, if it does not exist
  167. tmp_lang[i] <- NA
  168. }
  169. #### Instance of Toot
  170. if(length(data[[i]]$uri) > 0){
  171. tmp_inst[i] <- data[[i]]$uri
  172. } else {
  173. # insert empty value, if it does not exist
  174. tmp_inst[i] <- NA
  175. }
  176. #### URL of Toot
  177. if(length(data[[i]]$url) > 0){
  178. tmp_link[i] <- data[[i]]$url
  179. } else {
  180. # insert empty value, if it does not exist
  181. tmp_link[i] <- NA
  182. }
  183. #### Text/Content of Toot
  184. if(length(data[[i]]$content) > 0){
  185. tmp_text[i] <- data[[i]]$content
  186. } else {
  187. # insert empty value, if it does not exist
  188. tmp_text[i] <- NA
  189. }
  190. #### Number of Retoots
  191. if(length(data[[i]]$reblogs_count) > 0){
  192. tmp_reto[i] <- data[[i]]$reblogs_count
  193. } else {
  194. # insert empty value, if it does not exist
  195. tmp_reto[i] <- NA
  196. }
  197. #### Number of Favorites
  198. if(length(data[[i]]$favourites_count) > 0){
  199. tmp_favs[i] <- data[[i]]$favourites_count
  200. } else {
  201. # insert empty value, if it does not exist
  202. tmp_favs[i] <- NA
  203. }
  204. #### Number of Favorites
  205. if(length(data[[i]]$media_attachments) > 0){
  206. tmp_murl[i] <- data[[i]]$media_attachments[[1]]$url
  207. } else {
  208. # insert empty value, if it does not exist
  209. tmp_murl[i] <- NA
  210. }
  211. #### Account of Tooter
  212. if(length(data[[i]]$account) > 0){
  213. tmp_acct[i] <- data[[i]]$account$acct
  214. } else {
  215. # insert empty value, if it does not exist
  216. tmp_acct[i] <- NA
  217. }
  218. }
  219. return(data.frame(cbind(tmp_datetime,
  220. tmp_lang,
  221. tmp_inst,
  222. tmp_text,
  223. tmp_link,
  224. tmp_reto,
  225. tmp_favs,
  226. tmp_murl,
  227. tmp_acct)))
  228. }
  229. datetime <- c()
  230. lang <- c()
  231. inst <- c()
  232. link <- c()
  233. text <- c()
  234. reto <- c()
  235. favs <- c()
  236. murl <- c()
  237. acct <- c()
  238. for(i in 1:999){
  239. if(i == 1){
  240. mastodon_instance <- "https://mastodon.social"
  241. mastodon_hashtag <- "ilovefs"
  242. mastodon_url <- paste0(mastodon_instance,
  243. "/api/v1/timelines/tag/",
  244. mastodon_hashtag,
  245. "?limit=40")
  246. } else {
  247. mastodon_lheader <- parse_headers(mastodon_reqres$headers)[11]
  248. mastodon_next <- sub(x = mastodon_lheader, pattern = ".*link:\ <", replace = "")
  249. mastodon_url <- sub(x = mastodon_next, pattern = ">;\ rel=\"next\".*", replace = "")
  250. }
  251. mastodon_reqres <- curl_fetch_memory(mastodon_url)
  252. mastodon_rawjson <- rawToChar(mastodon_reqres$content)
  253. toots <- fromJSON(mastodon_rawjson)
  254. if(length(toots) > 0){
  255. tmp_mastodon_df <- mastodon.fetchdata(data = toots)
  256. datetime <- c(datetime, as.character(tmp_mastodon_df$tmp_datetime))
  257. lang <- c(lang, as.character(tmp_mastodon_df$tmp_lang))
  258. inst <- c(inst, as.character(tmp_mastodon_df$tmp_inst))
  259. link <- c(link, as.character(tmp_mastodon_df$tmp_link))
  260. text <- c(text, as.character(tmp_mastodon_df$tmp_text))
  261. reto <- c(reto, as.character(tmp_mastodon_df$tmp_reto))
  262. favs <- c(favs, as.character(tmp_mastodon_df$tmp_favs))
  263. murl <- c(murl, as.character(tmp_mastodon_df$tmp_murl))
  264. acct <- c(acct, as.character(tmp_mastodon_df$tmp_acct))
  265. } else {
  266. break
  267. }
  268. }
  269. ### Time of post
  270. #### date (as numeric value)
  271. date <- sub(pattern = "T.*", x = datetime, replacement = "")
  272. date <- gsub(pattern = "-", x = date, replacement = "")
  273. #### time (as numeric value)
  274. time <- sub(pattern = ".*T", x = datetime, replacement = "")
  275. time <- sub(pattern = "\\..*", x = time, replacement = "")
  276. time <- gsub(pattern = ":", x = time, replacement = "")
  277. ### Removing HTML-Tags from Toots
  278. text <- gsub(pattern = "<.*?>", x = text, replacement = "")
  279. text <- gsub(pattern = " ", x = text, replacement = "")
  280. ### Cleaning Instance-String
  281. #### GNUsocial
  282. inst <- sub(pattern = "tag:", x = inst, replacement = "")
  283. inst <- sub(pattern = ",\\d+.*", x = inst, replacement = "")
  284. #### Mastodon
  285. inst <- sub(pattern = "https:\\/\\/", x = inst, replacement = "")
  286. inst <- sub(pattern = "\\/.*", x = inst, replacement = "")
  287. ### Only include Toots from this year
  288. mastodon_exclude <- which(as.numeric(date) < 20180210)
  289. date <- date[-mastodon_exclude]
  290. time <- time[-mastodon_exclude]
  291. lang <- lang[-mastodon_exclude]
  292. inst <- inst[-mastodon_exclude]
  293. text <- text[-mastodon_exclude]
  294. link <- link[-mastodon_exclude]
  295. reto <- reto[-mastodon_exclude]
  296. favs <- favs[-mastodon_exclude]
  297. murl <- murl[-mastodon_exclude]
  298. acct <- acct[-mastodon_exclude]
  299. ### Creating dataframe
  300. mastodon <- data.frame(cbind(date, time, lang, inst, text, link, reto, favs, murl, acct))
  301. #### Clean-Up
  302. rm(list = c("date", "time", "lang", "inst", "text", "link", "favs", "reto", "murl", "datetime", "acct"))
  303. mastodon <- within(data = mastodon, expr = {
  304. date <- as.character(date);
  305. time <- as.character(time);
  306. text <- as.character(text);
  307. link <- as.character(link);
  308. murl <- as.character(murl);
  309. })
  310. # }}}
  311. ## Reddit Collector {{{ ----
  312. ### Authentication at Reddit
  313. # no authentication necessary, hence we can directly start scraping
  314. ### Get posts on Reddit
  315. reddit_post_dirty <- reddit_urls(search_terms = "ilovefs",
  316. #subreddit = "freesoftware linux opensource",
  317. cn_threshold = 0,
  318. page_threshold = 99999,
  319. sort_by = "new",
  320. wait_time = 5)
  321. ### Only use posts from the current year
  322. reddit_searchinyear <- 18 # has to have format "YY", eg "18" for "2018"
  323. reddit_post_year <- gsub(x = reddit_post_dirty$date,
  324. pattern = "\\d.-\\d.-",
  325. replace = "")
  326. reddit_post <- reddit_post_dirty[which(reddit_post_year == reddit_searchinyear),]
  327. ### Extracting relevant variables
  328. comt <- c() # Comments / Replies
  329. subr <- c() # Subreddit
  330. ptns <- c() # Points / Score
  331. ttle <- c() # Title
  332. text <- c() # Text / Content
  333. link <- c() # Linked to Website
  334. date <- c() # Date
  335. rurl <- c() # Reddit-URL of post
  336. acct <- c() # Author of Post
  337. for(i in c(1:length(reddit_post$URL))){
  338. comt[i] <- reddit_post$num_comments[i]
  339. ttle[i] <- reddit_post$title[i]
  340. rurl[i] <- reddit_post$URL[i]
  341. date[i] <- gsub(x = reddit_post$date[i], pattern = "-", replace = "")
  342. subr[i] <- reddit_post$subreddit[i]
  343. Sys.sleep(2)
  344. reddit_content <- reddit_content(URL = reddit_post$URL[i], wait_time = 0)
  345. ptns[i] <- reddit_content$post_score[1]
  346. text[i] <- reddit_content$post_text[1]
  347. link[i] <- reddit_content$link[1]
  348. acct[i] <- reddit_content$author[1]
  349. }
  350. ### Creating dataframe
  351. reddit <- data.frame(cbind(date, rurl, link, text, ttle, ptns, subr, comt, acct))
  352. #### Clean-Up
  353. rm(list = c("date", "rurl", "link", "text", "ttle", "ptns", "subr", "comt", "acct"))
  354. reddit <- within(data = reddit, expr = {
  355. date <- as.character(date);
  356. rurl <- as.character(rurl);
  357. link <- as.character(link);
  358. text <- as.character(text);
  359. ttle <- as.character(ttle);
  360. ptns <- as.numeric(as.character(ptns));
  361. subr <- as.character(subr);
  362. comt <- as.numeric(as.character(comt));
  363. })
  364. # }}}
  365. ### Exporting data {{{ ----
  366. time_of_saving <- sub(x = Sys.time(), pattern = " CET", replace = "")
  367. time_of_saving <- sub(x = time_of_saving, pattern = " ", replace = "_")
  368. time_of_saving <- gsub(x = time_of_saving, pattern = ":", replace = "-")
  369. #### RData
  370. save_path <- paste0("./data/ilovefs-all_", time_of_saving, ".RData")
  371. save(list = c("twitter", "mastodon", "reddit"), file = save_path)
  372. #### Text
  373. ##### Fediverse
  374. save_path_fed_t <- paste0("./data/ilovefs-fediverse_", time_of_saving, ".txt")
  375. write.table(mastodon, file = save_path_fed_t)
  376. ##### Twitter
  377. save_path_twitter_t <- paste0("./data/ilovefs-twitter_", time_of_saving, ".txt")
  378. write.table(twitter, file = save_path_twitter_t)
  379. ##### Reddit
  380. save_path_reddit_t <- paste0("./data/ilovefs-reddit_", time_of_saving, ".txt")
  381. write.table(reddit, file = save_path_reddit_t)
  382. #### CSV
  383. ##### Fediverse
  384. save_path_fed_c <- paste0("./data/ilovefs-fediverse_", time_of_saving, ".csv")
  385. write.csv(mastodon, file = save_path_fed_c)
  386. ##### Twitter
  387. save_path_twitter_c <- paste0("./data/ilovefs-twitter_", time_of_saving, ".csv")
  388. write.csv(twitter, file = save_path_twitter_c)
  389. ##### Reddit
  390. save_path_reddit_c <- paste0("./data/ilovefs-reddit_", time_of_saving, ".csv")
  391. write.csv(reddit, file = save_path_reddit_c)
  392. # }}}