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.

161 lines
5.7KB

  1. #! /usr/bin/env Rscript
  2. # Usage -----------------------------------------------------------------------
  3. 'Gather tweets from the command line
  4. Usage:
  5. gathertweet search [--file=<file>] [options] [--] <terms>...
  6. gathertweet update [--file=<file> --and-simplify --polite --debug-args --token=<token> --backup --backup-dir=<dir>]
  7. gathertweet timeline [options] [--] <users>...
  8. gathertweet favorites [options] [--] <users>...
  9. gathertweet simplify [--file=<file> --output=<output> --debug-args --polite] [<fields>...]
  10. Options:
  11. -h --help Show this screen.
  12. --file <file> Name of RDS file where tweets are stored
  13. [default: tweets.rds]
  14. --no-parse Disable parsing of the results
  15. --token <token> See {rtweet} for more information
  16. --retryonratelimit Wait and retry when rate limited (only relevant when n
  17. exceeds 18000 tweets)
  18. --quiet Disable printing of {rtweet} processing messages
  19. --polite Only allow one process (search|update) to run at a time
  20. --backup Create a backup of existing tweet file
  21. --backup-dir <dir> Location for backups [default: backups]
  22. --debug-args Debug input arguments
  23. --and-simplify Create additional simplified tweet set.
  24. Run `gathertweet simplify` manually for more control.
  25. search:
  26. <terms> Search terms. Individual search terms are queried separately,
  27. but duplicated tweets are removed from the stored results.
  28. Each search term counts against the 15 minute rate limit of 180
  29. searches, which can be avoided by manually joining search terms
  30. into a single query. NOTE: Wrap queries with spaces in
  31. \'single quotes\': only use double quotes within single quotes.
  32. --type <type> Type of search results: "recent", "mixed", or "popular"
  33. [default: recent]
  34. --geocode <geocode> Geographical limiter of the template
  35. "latitude,longitude,radius"
  36. --since_id <since_id> Return results with an ID greather than (newer than) or
  37. equal to since_id, automatically extracted from the
  38. existing tweets <file>, if it exists, and ignored when
  39. <max_id> is set. Use "none" for all available tweets,
  40. or "last" for the maximum seen status_id in existing
  41. tweets. [default: last]
  42. search and timeline:
  43. -n, --n <n> Number of tweets to return [default: 18000]
  44. --include_rts Logical indicating whether retweets should be included
  45. (default is to exclude RTs)
  46. --max_id <max_id> Return tweets with an ID less (older) than or equal to
  47. timeline and favorites:
  48. <users> A list of users as user names, IDs, or a mixture of both,
  49. separated by spaces.
  50. timeline:
  51. --home If included, returns home-timeline instead of user-timeline.
  52. simplify:
  53. <fields> Tweet fields that should be included. By default includes:
  54. `status_id`, `created_at`, `user_id`, `screen_name`, `text`,
  55. `favorite_count`, `retweet_count`, `is_quote`, `hashtags`,
  56. `mentions_screen_name`, `profile_url`, `profile_image_url`,
  57. `media_url`, `urls_url`, `urls_expanded_url`.
  58. --output <output> Output file, default is input file with `_simplified`
  59. appended to name.
  60. ' -> doc
  61. library(docopt)
  62. args <- docopt(doc, version = paste('gathertweet version', packageVersion("gathertweet")))
  63. exit <- function(value = 0) q(save = "no", value)
  64. if (args$`--debug-args`) {
  65. str(args)
  66. saveRDS(args, "args.rds")
  67. exit()
  68. }
  69. do_gathertweet <- function() {
  70. library(gathertweet)
  71. collapse <- function(..., sep = ", ") paste(..., collapse = sep)
  72. # Which action was called?
  73. valid_actions <- c("search", "update", "simplify", "timeline", "favorites")
  74. action <- names(Filter(isTRUE, args[valid_actions]))
  75. if (!length(action)) {
  76. log_fatal("Please specify a valid action: {collapse(valid_actions)}")
  77. }
  78. if (args$polite) {
  79. lockfile <- paste0(
  80. ".gathertweet_",
  81. digest::digest(args[c("file", "search", "update", "simplify")]),
  82. ".lock"
  83. )
  84. lck <- filelock::lock(lockfile, exclusive = TRUE, timeout = 0)
  85. gathertweet:::stopifnot_locked(
  86. lck,
  87. "Another gathertweet {action} process is currently running for {args$file}"
  88. )
  89. on.exit({
  90. filelock::unlock(lck)
  91. unlink(lockfile)
  92. })
  93. }
  94. log_info("---- gathertweet {action} start ----")
  95. if (isTRUE(args$backup)) {
  96. backup_tweets(args$file, backup_dir = args[["backup-dir"]])
  97. }
  98. # Also simplify if --and-simplify flag is called
  99. if (args[["--and-simplify"]]) args$simplify <- TRUE
  100. tweets <-
  101. # Search ----
  102. if (isTRUE(args$search)) {
  103. do.call("gathertweet_search", args)
  104. # Update ----
  105. } else if (isTRUE(args$update)) {
  106. do.call("gathertweet_update", args)
  107. # Timeline ----
  108. } else if (isTRUE(args$timeline)) {
  109. if (!length(args$users)) {
  110. stop("Please provide a list of users as user names, user IDs, ",
  111. "or a mixture of both.")
  112. }
  113. do.call("gathertweet_timeline", args)
  114. # Favorites ----
  115. } else if (isTRUE(args$favorites)) {
  116. if (!length(args$users)) {
  117. stop("Please provide a list of users as user names, user IDs, ",
  118. "or a mixture of both.")
  119. }
  120. do.call("gathertweet_favorites", args)
  121. }
  122. # Simplify ----------------------------------------------------------------
  123. if (isTRUE(args$simplify)) {
  124. do.call("gathertweet_simplify", args)
  125. }
  126. if (args$polite) {
  127. filelock::unlock(lck)
  128. unlink(lockfile)
  129. }
  130. log_info("---- gathertweet {action} complete ----")
  131. }
  132. do_gathertweet()