Created
December 13, 2022 12:48
-
-
Save noamross/2c69d6eb7490efe10a65a7c3641e7b00 to your computer and use it in GitHub Desktop.
A {targets} workflow to nuke all your twitter data
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# A Targets workflow to nuke your Twitter data | |
# Still working around some rate limits, | |
# I suggest building each of the deletion targets | |
# individually, e.g., "tar_make(blockunblock)" | |
# Load packages required to define the pipeline: | |
library(targets) | |
library(tarchetypes) | |
library(tidyverse) | |
library(rtweet) | |
library(jsonlite) | |
library(stringi) | |
library(httr) | |
# Run once interactively first: auth_save(rtweet_user(), "twitternuker") | |
# Fetch data out of an exported twitter archive (here in ../tweet-archive) | |
data_targets <- tar_plan( | |
tweets = fromJSON(stri_replace_first_regex(read_file("../tweet-archive/data/tweets.js"), "window\\.[^=]+= ", "")), | |
following = fromJSON(stri_replace_first_regex(read_file("../tweet-archive/data/following.js"), "window\\.[^=]+= ", "")), | |
follower = fromJSON(stri_replace_first_regex(read_file("../tweet-archive/data/follower.js"), "window\\.[^=]+= ", "")), | |
likes = fromJSON(stri_replace_first_regex(read_file("../tweet-archive/data/like.js"), "window\\.[^=]+= ", "")), | |
direct_messages = fromJSON(stri_replace_first_regex(read_file("../tweet-archive/data/direct-messages.js"), "window\\.[^=]+= ", "")), | |
tar_target(tweet_ids, rev(tweets$tweet$id), iteration = "vector"), | |
tar_target(following_ids, following$following$accountId, iteration = "vector"), | |
tar_target(follower_ids, follower$follower$accountId, iteration = "vector"), | |
tar_target(like_ids, likes$like$tweetId, iteration = "vector"), | |
tar_target(dm_ids, bind_rows(direct_messages$dmConversation$messages)$messageCreate$id, iteration = "vector"), | |
tar_target(convo_ids, direct_messages$dmConversation$conversationId, iteration = "vector"), | |
) | |
# Now to nuke: | |
deletion_targets <- tar_plan( | |
tar_target( | |
unfollow, | |
post_unfollow_user(following_ids, token = rtweet::auth_as("twitternuker")), | |
pattern = map(following_ids), | |
error = "null"), | |
tar_target( | |
blockunblock, { | |
user_block(follower_ids, token = rtweet::auth_as("twitternuker")) | |
user_unblock(follower_ids, token = rtweet::auth_as("twitternuker")) | |
}, | |
pattern = map(follower_ids), | |
error = "null"), | |
tar_target( | |
delete, | |
{ | |
rate_limit_wait("/statuses/destroy/:id") | |
post_destroy(tweet_ids, token = rtweet::auth_as("twitternuker")) | |
}, | |
pattern = map(tweet_ids), | |
error = "null"), | |
tar_target( | |
unlike, | |
post_favorite(like_ids, destroy = TRUE, token = rtweet::auth_as("twitternuker")), | |
pattern = map(like_ids), | |
error = "null" | |
), | |
tar_target( | |
delete_dms, | |
httr::DELETE( | |
"https://api.twitter.com/1.1/direct_messages/events/destroy.json", | |
query = list(id = dm_ids), | |
config(token = rtweet::auth_as("twitternuker"))), | |
pattern = map(dm_ids), | |
error = "null" | |
) | |
) | |
list( | |
data_targets, | |
deletion_targets | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment