Example usage for twitter4j Query since

List of usage examples for twitter4j Query since

Introduction

In this page you can find the example usage for twitter4j Query since.

Prototype

String since

To view the source code for twitter4j Query since.

Click Source Link

Usage

From source file:crawling.SearchTweetsHashtag.java

License:Apache License

public static void main(String[] args) {

    Runtime.getRuntime().addShutdownHook(new Thread() {
        public void run() {
            /* my shutdown code here */
            // Record the user status
            out.println("Final status -------------------:");
            for (Long id : discoveredTweets.keySet()) {
                out.println(id + "," + discoveredTweets.get(id));
            }/*from  ww w.j  a  va  2s  .  c  o  m*/
            out.close();

            DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
            Date date = new Date();
            date = new Date();
            System.out.println("End at: " + dateFormat.format(date));
        }
    });

    if (args.length < 1) {
        System.out.println("Usage: java twitter4j.examples.PrintFilterStreamHashtag hashtag");
        System.exit(-1);
    }
    if (args.length == 2) {
        // Preload the collected user IDs
        //preloadID(args[1]);
    }

    buildStartTime();

    File file = new File(fileName);
    InputStream is = null;

    try {
        if (file.exists()) {
            is = new FileInputStream(file);
            prop.load(is);
        } else {
            System.out.println(fileName + " doesn't exist!");
            System.exit(-1);
        }
    } catch (IOException ioe) {
        ioe.printStackTrace();
        System.exit(-1);
    }

    try {
        FileWriter outFile = new FileWriter(args[0] + ".txt", true);
        out = new PrintWriter(outFile);
        hashtag = args[0];
        //out.close();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    avgUsers = new int[histCount];

    Query query = new Query();
    query.setQuery(hashtag);

    //query.setCount(100);
    query.since("2015-03-15");
    //query.setPage(20);

    //Twitter twitter = new TwitterFactory().getInstance();
    Twitter twitter = getOAuthTwitter();

    DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
    Date date = new Date();
    //System.out.println();
    System.out.println("----------------------------------------------");
    System.out.println("Start at: " + dateFormat.format(date));

    while (true) {
        //for (int i = 1; i <= 15; i ++){
        //   query.setPage(i);
        try {
            doASearch(twitter, query);
        } catch (TwitterException te) {
            // TODO Auto-generated catch block
            te.printStackTrace();
            System.out.println("Failed to search tweets: " + te.getMessage());

            // back-off
            Thread.currentThread();
            try {
                Thread.sleep(60 * 1000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

        Thread.currentThread();
        try {
            Thread.sleep(5 * 1000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}