Home » Java » java – Spring-Social-Twitter hasMedia returns false although there is a picture attached-Exceptionshub

java – Spring-Social-Twitter hasMedia returns false although there is a picture attached-Exceptionshub

Posted by: admin February 25, 2020 Leave a comment

Questions:

I’m trying to build a twitter app that searches for some tweets and download the attached media. But what amazes me is this: Some tweets that obviously have an image attached to it, don’t have an image attached according to the Twitter API.
tweet.hasMedia() returns false and tweet.entities.media is null.

Here’s the output from my debugger in IDEA:

IDEA output tweet with allegedly no media

The tweet I’m talking about is this: Tweet 1230401137754484737 by @Wahlrecht_de. When you click on the picture, Tweet 1230401137754484737’s photo opens, where you can see the picture from tweet that allegedly has no media.

How come? Is this some hidden feature or flaw in the spring-social-twitter-API?

I’ve tried other tweets and there hasMedia() returns as expected true. For example Tweet 1230756978533650432 by @Marvin400 and many others. In this case, the debugger output clearly shows that there is an attached media:

IDEA debugger output for tweet with media

One thing I found out, that this special tweet has a “source” of “Twitter Web client” (see debugger output) whereas the unproblematic tweets I found had some source like “Twitter for iPhone”, “Plume”, …
Could this have something to do with it?

I’m using

  • org.springframework.social:spring-social-config:jar:1.1.4.RELEASE
  • org.springframework.social:spring-social-twitter:jar:1.1.2.RELEASE
  • org.springframework.social:spring-social-core:jar:1.1.4.RELEASE
  • org.springframework.social:spring-social-web:jar:1.1.4.RELEASE

Here’s my simplified Code:

TwitterTemplateFactory.java:

@Component
public class TwitterTemplateFactory {

  @Value("${twitter.consumerKey}")
  private String consumerKey = null;
  @Value("${twitter.consumerSecret}")
  private String consumerSecret = null;
  @Value("${twitter.accessToken}")
  private String accessToken = null;
  @Value("${twitter.accessTokenSecret}")
  private String accessTokenSecret = null;

  public TimelineOperations timelineOperations() {
    return complexTemplate().timelineOperations();
  }

  public SearchOperations searchOperations() {
    return simpleTemplate().searchOperations();
  }

  private TwitterTemplate simpleTemplate() {
    return new TwitterTemplate(consumerKey, consumerSecret);
  }

  private TwitterTemplate complexTemplate() {
    return new TwitterTemplate(consumerKey, consumerSecret, accessToken, accessTokenSecret);
  }

}

MyScheduler.java

@Component
public class MyScheduler {
  @Autowired
  private TwitterTemplateFactory twitterTemplateFactory;

  @Scheduled(fixedDelay=600000L) // invoke every 10 minutes
  public void runScheduledTask() {
    String query = "from:Wahlrecht_de since:2020-02-20 -filter:retweets"; 
    List<Tweet> tweets = templateFactory.searchOperations().search(new SearchParameters(query)).getTweets();
    tweets.stream()
      .filter(Tweet::hasMedia)      // this does not work - tweet.hasMedia is false :-O!
      .forEach( tweet -> { ... } ); // forEach will not be entered because hasMedia is false!
  }
}

Screenshots:

Tweet with embedded media:
Tweet with allegedly no media Screenshot

Tweet when you click on the embedded media:
Tweet with allegedly no media Screenshot (clicked on image)

Any help appreciated.

How to&Answers: