twitter4j搜索具有特定帐户创建日期的用户

时间:2014-03-05 03:01:28

标签: twitter twitter4j

我想在Twitter上搜索特定帐户创建日期的所有用户。那么Twitter api允许这样做吗?如果是,我如何使用twitter4j?

2 个答案:

答案 0 :(得分:0)

我不确定是否可能。但是,您可以使用以下内容。

例如,假设您正在搜索在2010-03-05(yyyy-MM-dd)中创建的用户

您可以创建一个类似下面的查询,通过提供自从和之后的日期点,例如从1点开始到1点以后到达点。因为没有明确的字段来设置确切的日期,所以我提出了这个想法。然后,您应该使用分页机制调用SearchUsers()方法。您可以在此探索如何实现分页,我不记得它是如何,但您可以轻松地在Internet上找到一个示例。请尝试以下代码,我希望它能够正常运行:

 try
        {
            Query query = new Query();
            query.setSince("2010-03-04");
            query.setUntil("2010-03-06");
            ResponseList<User> userList = twitterObj.searchUsers(query.toString(), -1);

            for (User userItem : userList)
            {
                // Then here you can do whatever you want by using userItem object
            }

        }
        catch (TwitterException ex)
        {
            // Do necessary error handling mechanism here
        }

答案 1 :(得分:0)

public void tweetSearch(String queryRequest) throws IOException, TwitterException{
        // Create configuration builder and set key, token etc
        ConfigurationBuilder cb = new ConfigurationBuilder();
                cb.setDebugEnabled(true);
        cb.setOAuthConsumerKey("xxxxx"); 
// type your provided Consumer key and consumer secret key from twitter
//and leave access token access secret token as blank



cb.setOAuthConsumerSecret("xxxx");
    cb.setOAuthAccessToken("xxxx");
    cb.setOAuthAccessTokenSecret("xxxx");

    // Create Twitter instance
    Twitter Twitter = new TwitterFactory(cb.build()).getInstance();

    // Create file writer and buffer writer
    FileWriter fstream = new FileWriter("Twitterstream.txt",true);
    BufferedWriter out = new BufferedWriter(fstream);

    // Create Query object and set search string
    Query query = new Query("");
            //change the date as u wish...
            query.setSince("2014-06-12");
            query.setUntil("2014-06-14");
    query.setQuery(queryRequest);

    // Get query result
    QueryResult qr = Twitter.search(query);

    // Get tweets and write in the file
    while(qr.hasNext()){
        qr.nextQuery();
        List<Status> tweets = qr.getTweets();

        for (Status t: tweets){
                System.out.println(t.getId() + " - " + t.getCreatedAt() + ": " + t.getText());

            out.write("\n"+t.getId()+",");
            out.write("\t"+t.getText()+",");
            out.write("\t"+t.getUser()+",");
        }

    }
            System.out.println("Generated Twitter Stream");

}