Henrik Petterson
Henrik Petterson

Reputation: 7094

Direct link to Twitter retweet

I am working on a Twitter app and I want to find a way to link directly to a retweet.

For example. If user A makes a tweet, and user B retweets it, I want a URL that will take us to user B's profile where the retweet is displayed. What is that unique URL?

Upvotes: 3

Views: 3315

Answers (3)

JamesTheAwesomeDude
JamesTheAwesomeDude

Reputation: 1053

I recently needed to get a link to an arbitrary retweet that was too old for the search feature to work.

Here's what I did:

  1. Open the Network Monitor

  2. Visit the timeline of the retweeter

  3. Scroll until you find the retweet

  4. Look at the Network Monitor, open the latest request to the endpoint /UserTweets

  5. Either

    • go to the "Response" tab and sift through its JSON manually to get the id_str to craft the URL:
      (parsed data from Twitter /UserTweets endpoint)

    • or, paste the following into the Console, then right-click on the response, “Copy…Response”, and return to the console to press Enter:

    UserTweets = JSON.parse(prompt("Paste the copied Response data:"));
    
    // get the output from that endpoint,
    UserTweets
    // dig through the packaging,
    ['data']['user']['result']['timeline_v2']['timeline']['instructions']
    // filter out "pinned tweets" metadata,
    .filter(cmd => cmd['type'] == "TimelineAddEntries")
    // de-batch,
    .flatmap(cmd => cmd['entries'])
    // filter out "cursor" metadata,
    .filter(entry => entry['content']['entryType'] == "TimelineTimelineItem" )
    // then dig through a bunch more layers of packaging,
    .map(entry => entry['content']['itemContent']['tweet_results']['result'])
    // munge the tweets into a more-usable format,
    .map(tweet => ({user: tweet['core']['user_results']['legacy'], tweet: tweet['legacy']}))
    // filter out non-retweets,
    .filter(({tweet}) => tweet['retweeted_status_result'])
    // extract just the RT text and URL of the retweet itself,
    .map(({user, tweet}) => ({url: `https://twitter.com/${encodeURIComponent(user['screen_name'])}/status/${encodeURIComponent(tweet['id_str'])}`, text: tweet['full_text']}))
    // print results.
    .forEach(({url, text}) => console.log(url, text))
    

et voilà:

https://twitter.com/ThePSF/status/1398372497741946884

Upvotes: 7

f0i
f0i

Reputation: 31

JamesTheAwesomeDude's answer worked for me with some light modifications for the new twitter API:

UserTweets = { /*PASTE HERE*/ }

// .timeline changed to .timeline_v2
UserTweets.data.user.result.timeline_v2.timeline.instructions
.filter(cmd => cmd['type'] == "TimelineAddEntries")
.map(cmd => cmd['entries']).flat()
.filter(entry => entry['content']['entryType'] == "TimelineTimelineItem" )
.map(entry => entry['content']['itemContent']['tweet_results']['result'])
// .user changed to .user_results.result
.map(tweet => [tweet['core']['user_results']['result']['legacy'], tweet['legacy']])
.filter(([user, tweet]) => tweet['retweeted_status_result'])
.map(([user, tweet]) => [`https://twitter.com/${user['screen_name']}/status/${tweet['id_str']}`, tweet['full_text']])
.forEach(([url, rt_text]) => console.log(url, rt_text))

Upvotes: 1

Chlod Alejandro
Chlod Alejandro

Reputation: 606

Internally, all Retweets are simply special Tweets. This means each Retweet also has an ID (which is stored on id and id_str at the root of the Tweet object). For proof, here's a Retweet from the Twitter Retweets account.

If you're using Tweet Streams (e.g. statuses/filter), you'll be able to pick up this ID from the returned Tweet object of the retweet. From the ID, you can just build a normal Twitter link with https://twitter.com/<username>/status/<retweet id>. Assuming your Retweet object is named retweet, the correct link would be (in JavaScript) `https://twitter.com/${retweet.user.screen_name}/status/${retweet.id_str}`.

If the Retweet is fairly recent, you can do a status search (search/tweets) on the user's profile (i.e. you have to set the q parameter to from:<username>) to find the Retweet. You'll most likely have to cross check the ID of the Tweet you want and the ID of the Retweet you're looking for to be sure, though.

If you're trying to get the Retweet ID of an old Tweet, however, you might have to use Twitter's Premium APIs, which are paid.

Upvotes: 3

Related Questions