项目作者: mkearney

项目描述 :
My Jeopardy Twitter bot
高级语言: R
项目地址: git://github.com/mkearney/jeopboty.git
创建时间: 2017-04-12T20:18:23Z
项目社区:https://github.com/mkearney/jeopboty

开源协议:

下载


#jeopboty: a jeopardy twitter bot

I scraped Jeopardy clues and answers from an online archive and created a bot-like script to post them to Twitter.

The bot script can be found in the R folder. It’s also pasted below.

  1. ## save current directory
  2. od <- getwd()
  3. ## for now, go to R dir in jeopboty
  4. setwd("~/r/jeopboty/R")
  5. ## Reading data and the [correct] Twitter token
  6. ## load rtweet
  7. library(rtweet)
  8. ## If you haven't created and saved a personal access Twitter API
  9. ## token, do it now following these instructions (uncomment next line):
  10. ## vignette("auth", "rtweet")
  11. ## Make sure you've given the token "write" access (permission)
  12. ## which you can check through your Twitter account.
  13. ## Uncomment next line to open browser to Twitter's app page.
  14. ## browseURL("https://apps.twitter.com")
  15. ## If you already saved your token as an environment variable, you can
  16. ## fetch it using the get_tokens() function.
  17. token <- get_tokens()
  18. ## I have a token I use exclusively for my #jeopboty statuses,
  19. ## so I've saved that in my data folder (note: token won't
  20. ## appear in my Github repository; nice try!)
  21. token <- readRDS("../data/rtw.rds")
  22. ## Get the screen name associated with your token.
  23. my_screen_name <- token$credentials$screen_name
  24. ## This should print out YOUR screen name:
  25. my_screen_name
  26. ## Read jeopardy data.
  27. jeop <- readRDS("../data/jeopardy.rds")
  28. ## Posting the CLUE status on Twitter
  29. ## Randomly select a question.
  30. i <- sample(seq_len(NROW(jeop)), 1L)
  31. ## Compose CLUE status.
  32. clue <- paste0("Clue: ", jeop$clue[i], " #jeopboty")
  33. ## Post CLUE status to Twitter.
  34. post_tweet(clue, token = token)
  35. ## Keeping track of questions you've tweeted over time
  36. ## Either extract and update or create "used" data object.
  37. if ("used" %in% names(attributes(jeop))) {
  38. ## Extract used data attribute
  39. used <- attr(jeop, "used")
  40. ## Determine clue [number] n post.
  41. n <- NROW(used[["id"]]) + 1L
  42. ## Update (append) used object
  43. used[["id"]][n] <- n
  44. used[["datetime"]][n] <- Sys.time()
  45. used[["clue"]][n] <- jeop$clue[i]
  46. used[["answer"]][n] <- jeop$answer[i]
  47. } else {
  48. ## Create used object.
  49. used <- list(
  50. id = 1,
  51. datetime = Sys.time(),
  52. clue = jeop$clue[i],
  53. answer = jeop$answer[i])
  54. }
  55. ## Add "used" data attribute to "jeop" data.
  56. attr(jeop, "used") <- used
  57. ## Save data (dropping the row that was just posted.
  58. saveRDS(jeop[-i, ], "../data/jeopardy.rds")
  59. ## Posting the ANSWER status on Twitter
  60. ## Get recent timeline data for your account.
  61. tw <- get_timeline(my_screen_name)
  62. ## Find and return status ID of most recent clue.
  63. status_id <- tw$status_id[grep("^Clue", tw$text)[1]]
  64. ## Use the status ID to create quote link.
  65. quote <- paste0("https://twitter.com/",
  66. my_screen_name,
  67. "/status/",
  68. status_id)
  69. ## Compose ANSWER status.
  70. answer <- paste0(
  71. "Answer: ", jeop$answer[i], " #jeopboty ", quote)
  72. ## Post ANSWER status to Twitter.
  73. post_tweet(answer, token = token)
  74. ## on exit return wd
  75. setwd(od)