Created
January 5, 2024 08:20
-
-
Save selcukcihan/359142a20aa5e8abf80578f085b21a6a to your computer and use it in GitHub Desktop.
Script to delete tweets using the twitter data export output file
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
/* | |
* Usage: node delete_my_tweets.js {TWITTER_DATA_EXPORT}/data/tweets.js | |
* Set your twitter api keys before running the script on these env variables: | |
* TWITTER_CONSUMER_APP_KEY | |
* TWITTER_CONSUMER_APP_SECRET | |
* TWITTER_ACCESS_TOKEN | |
* TWITTER_ACCESS_SECRET | |
*/ | |
import { TwitterApi } from "twitter-api-v2"; | |
import fs from "fs"; | |
const DELETED_TWEETS_FILE = "deleted.json"; | |
const deletedTweets = fs.existsSync(DELETED_TWEETS_FILE) | |
? JSON.parse(fs.readFileSync(DELETED_TWEETS_FILE, "utf8")) | |
: []; | |
const client = new TwitterApi({ | |
appKey: process.env.TWITTER_CONSUMER_APP_KEY, | |
appSecret: process.env.TWITTER_CONSUMER_APP_SECRET, | |
accessToken: process.env.TWITTER_ACCESS_TOKEN, | |
accessSecret: process.env.TWITTER_ACCESS_SECRET, | |
}); | |
const delay = (ms) => new Promise((resolve) => setTimeout(resolve, ms)); | |
const deleteTweet = async (tweetId) => { | |
try { | |
const response = await client.v2.deleteTweet(tweetId); | |
console.log(response); | |
if (response?.data?.deleted) { | |
deletedTweets.push(tweetId); | |
await delay(2000); | |
return true; | |
} else { | |
return false; | |
} | |
} catch (e) { | |
console.error(e); | |
return false; | |
} | |
}; | |
async function run() { | |
const tweetsJs = fs.readFileSync(process.argv[2], "utf8"); | |
const tweets = JSON.parse(tweetsJs.replace("window.YTD.tweets.part0 = ", "")); | |
let i = 0; | |
for (const tweet of tweets) { | |
console.log("Tweet", i, tweet.tweet.id_str, tweet.tweet.favorite_count); | |
i++; | |
if (parseInt(tweet?.tweet?.favorite_count || "0") > 300) { | |
continue; | |
} | |
const tweetId = tweet.tweet.id_str; | |
if (!deletedTweets.includes(tweetId)) { | |
const deleted = await deleteTweet(tweetId); | |
if (!deleted) { | |
console.error("Failed to delete tweet", tweetId); | |
break; | |
} | |
} | |
} | |
fs.writeFileSync( | |
DELETED_TWEETS_FILE, | |
JSON.stringify(deletedTweets, null, 2), | |
"utf8" | |
); | |
} | |
run(); |
Hello,
wanted to ask how I can get the JS to work? Do I have to start it in the Chrome Console? If so, where do I have to put the file?
Tesekürler! :)
Hi, you run it with node on the terminal (step 4 above).
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Prerequisites
You need to set up twitter developer account, follow https://developer.twitter.com/en/docs/twitter-api/getting-started/getting-access-to-the-twitter-api
Export your API keys before running the script:
You also need to download your tweets from https://twitter.com/settings/download_your_data
When you're all set up, run
node delete_my_tweets.js {TWITTER_DATA_EXPORT}/data/tweets.js
You can tweak the filter to select which tweets to be deleted, current filter reads:
parseInt(tweet?.tweet?.favorite_count || "0") > 300
which implies "do not delete tweets with more than 300 likes".