Skip to content

Instantly share code, notes, and snippets.

@renschler
Last active February 21, 2019 06:08
Show Gist options
  • Save renschler/b11664b606cf9a476d0164fabaaf5eab to your computer and use it in GitHub Desktop.
Save renschler/b11664b606cf9a476d0164fabaaf5eab to your computer and use it in GitHub Desktop.
/*
See https://twitter.com/renschler/status/1081666886377619457 for context,
this gist contains JS snippets you can use one after another to download a csv with your liked tweets.
*/
/* ***************************************************************************** */
/*
Scroll to load liked tweets...
Fist go to your likes page : https://twitter.com/i/likes
Then scroll down as much as possible, you can use the snippet below to automate the process
When I ran with maxScroll set to 1200000, that got me ~4400 liked tweets, and browser consumed ~ 4.5 GB of memory.
You can stop it early by running clearInterval(intervalId)
*/
let maxScroll = 2000000;
function intervalScroll(){
if (scrollY < maxScroll){
scrollTo(null, scrollY+6000);
} else {
clearInterval(intervalId);
}
}
let intervalId = setInterval(intervalScroll, 3000);
/* ***************************************************************************** */
/*
Process liked tweets on page...
Once done scrolling, you can paste this into the console and it will download a csv file of all liked tweets on the page.
You can then copy paste the csv into your spreadsheet.
In google sheets you'll probably need to Data -> Split Text to Columns if the pasted values end up in one column.
*/
let tweets = [];
let regex = /\//g;
let options = { year: 'numeric', month: '2-digit', day: '2-digit', hour:'2-digit', minute:'2-digit', timeZone: 'America/Los_Angeles'}; // trying to match my integromat date format YYYY-MM-DD HH:MM A
[...document.querySelectorAll("div[class*='js-stream-tweet']")].map((node) => {
let newTweet = {
handle: node.getAttribute("data-screen-name"),
link: "https://twitter.com" + node.getAttribute("data-permalink-path"),
date: new Date(Number(node.querySelector("span[class*='_timestamp']").getAttribute("data-time-ms"))).toLocaleString('ja-JP', options).replace(regex, '-'),
text: node.querySelector("p[class*='TweetTextSize']").textContent
};
tweets.push(newTweet)
})
let csvTweets = tweets.map((tweet)=>{
return `${tweet.handle},${tweet.link},${tweet.date},${tweet.text.replace(/,/g,"").replace(/\n/g,"")}`
}).reverse().join('\n');
let filename = "likedTweets.csv";
let tweetsBlob = new Blob([csvTweets], {type: 'text/csv'});
let link = document.createElement("a");
let url = URL.createObjectURL(tweetsBlob);
link.setAttribute("href", url);
link.setAttribute("download", filename);
link.style.visibility = 'hidden';
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment