Skip to content

Instantly share code, notes, and snippets.

@ivan
Last active November 29, 2022 13:40
Show Gist options
  • Save ivan/ab5a7233df9b436af0014e92132d0c34 to your computer and use it in GitHub Desktop.
Save ivan/ab5a7233df9b436af0014e92132d0c34 to your computer and use it in GitHub Desktop.
saving your tweets with snscrape and SingleFile cli

Another way to save your own tweets exactly as they are rendered on Twitter, with replies, using snscrape and SingleFile/cli:

  1. TWEETER=someusername
    snscrape twitter-user ${TWEETER} | grep -o -P '\d+$' | sed -r 's,^,https://twitter.com/i/status/,g' > ${TWEETER}-tweets # write a URL list
    
  2. Make sure the oldest tweet in this list is in fact your oldest tweet, and that snscrape did not stop early due to Twitter Search problems.
  3. Merge in all the tweet URLs from your Twitter data export, to include retweets and Circle tweets:
    cat data/tweets.js | sed -r 's/^window\.YTD\.tweets\.part0 = //' | jq -r '.[].tweet.id' | sed -r 's,^,https://twitter.com/i/status/,g' >> ${TWEETER}-tweets
    cat ${TWEETER}-tweets | sort | uniq > ${TWEETER}-tweets.uniq && mv -f ${TWEETER}-tweets{.uniq,}
    
  4. Write a cookies.txt file with your Twitter cookies, exported using the cookie.txt browser extension.
  5. Unmute people if you want their replies included.
  6. single-file \
       --browser-executable-path google-chrome-unstable \
       --browser-cookies-file cookies.txt \
       --browser-wait-delay 2000 \
       --urls-file ${TWEETER}-tweets \
       --user-agent "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36" \
       --save-original-urls \
       --filename-template '{page-title} - {url-href-flat}.html'
    

Then run it again for all the URLs that failed due to transient Twitter errors...

@ivan
Copy link
Author

ivan commented Nov 27, 2022

I observed this crash after 5920 tweets: https://gist.github.com/ivan/2eb572eaafb588f07b729a3315cbf265, so watch out for that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment