1
votes

I am trying to use twitter4j to query twitter status data. I need tweets only for the user who posted them on his/her time line for a day. So far, I used this code to achieve this:

try {
    for (int i = 0; i < userNames.length; i++) {
        int totalCount = 0;
        Query query = new Query(userNames[i]);
        query.setCount(100);
        int searchResultCount;
        long lowestTweetId = Long.MAX_VALUE;
        totalCount = 0;
        Date date = new Date(
                DateTimeUtils.getNDaysbackDailySliceStamp(1));
        String modifiedDate = new SimpleDateFormat("yyyy-MM-dd")
                .format(date);
        System.out.println(modifiedDate);
        query.setSince(modifiedDate);
        date = new Date(DateTimeUtils.getDailySliceStamp());
        modifiedDate = new SimpleDateFormat("yyyy-MM-dd").format(date);
        System.out.println(modifiedDate);
        query.setUntil(modifiedDate);
        List<DBObject> dbl = new ArrayList<DBObject>();
        Set<Long> ste =  new HashSet<Long>();
        do {
            QueryResult queryResult = twitter.search(query);
            searchResultCount = queryResult.getTweets().size();
            for (Status st : queryResult.getTweets()) {
                if (!st.isRetweet()) {
                    URLEntity[] uEn = st.getURLEntities();
                    StringBuilder url = new StringBuilder();
                    for (URLEntity urle : uEn) {
                        if (urle.getURL() != null && !urle.getURL().isEmpty()) {
                            url.append(urle.getExpandedURL());
                        }
                    }
                    ste.add(st.getId());
                    dbl.add(createTweetObject(userNames[i]/*, total*/,
                            st.getText(), st.getRetweetCount(), st.getId(),
                            url.toString(), st.getCreatedAt(), st.isRetweet()));
                }
                }
        } while (searchResultCount != 0 && searchResultCount % 100 == 0);
        System.out.println(dbl.size());
        System.out.println(dbl);
        if (dbl != null && !dbl.isEmpty()) {
        //  populateTweetCollection(dbl);
        }
        System.out.println("TweetCount"+ste.size());
        System.out.println(ste);
    }
} catch (Exception e) {
    log.error("Exception in TwitterTime line api---"
            + Config.getStackTrace(e));
}

But this code gives me tweets made by others mentioning the User I am looking for.
For example I searched for my tweets in a day which were actually 8 but it gave me 12 results as some of my friends tweeted on their time line mentioning my twitter name using @username operator.

Also one thing i want to confirm if truncated tweet has same id for the whole group.

Regards

2

2 Answers

1
votes

Try this

        try {

         ResponseList<User> users = twitter.lookupUsers("user name"); 
         for (User auser : users) { 
             System.out.println("Friend's Name " + auser.getName()); 
             if (auser.getStatus() != null) {
                 System.out.println("Friend timeline"); 
                 List<Status> statusess =
                     twitter.getHomeTimeline(); 
                 for (Status status3 : statusess) {
                        System.out.println(status3.getText()); 
                 } 
            } 
        }
    } catch (TwitterException e) {
        e.printStackTrace();
    }
-1
votes

It worked using this code if (!st.isRetweet() && (st.getUser().getScreenName().equalsIgnoreCase(userNames[i]))) { URLEntity[] uEn = st.getURLEntities(); StringBuilder url = new StringBuilder(); } if (st.getId() < lowestTweetId) { lowestTweetId = st.getId(); query.setMaxId(lowestTweetId); }

I verified that it is not an RT and also userName screen Name is also similar to the user I am looking for.

Regards
Virendra Agarwal