Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Twitter4j: how to get twit url from status?

Tags:

java

twitter

I am using twitter4j-3.0.5 on Windows.

When new status arrives to the StatusListener.onStatus() method, I would like to print out it's URL but I don't find any appropriate method. status.getURLEntities() is always empty, even tough if I look at the json passed to the client, the url field is not empty and contains valid URL. I just don't know how to get this value from java.

Please advise.

like image 233
Moisei Avatar asked Feb 26 '14 15:02

Moisei


People also ask

How do you get the URL of a tweet?

Navigate to the Tweet you'd like the URL of. Click the icon located within the Tweet. From the pop-up menu, select Copy link to Tweet. The URL should now be copied to your clipboard.

What is a Twitter URL?

The easiest way is to type twitter.com/username which is what your Twitter web address is. On desktop, login to your Twitter account in any web browser and go to your profile page to copy your Twitter URL.


2 Answers

The getURLEntities() method returns any URLs in the content of the tweet itself, not the URL of the tweet.

Fortunately the URL of a tweet is in the following format:

https://twitter.com/[screen name of user]/status/[id of status]

So you can get the URL of a status with this code:

Status status;
String url= "https://twitter.com/" + status.getUser().getScreenName() 
    + "/status/" + status.getId();
System.out.println(url);
like image 170
Jason Pather Avatar answered Sep 18 '22 00:09

Jason Pather


URL can be exposed by 2 ways.

  • giving the twitter link

  • giving the website link which is tweeted.

Sample Example:

package com.twitter.example;

import java.util.List;

import twitter4j.Status;
import twitter4j.Twitter;
import twitter4j.TwitterException;
import twitter4j.TwitterFactory;
import twitter4j.URLEntity;
import twitter4j.conf.ConfigurationBuilder;

public class TwitterApplication {
    public static void main(String[] args) throws TwitterException {
        ConfigurationBuilder cf = new ConfigurationBuilder();
        cf.setOAuthConsumerKey("OAUTH_CONSUMER_KEY")
                .setOAuthConsumerSecret("OAUTH_CONSUMER_KEY_SECRET")
                .setOAuthAccessToken("OAUTH_ACCESS_TOKEN")
                .setOAuthAccessTokenSecret("OAUTH_ACCESS_TOKEN_SECRET");

        TwitterFactory tf = new TwitterFactory(cf.build());
        Twitter twitter = tf.getInstance();

        // get username and status
        List<Status> statusList = twitter.getHomeTimeline();

        int i = 0;
        for (Status status : statusList) {
            i++;
            String url = "https://twitter.com/" + status.getUser().getScreenName() + "/status/" + status.getId();
            System.out.println("Twitter URL:"+url);

            URLEntity[] urls = status.getURLEntities();
            for(URLEntity urlEntity : urls) {
                url = urlEntity.getURL();
                System.out.println("Website link URL in the tweet: "+url);
            }
            if (i == 1)
                break;
        }
    }
}

Output:

Twitter URL:https://twitter.com/CulturalGM/status/966308069884952577

Website link URL in the tweet: https://websitelink

like image 33
SkyWalker Avatar answered Sep 22 '22 00:09

SkyWalker