redouane59 / twittered

Twitter API client for Java developers
Apache License 2.0
236 stars 64 forks source link

IAPIEventListener using V2 route implements V1 Tweet Object #330

Closed ShaunRly closed 2 years ago

ShaunRly commented 2 years ago

Is there a way to implement a Filtered Stream that consumes TweetV2 objects? Currently IAPIEventListener seems to be using the TweetV1 type object which doesn't have access to certain fields, namely the MatchingRules field.

Apologies if I'm missing something here.

redouane59 commented 2 years ago

Hey, The filteredStream endpoint is part of V2 endpoints, the streamed tweets should be V2 objects. Do you have any example to share with me ? Thanks

ShaunRly commented 2 years ago

Sure, I'll post the block using the stream endpoint

public static void main(String[] args) throws JSONException, IOException, URISyntaxException {
        TwitterClient twitterClient = new TwitterClient(TwitterCredentials.builder()
                .accessToken(<KEY>)
                .apiKey(<KEY>)
                .apiSecretKey(<KEY>)
                .build());

        //twitterClient.addFilteredStreamRule("#NewYork sample:10", "base");

        IAPIEventListener eventListener = new IAPIEventListener() {
            @Override
            public void onStreamError(int i, String s) {
                System.out.println(s);
            }

            @Override
            public void onTweetStreamed(Tweet tweet) {
               sentimentProxy.postTweetsForSentimentAnalysis(new TweetDTO(
                       tweet.getText(),
                       tweet.getCreatedAt(),
                       tweet.getGeo().getCoordinates().toString()
               ));
            }

            @Override
            public void onUnknownDataStreamed(String s) {
                System.out.println("UNKNOWN - " + s);
            }

            @Override
            public void onStreamEnded(Exception e) {
                System.out.println(e);
            }
        };

        Map<String, String> rules = new HashMap<>();
        rules.put("#NewYork sample:10", "test rule");
        setupRules(twitterClient.getBearerToken(), rules);

        Future<Response> tweetStream = twitterClient.startFilteredStream(eventListener);
    }

Note i'm using external methods to setup the rules for the stream as i'm still having issues with the addFilteredStreamRule method, but I don't suspect that has anything to do with this issue.

As far as intelij knows, in the onTweetStreamed Event, it has no access to the matching rules field at that point.

redouane59 commented 2 years ago

Hmmm that's strange because the code looks ok. Can you show me the Tweet object that you are retrieving ? The matchingRules field is always null ? I just saw that the interface didn't contain the matching_rules field, did you try to cast your Tweet object in TweetV2 ?

ShaunRly commented 2 years ago

Ah casting got it! I didn't expect that to work honestly, should have been one of the first things i tried but I assumed it was getting instantiated as a V1 and thus wouldn't have those fields even after casting. Shows I still have a bit to learn, thanks!

But yes I think adding the V2 fields to the interface would be more foolproof in the long run. Thanks again!

redouane59 commented 2 years ago

Yes definitively, let's keep this issue open to not forget this improvement.

redouane59 commented 2 years ago

Is it ok for you #331 ?

ShaunRly commented 2 years ago

Looks great dude, I was just doing the same thing but you're quicker than I am! Thanks for the quick response and fix,

redouane59 commented 2 years ago

Looks great dude, I was just doing the same thing but you're quicker than I am! Thanks for the quick response and fix,

Haha don't worry, this will be available in the next release. Until then, just parse the Tweet to TweetV2.

Thanks !