Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

192 lines
4.6KB

  1. #' @title gathertweet actions
  2. #' @export
  3. gathertweet_search <- function(
  4. terms,
  5. file = "tweets.rds",
  6. n = 18000,
  7. max_id = NULL,
  8. since_id = "last",
  9. type = "recent",
  10. include_rts = FALSE,
  11. geocode = NULL,
  12. `no-parse` = FALSE,
  13. token = NULL,
  14. retryonratelimit = FALSE,
  15. quiet = FALSE,
  16. ...
  17. ) {
  18. log_info("Searching for \"{paste0(terms, collapse = '\", \"')}\"")
  19. since_id <- set_since_id(since_id, max_id, file)
  20. tweets <- lapply(
  21. terms,
  22. function(term) rtweet::search_tweets(
  23. q = term,
  24. n = as.integer(n),
  25. type = type,
  26. include_rts = include_rts,
  27. geocode = geocode,
  28. max_id = max_id,
  29. parse = isFALSE(`no-parse`),
  30. token = token,
  31. retryonratelimit = retryonratelimit,
  32. verbose = isFALSE(quiet),
  33. since_id = since_id
  34. )
  35. )
  36. if (isTRUE(`no-parse`)) {
  37. log_info("Saving un-parsed tweets in {file}")
  38. saveRDS(tweets, file)
  39. } else {
  40. tweets <- dplyr::bind_rows(tweets)
  41. save_tweets_or_exit(tweets, file)
  42. }
  43. tweets
  44. }
  45. #' @export
  46. gathertweet_update <- function(file = "tweets.rds", `no-parse` = FALSE, token = NULL, ...) {
  47. logger("Updating tweets in {file}")
  48. if (!file.exists(file)) {
  49. log_fatal("`{file}` does not exist")
  50. }
  51. tweets <- update_tweets(
  52. file = file,
  53. # passed to rtweet::lookup_statuses()
  54. parse = isFALSE(`no-parse`),
  55. token = token
  56. )
  57. log_debug("Status lookup returned {nrow(tweets)} tweets")
  58. tweets <- save_tweets(tweets, file)
  59. log_debug("Total of {nrow(tweets)} tweets in {file}")
  60. tweets
  61. }
  62. #' @export
  63. gathertweet_timeline <- function(
  64. users,
  65. file = "tweets.rds",
  66. n = 3200,
  67. max_id = NULL,
  68. home = TRUE,
  69. `no-parse` = FALSE,
  70. token = NULL,
  71. include_rts = FALSE,
  72. ...
  73. ) {
  74. log_info("Gathering tweets by {collapse(users)}")
  75. n <- as.integer(n)
  76. if (n > 3200) {
  77. log_warn("Twitter API for timelines returns a maximum of 3200 tweets per user")
  78. }
  79. tweets <- rtweet::get_timeline(
  80. user = users,
  81. n = n,
  82. max_id = max_id,
  83. home = isTRUE(home),
  84. parse = isFALSE(`no-parse`),
  85. check = TRUE,
  86. token = token,
  87. include_rts = isTRUE(include_rts)
  88. )
  89. save_tweets_or_exit(tweets, file)
  90. tweets
  91. }
  92. #' @export
  93. gathertweet_favorites <- function(
  94. users,
  95. file = "tweets.rds",
  96. n = 3000,
  97. max_id = NULL,
  98. since_id = NULL,
  99. `no-parse` = FALSE,
  100. token = NULL,
  101. ...
  102. ) {
  103. log_info("Gathering tweets favorited by {collapse(users)}")
  104. since_id <- set_since_id(since_id, max_id, file)
  105. n <- as.integer(n)
  106. if (n > 3000) {
  107. log_warn("Twitter API for favorites/list returns a maximum of 3000 tweets per user")
  108. n <- 3000
  109. }
  110. tweets <- rtweet::get_favorites(
  111. user = users,
  112. n = n,
  113. max_id = max_id,
  114. since_id = since_id,
  115. parse = isFALSE(`no-parse`),
  116. token = token
  117. )
  118. save_tweets_or_exit(tweets, file)
  119. tweets
  120. }
  121. #' @export
  122. gathertweet_simplify <- function(
  123. file = "tweets.rds",
  124. fields = NULL,
  125. output = NULL,
  126. ...
  127. ) {
  128. logger("Simplifying tweets in {file}")
  129. if (!file.exists(file)) {
  130. log_fatal("`{file}` does not exist")
  131. }
  132. tweets_simplified <- simplify_tweets(
  133. tweets = NULL,
  134. file = file,
  135. .fields = fields
  136. )
  137. log_debug("Simplified {nrow(tweets_simplified)} tweets")
  138. if (is.null(output)) {
  139. output <- gathertweet:::path_add(file, append = "_simplified")
  140. }
  141. log_info("Saving simplified tweets to {output}")
  142. save_tweets(tweets_simplified, output)
  143. }
  144. isFALSE <- function(x) is.logical(x) && length(x) == 1L && !is.na(x) && !x
  145. set_since_id <- function(since_id = NULL, max_id = NULL, file = NULL) {
  146. since_id <- if (is.null(max_id)) {
  147. if (since_id == "last") {
  148. if (is.null(file)) {
  149. log_fatal("`file` must be provided for since_id = \"last\"")
  150. }
  151. last_seen_tweet(file = file)
  152. } else if (since_id == "none") {
  153. NULL
  154. } else since_id
  155. }
  156. if (!is.null(since_id)) log_info("Tweets from {since_id}")
  157. if (!is.null(max_id)) log_info("Tweets up to {max_id}")
  158. since_id
  159. }
  160. save_tweets_or_exit <- function(tweets, file) {
  161. if (nrow(tweets) == 0) {
  162. log_info("---- No new tweets. ----")
  163. exit()
  164. }
  165. tweets <- tweets[!duplicated(tweets$status_id), ]
  166. tweets <- tweets[order(tweets$status_id), ]
  167. log_info("Gathered {nrow(tweets)} tweets")
  168. tweets <- save_tweets(tweets, file)
  169. log_info("Total of {nrow(tweets)} tweets in {file}")
  170. }