Mustafa K.
Mustafa K.

Reputation: 45

How to filter tweets via the since/until parameters in Twitter4J queries?

I m listening twitter streams by coordinate using twitter4j without any input keyword, as i know twitter api just gives tweets of last 7 days. My code was working funny and i had an network problem and i could not get streams of 2 days ago. i need to get tweets of 22.04.2018 and 23.04.2018. I need to use until parameter to get these tweets but i could not see any sample of using until parameter without any input query. The following is my code statement when i listen for streams in given coordinates, how can i add until parameter to get past 2 days streams.

        double latitude1 = 36.000000;
        double longitude1 = 26.000000;
        double latitude2 = 42.000000;
        double longitude2 = 45.000000;

        double[][] latlong = {{longitude1, latitude1}, {longitude2, latitude2}};

        twitterStream.addListener(listener);
        FilterQuery fq = new FilterQuery(); 
        fq.locations(latlong);

        twitterStream.filter(fq);

So the question is how can i use FilterQuery with until,

i tried something like as follows ;

        MyConnectionBuilder myConnection = new MyConnectionBuilder();

        Twitter twitter = new 
        TwitterFactory(myConnection.configuration.build()).getInstance();

        double latitude1 = 42.000000;
        double longitude1 = 45.000000;


        Query query = new Query(); 

        GeoLocation obj = new GeoLocation(latitude1, longitude1);
        query.setGeoCode(obj, 2000, Unit.valueOf("km"));

        query.setSince("20180421");
        query.setUntil("20180422");
        query.setLang("tr");

        QueryResult result = twitter.search(query);

        for (Status status : result.getTweets()) {
            System.out.println("@" + status.getUser().getScreenName() + ":" + status.getText());
        }

but nothing comes from result query, what can be wrong with these code statements ?

Upvotes: 1

Views: 429

Answers (1)

MWiesner
MWiesner

Reputation: 9068

The official JavaDoc of Twitter4J's Query class tells us for setSince:

If specified, returns tweets with since the given date.

Date should be formatted as YYYY-MM-DD

So have to set a date in the String format YYYY-MM-DD, e.g., "2018-04-21". Note well, that dashes are used here which you did not provide in your original code snippet.

Analogously, you have to apply this date pattern style to setUntil:

If specified, returns tweets with generated before the given date.

Date should be formatted as YYYY-MM-DD

Hope this helps.

Upvotes: 1

Related Questions