redouane59 / twittered

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

Any example for streaming tweets? #327

Closed kitkars closed 2 years ago

kitkars commented 2 years ago

Can you give me an example for printing all the new tweets with specific hash tag?

I was looking at your FilteredStreamRulePredicate. I am not sure how it is connected with startFilteredStream

kitkars commented 2 years ago

What is wrong here?

    FilteredStreamRulePredicate rulePredicate = FilteredStreamRulePredicate.withHashtag("something");
    twitterClient.addFilteredStreamRule(rulePredicate, "something");

I get this error

  Exception in thread "main" java.lang.IllegalArgumentException
    at io.github.redouane59.twitter.TwitterClient.addFilteredStreamRule(TwitterClient.java:968)
    at io.github.redouane59.twitter.TwitterClient.addFilteredStreamRule(TwitterClient.java:979)
redouane59 commented 2 years ago

On my side, your code is running correctly. Are you sure to have correctly set up your twitter dev account credentials ? Are other endpoints working well ?

ShaunRly commented 2 years ago

I'm also having a similar problem using addFilteredStreamRule

twitterClient.addFilteredStreamRule("has:geo sample:10", "base");

Exception in thread "main" java.lang.IllegalArgumentException
    at io.github.redouane59.twitter.TwitterClient.addFilteredStreamRule(TwitterClient.java:981)
    at com.ironhack.Sentimentamap.TwitterAPIMS.Main.TwitterStreamer.main(TwitterStreamer.java:22)

The stream does work without the rule attached so the credentials are definitely being used correctly. Using breakpoints the issue seems to be happening here with data being returned as null.

public StreamRule addFilteredStreamRule(String value, String tag) {
        String url = this.urlHelper.getFilteredStreamRulesUrl();
        StreamRule rule = StreamRule.builder().value(value).tag(tag).build();

        try {
            String body = "{\"add\": [" + OBJECT_MAPPER.writeValueAsString(rule) + "]}";
            StreamRules result = (StreamRules)this.requestHelperV2.postRequest(url, body, StreamRules.class).orElseThrow(NoSuchElementException::new);
            if (result.getData() != null && !result.getData().isEmpty()) {
                return (StreamRule)result.getData().get(0);
            } else {
                throw new IllegalArgumentException();

-- EDIT The stream works perfectly fine by posting your own rules directly to rule post routing

redouane59 commented 2 years ago

Hmmm it's really hard for me to fix that as far as I'm not able to reproduce it locally :( Can you try to show me the raw result you have from the Twitter API ?

ShaunRly commented 2 years ago

Sure, direct response is

{"meta":{"sent":"2021-11-27T14:58:14.808Z","summary":{"created":0,"not_created":1,"valid":0,"invalid":1}},"errors":[{"value":"#NewYork sample:10","id":"1464244590714699776","title":"DuplicateRule","type":"https://api.twitter.com/2/problems/duplicate-rules"}]}

Which seems correct, but in abstractRequestHelper, it seems the problem may be with the method call of

result = convert(stringResponse, classType);

271120211638025859

redouane59 commented 2 years ago

Ok now I understand your problem. You are trying to add two times the same rule. The first time, it is working because the Twitter API is returning an http code 200. The second time, the Twitter API is returning an error code because you are not allowed to add the same rule twice. The addFilteredStreamRule method is throwing an exception because of that. This behaviour is the one expected.

BurnSaibot commented 2 years ago

Hi, got same troubles here, it would be convenient to raise/log the error from http response, else we don't know why adding the rule is throwing exceptions.

redouane59 commented 2 years ago

Hi, got same troubles here, it would be convenient to raise/log the error from http response, else we don't know why adding the rule is throwing exceptions.

You're right, I'm going to add it.