Skip to content

Instantly share code, notes, and snippets.

@alesmit
Last active November 7, 2018 22:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alesmit/87a3e1d4282fae54289e59ea442da020 to your computer and use it in GitHub Desktop.
Save alesmit/87a3e1d4282fae54289e59ea442da020 to your computer and use it in GitHub Desktop.
rettiwT - Extract tweets from the UI
/*
* Description:
* Use it to get an array of tweets from a twitter profile.
*
* Note: this may stop working if Twitter devs update the DOM.
* Last tested date: 2018-11-07
*
* How to use it:
* Copy-paste in the Chrome console, then type rettiwt() and hit Enter.
* Use JSON.stringify(rettiwt()) to get tweets data as a JSON string.
* Note: you must be on a twitter profile / stream page to make it work! (e.g. https://twitter.com/search?q=from:NASA+since:2018-10-01+until:2018-10-31)
*
*/
function rettiwt() {
const tweets = [];
document.querySelectorAll('.stream-item[data-item-type="tweet"]').forEach(tweetEl => {
const date = new Date(+`${tweetEl.querySelector('.content .stream-item-header .time .js-short-timestamp').getAttribute('data-time')}000`);
tweets.push({
id: tweetEl.getAttribute('data-item-id'),
username: tweetEl.querySelector('.content .stream-item-header .username b').textContent,
name: tweetEl.querySelector('.content .stream-item-header .fullname').textContent,
dateStr: date.toDateString(),
dateIso: date.toISOString(),
content: tweetEl.querySelector('.content .tweet-text').textContent,
numComments: +tweetEl.querySelector('.content .stream-item-footer .ProfileTweet-action--reply .ProfileTweet-actionCount .ProfileTweet-actionCountForPresentation').textContent,
numShares: +tweetEl.querySelector('.content .stream-item-footer .ProfileTweet-action--retweet .ProfileTweet-actionCount .ProfileTweet-actionCountForPresentation').textContent,
numLikes: +tweetEl.querySelector('.content .stream-item-footer .ProfileTweet-action--favorite .ProfileTweet-actionCount .ProfileTweet-actionCountForPresentation').textContent,
});
});
return tweets;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment