Twitter查询API过滤器无法在日期范围

时间:2015-10-08 05:09:51

标签: java twitter twitter4j

我正在尝试使用twitter4j来查询推特状态数据。我只需要在他/她的时间线上发布一天的用户的推文。 到目前为止,我使用此代码来实现此目的:

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));
}

但是这段代码给了我其他人提到我正在寻找的用户的推文 例如,我在一天中搜索了我的推文实际上是8但是它给了我12个结果,因为我的一些朋友在他们的时间线上发了推文,用@username运算符提到了我的推特名称。

还有一件事我想确认截断的推文是否对整个组都有相同的id

此致

2 个答案:

答案 0 :(得分:1)

试试这个

        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 :(得分:-1)

使用此代码 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); }

我确认它不是RT和userName屏幕名称也类似于我要查找的用户。

问候
Virendra Agarwal